完成下面父类及子类的声明:
(1) 声明Student类。
属性包括:学号、姓名、英语成绩、数学成绩、计算机成绩、总成绩。
方法包括:构造方法、get方法、set方法、 toString方法、compare方法(比较二个学生的总成绩大小,结果分为大于、等于、小于),sum方法(计算总成绩)、testScore方法(计算测评成绩)。
(2)声明StudentXW(学习委员)类为Student类的子类。
在StudentXW类中增加责任属性,并重写testScore方法(测评成绩=平均分+3)。
(3)声明StudentBZ类中增加责任属性,并重写testScore方法(测评成绩=平均分+5)。
(4)声明测试类,生成若干个Student类、StudentXW类及StudentBZ类对象,并分别计算它们的测评成绩。
Student类
代码:
package test1;
public class Student {
protected int id;
protected String name;
protected int English;
protected int math;
protected int computer;
protected int sum;
protected double average;
public void set_Id(int id){
this.id=id;
}
public void set_Name(String name){
this.name=name;
}
public void set_English(int English){
this.English=English;
}
public void set_Math(int math){
this.math=math;
}
public void set_Computer(int computer){
this.computer=computer;
}
public int get_Id(){
return id;
}
public String get_Name(){
return name;
}
public int get_English(){
return English;
}
public int get_Math(){
return math;
}
public int get_Computer(int computer){
return computer;
}
public int get_Sun(){
sum=English+math+computer;
return sum;
}
public double testScore(){
average=(double)((English+math+computer)/3);
return average;
}
Student(){
}
public Student(int id,String name,int English,int math,int computer){
this.id=id;
this.name=name;
this.English=English;
this.math=math;
this.computer=computer;
}
public void compare(int a,int b){
if(a==b)
System.out.println("总分相等");
else if(a>b)
System.out.println(a+"大于"+b);
else
System.out.println(a+"小于"+b);
}
public String toString(){
return("姓名:"+name+" "+"英语="+English+" "+"数学="+math+" "+"电脑="+computer+" "+"总分="+sum);
}
StudentXZ
代码:
package test1;
public class StudentXW extends Student{
private String xwduty;
StudentXW(String xwduty,int id,String name,int English,int math,int computer){
super(id, name, English, math, computer);
this.xwduty=xwduty;
}
public void zhiwu(){
System.out.print("职务:"+xwduty+" ");
}
@Override
public double testScore(){
average=(double)((English+math+computer)/3+3);
return average;
}
}
StudentBZ类
代码:
package test1;
public class StudentBZ extends Student{
protected String bzduty;
public StudentBZ(String bzduty,int id,String name,int English,int math,int computer){
super( id, name, English, math, computer);
this.bzduty=bzduty;
}
public void zhiwu(){
System.out.print("职务:"+bzduty+" ");
}
@Override
public double testScore(){
average=(double)((English+math+computer)/3+5);
return average;
}
}
测试类
代码:
package test1;
public class tsetClass {
/**
* @param args
*/
public static void main(String[] args) {
Student s1=new Student(36,"zhang",75,80,85);
s1.get_Sun();
System.out.println(s1.toString());
System.out.println("average="+s1.testScore());
StudentXW s2=new StudentXW("XueXiWeiYuan",36,"zhang",76,80,85);
s2.get_Sun();
s2.zhiwu();
System.out.println(s2.toString());
System.out.println("average="+s2.testScore());
StudentBZ s3=new StudentBZ("BanZhang",39,"wang",76,89,95);
s3.get_Sun();
s3.zhiwu();
System.out.println(s3.toString());
System.out.println("average="+s3.testScore());
s1.compare(s2.get_Sun(), s3.get_Sun());
}
}
运行结果: