(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类对象,并分别计算它们的评测成绩。
时间: 2024-02-23 11:03:14 浏览: 113
设计一个学生类Student(学生学号、姓名、数学、英语、计算机成绩;)
5星 · 资源好评率100%
好的,以下是你要的代码。
Student类:
```java
public class Student implements Comparable<Student> {
private String id;
private String name;
private double englishScore;
private double mathScore;
private double computerScore;
private double totalScore;
public Student(String id, String name, double englishScore, double mathScore, double computerScore) {
this.id = id;
this.name = name;
this.englishScore = englishScore;
this.mathScore = mathScore;
this.computerScore = computerScore;
this.totalScore = sum();
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
this.totalScore = sum();
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
this.totalScore = sum();
}
public double getComputerScore() {
return computerScore;
}
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
this.totalScore = sum();
}
public double getTotalScore() {
return totalScore;
}
private double sum() {
return englishScore + mathScore + computerScore;
}
public double testScore() {
return sum() / 3;
}
@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 Objects.equals(id, student.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public int compareTo(Student o) {
return Double.compare(o.getTotalScore(), this.getTotalScore());
}
}
```
StudentXW类:
```java
public class StudentXW extends Student {
private String responsibility;
public StudentXW(String id, String name, double englishScore, double mathScore, double computerScore, String responsibility) {
super(id, name, englishScore, mathScore, computerScore);
this.responsibility = responsibility;
}
public String getResponsibility() {
return responsibility;
}
public void setResponsibility(String responsibility) {
this.responsibility = responsibility;
}
@Override
public double testScore() {
return super.testScore() + 3;
}
}
```
StudentBZ类:
```java
public class StudentBZ extends Student {
private String responsibility;
public StudentBZ(String id, String name, double englishScore, double mathScore, double computerScore, String responsibility) {
super(id, name, englishScore, mathScore, computerScore);
this.responsibility = responsibility;
}
public String getResponsibility() {
return responsibility;
}
public void setResponsibility(String responsibility) {
this.responsibility = responsibility;
}
@Override
public double testScore() {
return super.testScore() + 5;
}
}
```
测试类:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("001", "张三", 90, 80, 70));
students.add(new Student("002", "李四", 80, 90, 70));
students.add(new Student("003", "王五", 70, 80, 90));
List<StudentXW> studentXWS = new ArrayList<>();
studentXWS.add(new StudentXW("004", "赵六", 80, 70, 90, "学习委员"));
studentXWS.add(new StudentXW("005", "钱七", 70, 90, 80, "学习委员"));
List<StudentBZ> studentBZS = new ArrayList<>();
studentBZS.add(new StudentBZ("006", "孙八", 90, 80, 70, "班长"));
studentBZS.add(new StudentBZ("007", "周九", 80, 90, 70, "班长"));
for (Student student : students) {
System.out.println(student.getName() + "的评测成绩为:" + student.testScore());
}
for (StudentXW studentXW : studentXWS) {
System.out.println(studentXW.getName() + "的评测成绩为:" + studentXW.testScore());
}
for (StudentBZ studentBZ : studentBZS) {
System.out.println(studentBZ.getName() + "的评测成绩为:" + studentBZ.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());
}
}
}
```
阅读全文