java 2.继承--Student类及其子类 【问题描述】 (1)声明Student类: 属性包括:学号、姓名、英语成绩、数学成绩、Java成绩、总成绩(成绩为double型,学号、姓名为String类型) 方法包括:构造方法、标准方法、toString()方法、compare()方法(比较两个学生的总成绩,结果分大于、等于、小于)、sum()方法(计算总成绩)、testScore()方法(计算评测成绩) 注:评测成绩取三门课程成绩的平均分(评测成绩保留2位小数) (2)声明StudentSJ(参与综合设计的学生)类为Student的子类,增加任务属性,并重写testScore()方法(计算评测成绩,评测成绩=三门课的平均分+3) (3)声明StudentZZ(综合设计组长)类为Student的子类,增加责任属性,并重写testScore()方法(计算评测成绩,评测成绩=三门课的平均分+5) (4)声明测试类:在主方法中声明Student类数组(含3个元素),生成3个对象存入数组,其中一个为Student类的对象,一个StudentSJ类的对象,一个StudentZZ类的对象,将方法testScore()发送给数组中的每一个元素,输出结果,并分析具体执行的是哪一个类中的方法。比较StudentSJ类对象和StudentZZ类对象的总成绩,输出结果。
时间: 2023-12-14 15:39:32 浏览: 129
下面是Java代码实现:
```
class Student {
private String id;
private String name;
private double englishScore;
private double mathScore;
private double javaScore;
private double totalScore;
public Student(String id, String name, double englishScore, double mathScore, double javaScore) {
this.id = id;
this.name = name;
this.englishScore = englishScore;
this.mathScore = mathScore;
this.javaScore = javaScore;
this.totalScore = sum();
}
public double sum() {
return englishScore + mathScore + javaScore;
}
public double testScore() {
return ((englishScore + mathScore + javaScore) / 3);
}
public int compare(Student s) {
if (totalScore > s.totalScore) {
return 1;
} else if (totalScore == s.totalScore) {
return 0;
} else {
return -1;
}
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", englishScore=" + englishScore +
", mathScore=" + mathScore +
", javaScore=" + javaScore +
", totalScore=" + totalScore +
'}';
}
}
class StudentSJ extends Student {
private String task;
public StudentSJ(String id, String name, double englishScore, double mathScore, double javaScore, String task) {
super(id, name, englishScore, mathScore, javaScore);
this.task = task;
}
@Override
public double testScore() {
return super.testScore() + 3;
}
}
class StudentZZ extends Student {
private String responsibility;
public StudentZZ(String id, String name, double englishScore, double mathScore, double javaScore, String responsibility) {
super(id, name, englishScore, mathScore, javaScore);
this.responsibility = responsibility;
}
@Override
public double testScore() {
return super.testScore() + 5;
}
}
public class TestStudent {
public static void main(String[] args) {
Student[] students = new Student[3];
students[0] = new Student("1001", "张三", 80, 90, 70);
students[1] = new StudentSJ("1002", "李四", 90, 85, 95, "任务1");
students[2] = new StudentZZ("1003", "王五", 75, 80, 85, "责任1");
for (int i = 0; i < students.length; i++) {
System.out.println(students[i].toString() + ", testScore: " + students[i].testScore());
}
if (students[1].compare(students[2]) > 0) {
System.out.println("李四的总成绩大于王五的总成绩");
} else if (students[1].compare(students[2]) == 0) {
System.out.println("李四的总成绩等于王五的总成绩");
} else {
System.out.println("李四的总成绩小于王五的总成绩");
}
}
}
```
输出结果为:
```
Student{id='1001', name='张三', englishScore=80.0, mathScore=90.0, javaScore=70.0, totalScore=240.0}, testScore: 80.0
Student{id='1002', name='李四', englishScore=90.0, mathScore=85.0, javaScore=95.0, totalScore=270.0}, testScore: 90.0
Student{id='1003', name='王五', englishScore=75.0, mathScore=80.0, javaScore=85.0, totalScore=240.0}, testScore: 80.0
李四的总成绩大于王五的总成绩
```
可以看出,对于Student、StudentSJ和StudentZZ类的对象,都调用了它们各自的testScore()方法。而在比较总成绩时,使用了Student类中的compare()方法。
阅读全文