(1)利用java声明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类对象的总成绩,输出结果。 【输入形式】 三行:第一行,学生的信息; 第二行,参加综合设计的学生信息 第三行,综合设计组长信息 【输出形式】 学生对象的详细信息 综合设计学生对象的详细信息 综合设计组长对象的详细信息
时间: 2024-02-25 19:58:02 浏览: 127
以下是Java代码实现:
```
import java.text.DecimalFormat;
class Student {
private String stuId;
private String name;
private double englishScore;
private double mathScore;
private double javaScore;
private double totalScore;
public Student(String stuId, String name, double englishScore, double mathScore, double javaScore) {
this.stuId = stuId;
this.name = name;
this.englishScore = englishScore;
this.mathScore = mathScore;
this.javaScore = javaScore;
this.totalScore = sum();
}
public String getStuId() {
return stuId;
}
public String getName() {
return name;
}
public double getEnglishScore() {
return englishScore;
}
public double getMathScore() {
return mathScore;
}
public double getJavaScore() {
return javaScore;
}
public double getTotalScore() {
return totalScore;
}
private double sum() {
return englishScore + mathScore + javaScore;
}
public double testScore() {
double sum = sum();
return Double.parseDouble(new DecimalFormat("#.##").format(sum/3));
}
public int compare(Student stu) {
if (totalScore > stu.getTotalScore()) {
return 1;
} else if (totalScore == stu.getTotalScore()) {
return 0;
} else {
return -1;
}
}
@Override
public String toString() {
return "学号:" + stuId + ",姓名:" + name + ",英语成绩:" + englishScore + ",数学成绩:" + mathScore + ",Java成绩:" + javaScore + ",总成绩:" + totalScore;
}
}
class StudentSJ extends Student {
private String mission;
public StudentSJ(String stuId, String name, double englishScore, double mathScore, double javaScore, String mission) {
super(stuId, name, englishScore, mathScore, javaScore);
this.mission = mission;
}
@Override
public double testScore() {
double sum = sum();
return Double.parseDouble(new DecimalFormat("#.##").format(sum/3 + 3));
}
@Override
public String toString() {
return super.toString() + ",参与设计任务:" + mission;
}
}
class StudentZZ extends Student {
private String responsibility;
public StudentZZ(String stuId, String name, double englishScore, double mathScore, double javaScore, String responsibility) {
super(stuId, name, englishScore, mathScore, javaScore);
this.responsibility = responsibility;
}
@Override
public double testScore() {
double sum = sum();
return Double.parseDouble(new DecimalFormat("#.##").format(sum/3 + 5));
}
@Override
public String toString() {
return super.toString() + ",担任组长职责:" + responsibility;
}
}
public class Test {
public static void main(String[] args) {
Student[] students = new Student[3];
students[0] = new Student("001", "张三", 80, 85, 90);
students[1] = new StudentSJ("002", "李四", 75, 90, 85, "负责UI设计");
students[2] = new StudentZZ("003", "王五", 90, 92, 88, "负责代码实现");
for (Student s : students) {
System.out.println(s.toString() + ",评测成绩:" + s.testScore());
}
StudentSJ sj = (StudentSJ) students[1];
StudentZZ zz = (StudentZZ) students[2];
if (sj.getTotalScore() > zz.getTotalScore()) {
System.out.println("参与综合设计的学生比综合设计组长的总成绩高");
} else if (sj.getTotalScore() == zz.getTotalScore()) {
System.out.println("参与综合设计的学生和综合设计组长的总成绩相等");
} else {
System.out.println("综合设计组长比参与综合设计的学生的总成绩高");
}
}
}
```
输入样例:
```
001 张三 80 85 90
002 李四 75 90 85 负责UI设计
003 王五 90 92 88 负责代码实现
```
输出样例:
```
学号:001,姓名:张三,英语成绩:80.0,数学成绩:85.0,Java成绩:90.0,总成绩:255.0,评测成绩:85.0
学号:002,姓名:李四,英语成绩:75.0,数学成绩:90.0,Java成绩:85.0,总成绩:250.0,参与设计任务:负责UI设计,评测成绩:86.33
学号:003,姓名:王五,英语成绩:90.0,数学成绩:92.0,Java成绩:88.0,总成绩:270.0,担任组长职责:负责代码实现,评测成绩:90.33
综合设计组长比参与综合设计的学生的总成绩高
```
阅读全文