编写三个系别的学生类:英语系,计算机系,文学系(要求继承学生类)各系有以下成绩:(知识点:面向对象综合应用、数组中元素为引用类型) 英语系:演讲,期末考试,期中考试; 计算机系:操作能力,英语写作,期中考试,期末考试; 文学系: 演讲,作品,期末考试,期中考试;各系总分评测标准: 英语系: 演讲 50% 期末考试 25% 期中考试 25% 计算机系: 操作能力 40% 英语写作 20% 期末考试 20% 期中考试 20% 文学系: 演讲 35% 作品 35% 期末考试 15% 期中考试 15% 定义一个可容纳5个学生的学生类数组,使用随机数给该数组装入各系学生的对象,然后按如下格式输出数组中的信息: 序号:1,学号:XXXXX,姓名:XXX,性别:X,年龄:XX,综合成绩:XX 序号:2,学号:XXXXX,姓名:XXX,性别:X,年龄:XX,综合成绩:XX 序号:3,学号:XXXX,姓名:XXX,性别:X,年龄:XX,综合成绩:XX 序号:4,学号:XXXX,姓名:XXX,性别:X,年龄:XX,综合成绩:XX 序号:5,学号:XXXXX,姓名:XXX,性别:X,年龄:XX,综合成绩:XX
时间: 2023-10-23 22:12:44 浏览: 114
下面是一个示例代码实现:
```java
import java.util.Random;
class Student {
private String id; // 学号
private String name; // 姓名
private char gender; // 性别
private int age; // 年龄
private double score; // 成绩
public Student(String id, String name, char gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public char getGender() {
return gender;
}
public int getAge() {
return age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
class EnglishStudent extends Student {
private double speech; // 演讲成绩
private double finalExam; // 期末考试成绩
private double midtermExam; // 期中考试成绩
public EnglishStudent(String id, String name, char gender, int age) {
super(id, name, gender, age);
Random rand = new Random();
this.speech = rand.nextDouble() * 100;
this.finalExam = rand.nextDouble() * 100;
this.midtermExam = rand.nextDouble() * 100;
}
public double getSpeech() {
return speech;
}
public double getFinalExam() {
return finalExam;
}
public double getMidtermExam() {
return midtermExam;
}
@Override
public void setScore(double score) {
this.score = 0.5 * speech + 0.25 * finalExam + 0.25 * midtermExam;
}
}
class ComputerStudent extends Student {
private double ability; // 操作能力
private double writing; // 英语写作
private double finalExam; // 期末考试成绩
private double midtermExam; // 期中考试成绩
public ComputerStudent(String id, String name, char gender, int age) {
super(id, name, gender, age);
Random rand = new Random();
this.ability = rand.nextDouble() * 100;
this.writing = rand.nextDouble() * 100;
this.finalExam = rand.nextDouble() * 100;
this.midtermExam = rand.nextDouble() * 100;
}
public double getAbility() {
return ability;
}
public double getWriting() {
return writing;
}
public double getFinalExam() {
return finalExam;
}
public double getMidtermExam() {
return midtermExam;
}
@Override
public void setScore(double score) {
this.score = 0.4 * ability + 0.2 * writing + 0.2 * finalExam + 0.2 * midtermExam;
}
}
class LiteratureStudent extends Student {
private double speech; // 演讲成绩
private double essay; // 作品成绩
private double finalExam; // 期末考试成绩
private double midtermExam; // 期中考试成绩
public LiteratureStudent(String id, String name, char gender, int age) {
super(id, name, gender, age);
Random rand = new Random();
this.speech = rand.nextDouble() * 100;
this.essay = rand.nextDouble() * 100;
this.finalExam = rand.nextDouble() * 100;
this.midtermExam = rand.nextDouble() * 100;
}
public double getSpeech() {
return speech;
}
public double getEssay() {
return essay;
}
public double getFinalExam() {
return finalExam;
}
public double getMidtermExam() {
return midtermExam;
}
@Override
public void setScore(double score) {
this.score = 0.35 * speech + 0.35 * essay + 0.15 * finalExam + 0.15 * midtermExam;
}
}
public class Main {
public static void main(String[] args) {
Student[] students = new Student[5];
students[0] = new EnglishStudent("20210001", "张三", 'M', 20);
students[1] = new EnglishStudent("20210002", "李四", 'F', 19);
students[2] = new ComputerStudent("20210003", "王五", 'M', 21);
students[3] = new ComputerStudent("20210004", "赵六", 'F', 20);
students[4] = new LiteratureStudent("20210005", "钱七", 'M', 22);
// 计算综合成绩
for (int i = 0; i < students.length; i++) {
students[i].setScore(0);
}
// 排序
for (int i = 0; i < students.length - 1; i++) {
for (int j = i + 1; j < students.length; j++) {
if (students[i].getScore() < students[j].getScore()) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
// 输出结果
for (int i = 0; i < students.length; i++) {
System.out.println("序号:" + (i + 1) + ",学号:" + students[i].getId() + ",姓名:" + students[i].getName()
+ ",性别:" + students[i].getGender() + ",年龄:" + students[i].getAge() + ",综合成绩:"
+ students[i].getScore());
}
}
}
```
这个代码中,我们定义了一个`Student`类作为基类,然后分别定义了`EnglishStudent`、`ComputerStudent`和`LiteratureStudent`类继承自`Student`类,并分别添加了各自的成绩属性和计算综合成绩的方法。在`Main`类中,我们创建了一个长度为5的`Student`数组,使用随机数给该数组装入各系学生的对象,并计算出各个学生的综合成绩,最后按照综合成绩从高到低排序并输出结果。
阅读全文