(1)声明Student类。 属性包括:学号、姓名、英语成绩、数学成绩、计算机成绩和总成绩。 方法包括:构造方法、getter方法、setter方法、toString方法、equals方法、compare方法(比较两个学生的总成绩,结果分大于、小于、等于),sum方法(计算总成绩)和testScore的 方法(计算评测成绩)。 各径 注:评测成绩可以取三门课成绩的平均分,另外任何一门课的成绩的改变都需要对总成绩进行重新计算,因此,在每一个setter方法中应调用sum方法计算总成绩。 (2)声明StudentXW(学习委员)类为Student类的子类。 在StudentXW类中增加责任属性,并覆盖testScore方法(计算评测成绩,评测成绩=三门课的平均分+3)。 (3)声明StudentBZ(班长)类为Student类的子类。 在StudentBZ类中增加责任属性,并覆盖testScore方法(计算评测成绩,评测成绩=三门课的平均分+5)。 (4)声明测试类,生成若干个Student类、StudentXW类及StudentBZ类对象,并分别计算它们的评测成绩。编写一个JAVA程序
时间: 2024-02-23 11:03:19 浏览: 84
以下是代码实现:
```
public class Student implements Comparable<Student> {
private int id;
private String name;
private int englishScore;
private int mathScore;
private int computerScore;
private int totalScore;
public Student(int id, String name, int englishScore, int mathScore, int computerScore) {
this.id = id;
this.name = name;
this.englishScore = englishScore;
this.mathScore = mathScore;
this.computerScore = computerScore;
this.totalScore = sum();
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getEnglishScore() {
return englishScore;
}
public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
this.totalScore = sum();
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
this.totalScore = sum();
}
public int getComputerScore() {
return computerScore;
}
public void setComputerScore(int computerScore) {
this.computerScore = computerScore;
this.totalScore = sum();
}
public int getTotalScore() {
return totalScore;
}
private int sum() {
return englishScore + mathScore + computerScore;
}
private double testScore() {
return sum() / 3.0;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", englishScore=" + englishScore +
", mathScore=" + mathScore +
", computerScore=" + computerScore +
", totalScore=" + totalScore +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return id == student.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public int compareTo(Student o) {
return Integer.compare(o.totalScore, this.totalScore);
}
}
public class StudentXW extends Student {
private String responsibility;
public StudentXW(int id, String name, int englishScore, int mathScore, int computerScore, String responsibility) {
super(id, name, englishScore, mathScore, computerScore);
this.responsibility = responsibility;
}
@Override
public double testScore() {
return super.testScore() + 3.0;
}
}
public class StudentBZ extends Student {
private String responsibility;
public StudentBZ(int id, String name, int englishScore, int mathScore, int computerScore, String responsibility) {
super(id, name, englishScore, mathScore, computerScore);
this.responsibility = responsibility;
}
@Override
public double testScore() {
return super.testScore() + 5.0;
}
}
public class Test {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student(1, "张三", 80, 90, 70));
students.add(new StudentXW(2, "李四", 85, 95, 75, "学习委员"));
students.add(new StudentBZ(3, "王五", 90, 100, 80, "班长"));
for (Student student : students) {
System.out.println(student.getName() + "的评测成绩为:" + student.testScore());
}
Collections.sort(students);
System.out.println("总成绩排名:");
for (int i = 0; i < students.size(); i++) {
System.out.println("第" + (i + 1) + "名:" + students.get(i).getName() + ",总成绩为:" + students.get(i).getTotalScore());
}
}
}
```
在测试类中,我们创建了三个对象,分别为 `Student`、`StudentXW` 和 `StudentBZ`,并分别计算它们的评测成绩。最后,我们对学生进行按总成绩排序,并输出排名。
阅读全文