问题描述
(1)声明Student类。
属性包括学号(number)、姓名(name)、英语成绩(englishScore)、数学成绩(mathScore)、计算机成绩(computerScore)和总成绩(totalScore)。
方法包括构造方法、get方法、set方法、compare方法(比较两个学生的总成绩,结果分大于、小于、等于)、sum方法(计算总成绩)和testScore方法(计算评测成绩)。
注:评测成绩可以取三门课成绩的平均分,另外任何一门课的成绩的改变都需要对总成绩进行重新计算。
(2)声明StudentXW(学习委员)类为Student类的子类。
在StudentXW类中增加责任(responsibility)属性,并重写testScore方法(计算评测成绩,评测成绩=三门课的平均分+3)。
(3)声明StudentBZ(班长)类为Student类的子类。
在StudentBZ类中增加责任(responsibility)属性,并重写testScore方法(计算评测成绩,评测成绩=三门课的平均分+5)
(4)声明测试类,生成若干个Student类、StudentXW类及StudentBZ类对象,并分别计算它们的评测成绩,以及互相进行总成绩的对比。
完整代码
public class Main {
public static void main(String[] args) {
Student s1=new Student(12,"Lisi",70,70,70);
s1.sum();
System.out.println(s1.getName()+"'s evaluation score is " +s1.testScore());
Student s2=new Student(13,"Zhangsan",93,93,93);
s2.sum();
System.out.println(s2.getName()+"'s evaluation score is " +s2.testScore());
Student s3=new Student(14,"Wangwu",105,105,105);
s3.sum();
System.out.println(s3.getName()+"'s evaluation score is " +s3.testScore());
Student s4=new Student(15,"Zhaoliu",70,70,70);
s4.sum();
System.out.println(s4.getName()+"'s evaluation score is " +s4.testScore());
System.out.println();
s4.compare(s2,s1);
s4.compare(s2,s3);
s4.compare(s1,s4);
}
static class Student{
private int number;
private String name;
private double englishScore;
private double mathScore;
private double computerScore;
private double totalScore;
public Student(){}
public Student(int number,String name,double englishScore,double mathScore,double computerScore){
this.number=number;
this.name=name;
this.englishScore=englishScore;
this.mathScore=mathScore;
this.computerScore=computerScore;
}
public void setNumber(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
}
public double getEnglishScore() {
return englishScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
public double getMathScore() {
return mathScore;
}
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
}
public double getComputerScore() {
return computerScore;
}
void compare(Student s1,Student s2){
if(s1.totalScore>s2.totalScore)
System.out.println(s1.name+"'s total score is higher than "+s2.name);
if(s1.totalScore<s2.totalScore)
System.out.println(s1.name+"'s total score is lower than "+s2.name);
if(s1.totalScore==s2.totalScore)
System.out.println(s1.name+"'s total score is equal to "+s2.name);
}
void sum(){
totalScore=englishScore+mathScore+computerScore;
}
public double testScore(){
return(totalScore/3);
}
}
class StudentXW extends Student{
private String responsibility;
public void setResponsibility(String responsibility) {
this.responsibility = responsibility;
}
public String getResponsibility() {
return responsibility;
}
@Override
public double testScore() {
return super.testScore()+3;
}
}
class StudentBZ extends Student{
private String responsibility;
public void setResponsibility(String responsibility) {
this.responsibility = responsibility;
}
public String getResponsibility() {
return responsibility;
}
@Override
public double testScore() {
return super.testScore()+5;
}
}
}