Java__实验三 面向对象编程

【任务一】:编写一个计算各种形状的面积和周长的程序。
要求:父类Shape2D为一个抽象类,其中包含计算周长和计算面积两个方法。其他形状圆(Circle),三角形(Triangle)和长方形(Rectangle)均继承自Shape2D类。而Square继承自Rectangle类。编写测试类来计算这些形状的面积。
提示:不同的形状包含的属性应该不同,构造函数应该不同,计算周长和面积的方法也应该不同。
抽象类

	public abstract class Shape2D{
	public abstract double getCircuference();
	public abstract double getArea();
	}
public class Circle extends Shape2D{}

[代码]

package shape;

public abstract class Shape2D {
	public abstract double getCircumference(); //计算周长
	public abstract double getArea(); //计算面积
}


package shape;
import java.lang.*;

public class Circle extends Shape2D {
	
	private double r; //圆的半径

	public Circle(double r) {
        this.r = r;
	}

	@Override
	public double getCircumference() {
		return 2*Math.PI*r;
	}

	@Override
	public double getArea() {
		return Math.PI*r*r;
	}

}


package shape;
import java.lang.*;

public class Triangle extends Shape2D {
	
	private double a;
	private double b;
	private double c;

	public Triangle(double a, double b, double c) {
		this.a=a;
		this.b=b;
		this.c=c;
	}

	@Override
	public double getCircumference() {
		// TODO Auto-generated method stub
		return a+b+c;
	}

	@Override
	public double getArea() {
		// TODO Auto-generated method stub
		double p = (a+b+c)/2.0;
		return Math.sqrt(p*(p-a)*(p-b)*(p-c));
	}

}


package shape;

public class Rectangle extends Shape2D {

	private double  c;
	private double  k;
	
	public Rectangle(double c,double k) {
		// TODO Auto-generated constructor stub
		this.c=c;
		this.k=k;
	}
	
	@Override
	public double getCircumference() {
		// TODO Auto-generated method stub
		return 2*(c+k);
	}

	@Override
	public double getArea() {
		// TODO Auto-generated method stub
		return c*k;
	}


}


package shape;

public class TestShape {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//测试矩形类,求矩形的周长和面积
		Rectangle Rectrect=new Rectangle(5, 6);
        System.out.println("长是5,宽是6的矩形的周长是: "+Rectrect.getCircumference());
        System.out.println("长是5,宽是6的矩形的面积是: "+Rectrect.getArea());
        
        //测试圆形类,求圆形的周长和面积
        Circle circle=new Circle(5);
        System.out.println("\n半径是5的圆的周长是: "+circle.getCircumference());
        System.out.println("半径是5的圆的面积是: "+circle.getArea());
        
        
        //测试三角形类,求三角形的周长和面积
        Triangle triangle=new Triangle(3,4,5);
        System.out.println("\n边长为3,4,5的三角形的周长是: "+triangle.getCircumference());
        System.out.println("边长为3,4,5的三角形的面积是: "+triangle.getArea());
	}

}


public class test3 {

	public static void main(String[] args) {
		Line2D a = new Line2D(1, 2);
		Point2D x = new Point2D(-1, 0);
		Point2D y = new Point2D(0, 1);
		Line2D c = new Line2D(-1, 1, 1);
		Line2D b = null;
		if(x.equals(y)) {
			System.out.println("x,y两点相同,无法构造两点式");
		}else {
			System.out.println("x,y两点不相同,可以构造两点式");
			b = new Line2D(x, y);
		}
		if(c.equals(b)) {
			System.out.println("c,b两直线相等");
		}
		else {
			System.out.println("c,b两直线不等");
		}
		if(a.isParallel(b)) {
			System.out.println("a与b平行");
		}else {
			System.out.println("a与b不平行");
		}
		
	}
}

【任务三】:设计并实现三个类:班级(Class)、学生(Student)、课程(Course)。然后计算班上每个同学的加权成绩。最后允许用户输入学生的学号查询学生信息。
要求:班级类的私有属性包含班级名称、学生人数以及学生列表(用对象数组表示)。班级类的方法包括:
 构造函数Class(int nSutdent)
 使用addStudent(Student student)来往班级中添加学生
 使用calculateWeightedGrade()方法计算班上所有学生的加权成绩
 重写toString()方法来将班级信息转换成字符串
 编写方法获得班上的学生

学生类的私有属性包含学生的学号、姓名、所选课程数量、所选课程(用对象数组表示)。学生类的方法包括
构造函数Student(int id, String name, int nCourse)
使用addCourse(Course course)来往学生类中添加课程
使用calculateWeightedGrade()来计算该学生的加权成绩
重写toString()方法来将学生信息转换成字符串
使用printStudentInfo()方法来打印学生的基本信息
课程类包含私有属性课程编号、课程名称、学分、是否缓考以及成绩(成绩要求在0到100分之间)。方法包括获取课程各个属性的方法。
构造函数Course(int id, String name, int credit)
使用方法设置是否缓考和成绩
重写toString()方法来将课程信息转换成字符串
编写一个测试类来测试这三个类的使用。要求最少往班级中创建5个学生,学生可以选修0到最多5门课程。程序运行后,先计算好班级中每个同学的加权成绩。然后等待用户输入学号查询学生,如果有对应的学生,则输出学生的信息、选修课程信息、最后输出加权成绩。直到用户输入-1退出程序。
提示:加权成绩的计算公式为:
g=(∑_(i=1)^n▒〖s_i w_i 〗)/(∑_(i=1)^n▒w_i )
其中,g表示最后的加权成绩,n表示同学选修的课程数,si表示第i门课的成绩,wi表示第i门课的学分。
注意:如果某生某课程缓考,则该门课成绩不能计入加权成绩内。此外,类中不可出现公用属性,必 须使用封装。
对象数组

public class Student{}
public class Class{
	private Student[] stulist;
}

封装思想的体现

get/set

[代码]

package test3;
//鐝骇绫荤殑绉佹湁灞炴�у寘鍚彮绾у悕绉般�佸鐢熶汉鏁颁互鍙婂鐢熷垪琛紙鐢ㄥ璞℃暟缁勮〃绀猴級銆傜彮绾х被鐨勬柟娉曞寘鎷細
//飦瀯閫犲嚱鏁癈lass(int nSutdent)
//飦娇鐢╝ddStudent(Student student)鏉ュ線鐝骇涓坊鍔犲鐢�
//飦娇鐢╟alculateWeightedGrade()鏂规硶璁$畻鐝笂鎵�鏈夊鐢熺殑鍔犳潈鎴愮哗
//飦噸鍐檛oString()鏂规硶鏉ュ皢鐝骇淇℃伅杞崲鎴愬瓧绗︿覆
//飦紪鍐欐柟娉曡幏寰楃彮涓婄殑瀛︾敓
public class Class {
	private String Classname;
	private int Stunumber;
	private Student[] Stulist;
	private int i = 0;
	
	public Class(String classname, int stunumber) {
		super();
		Classname = classname;
		Stunumber = stunumber;
		this.Stulist = new Student[this.Stunumber];
	}
	public void addStudent(Student student)
	{
		this.Stulist[i] = student;
		i++;
	}
	public void calculateWeightedGrade()
	{
		for(int i = 0;i < this.Stunumber;i++)
		{
			this.Stulist[i].getWeightedGrade();
		}
	}
	public void findstu(int a)
	{
		int j = -1;
		for(int i = 0 ;i < this.Stunumber;i++)
		{
			if(this.Stulist[i].getId() == a)
			j = i;
		}
		if(j!=-1)
		{
			this.Stulist[j].printStudentInfo();
		}
		else
		{
			System.out.println("Don't find");
		}
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "This class classname is "+this.Classname+",stunumber is "+this.Stunumber;
	}
	
}


package test3;
//瀛︾敓绫荤殑绉佹湁灞炴�у寘鍚鐢熺殑瀛﹀彿銆佸鍚嶃�佹墍閫夎绋嬫暟閲忋�佹墍閫夎绋嬶紙鐢ㄥ璞℃暟缁勮〃绀猴級銆傚鐢熺被鐨勬柟娉曞寘鎷�
//飦瀯閫犲嚱鏁癝tudent(int id, String name, int nCourse)
//飦娇鐢╝ddCourse(Course course)鏉ュ線瀛︾敓绫讳腑娣诲姞璇剧▼
//飦娇鐢╟alculateWeightedGrade()鏉ヨ绠楄瀛︾敓鐨勫姞鏉冩垚缁�
//飦噸鍐檛oString()鏂规硶鏉ュ皢瀛︾敓淇℃伅杞崲鎴愬瓧绗︿覆
//飦娇鐢╬rintStudentInfo()鏂规硶鏉ユ墦鍗板鐢熺殑鍩烘湰淇℃伅
public class Student {
	private int id;
	private String name;
	private int nCourse;
	private Course[] course;
	private int WeightedGrade = 0;
	private int i = 0;
	
	
	public Student(int id, String name, int nCourse) {
		super();
		this.id = id;
		this.name = name;
		this.nCourse = nCourse;
		course = new Course[nCourse];
	}
	public int getId() {
		return id;
	}
	public int getWeightedGrade() {
		return WeightedGrade;
	}
	public void addCourse(Course course)
	{
		this.course[i] = course;
		i++;
	}
	private void calculateWeightedGrade()
	{
		int sumgrade = 0;
		int sumcredit = 0;
		for(int i = 0;i < this.nCourse;i++)
		{
			if(!this.course[i].isDelay())
			{
				sumgrade = sumgrade+this.course[i].getGrade() * this.course[i].getCredit();
				sumcredit = sumcredit+this.course[i].getCredit();
			}
			
		}
		this.WeightedGrade =  sumgrade/sumcredit;
	}
	public void printStudentInfo()
	{
		System.out.println("This student id is "+id+",name is "+name+",nCourse is "+nCourse);
		for(int i = 0;i<this.nCourse;i++)
		{
			System.out.println("    "+this.course[i].toString()+"    "+this.course[i].getGrade());
			
		}
		calculateWeightedGrade();
		System.out.println("WeightedGrade is "+this.WeightedGrade);
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "This student id is "+id+",name is "+name+",nCourse is"+nCourse;
	}
}


package test3;
//璇剧▼绫诲寘鍚鏈夊睘鎬ц绋嬬紪鍙枫�佽绋嬪悕绉般�佸鍒嗐�佹槸鍚︾紦鑰冧互鍙婃垚缁╋紙鎴愮哗瑕佹眰鍦�0鍒�100鍒嗕箣闂达級銆傛柟娉曞寘鎷幏鍙栬绋嬪悇涓睘鎬х殑鏂规硶銆�
//飦瀯閫犲嚱鏁癈ourse(int id, String name, int credit)
//飦娇鐢ㄦ柟娉曡缃槸鍚︾紦鑰冨拰鎴愮哗
//飦噸鍐檛oString()鏂规硶鏉ュ皢璇剧▼淇℃伅杞崲鎴愬瓧绗︿覆
public class Course {
	
	private int id;
	private String name; 
	private int credit;
	private boolean delay;
	private int grade;
	
	public Course(int id, String name, int credit) {
		super();
		this.id = id;
		this.name = name;
		this.credit = credit;
	}
	public boolean isDelay() {
		return delay;
	}
	public void setDelay(boolean delay) {
		this.delay = delay;
	}
	public int getGrade() {
		return grade;
	}
	public void setGrade(int grade) {
		if(grade>=0&&grade<=100)
		this.grade = grade;
		else
		{
			System.out.println("grade is error");
			this.grade = -1;
		}
	}	
	public int getCredit() {
		return credit;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return id +"    " + name +  "    " + credit;
	}
	
}


package test3;

import java.util.Scanner;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Class myclass = new Class("NO.1",5);
		
		Student stu1 = new Student(1,"A",1);
		Course c1 = new Course(1,"GS",5);
		c1.setGrade(91);
		c1.setDelay(false);
		stu1.addCourse(c1);
		myclass.addStudent(stu1);
		
		Student stu2 = new Student(2,"B",2);
		Course c2 = new Course(1,"GS",5);
		c2.setGrade(92);
		c2.setDelay(false);
		stu2.addCourse(c2);
		Course c3 = new Course(2,"XD",4);
		c3.setGrade(92);
		c3.setDelay(false);
		stu2.addCourse(c3);
		myclass.addStudent(stu2);
		
		
		Student stu3 = new Student(3,"C",3);
		Course c4 = new Course(1,"GS",5);
		c4.setGrade(93);
		c4.setDelay(false);
		stu3.addCourse(c4);
		Course c5 = new Course(2,"XD",4);
		c5.setGrade(93);
		c5.setDelay(false);
		stu3.addCourse(c5);
		Course c6 = new Course(3,"LX",3);
		c6.setGrade(93);
		c6.setDelay(false);
		stu3.addCourse(c6);
		myclass.addStudent(stu3);
		
		
		Student stu4 = new Student(4,"D",4);
		Course c7 = new Course(1,"GS",5);
		c7.setGrade(94);
		c7.setDelay(false);
		stu4.addCourse(c7);
		Course c8 = new Course(2,"XD",4);
		c8.setGrade(94);
		c8.setDelay(false);
		stu4.addCourse(c8);
		Course c9 = new Course(3,"LX",3);
		c9.setGrade(94);
		c9.setDelay(false);
		stu4.addCourse(c9);
		Course c10 = new Course(4,"YY",2);
		c10.setGrade(94);
		c10.setDelay(false);
		stu4.addCourse(c10);
		myclass.addStudent(stu4);
		
		
		Student stu5 = new Student(5,"E",5);
		Course c11 = new Course(1,"GS",5);
		c11.setGrade(95);
		c11.setDelay(false);
		stu5.addCourse(c11);
		Course c12 = new Course(2,"XD",4);
		c12.setGrade(95);
		c12.setDelay(false);
		stu5.addCourse(c12);
		Course c13 = new Course(3,"LX",3);
		c13.setGrade(95);
		c13.setDelay(false);
		stu5.addCourse(c13);
		Course c14 = new Course(4,"YY",2);
		c14.setGrade(95);
		c14.setDelay(false);
		stu5.addCourse(c14);
		Course c15 = new Course(5,"HB",1);
		c15.setGrade(95);
		c15.setDelay(true);
		stu5.addCourse(c15);
		myclass.addStudent(stu5);
		
		int i=1;
		while(i==1)
		{
			System.out.println(myclass.toString());
			myclass.calculateWeightedGrade();
			System.out.println("Input student number:");
			Scanner input= new Scanner(System.in);
			int in = input.nextInt();
			if(in == -1)
			{
				i = 0;
			}
			else
			{
				myclass.findstu(in);
			}
		}
	}

}


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值