首先,我们需要创建一个学生类(Student),其中包括属性:考号(id)、姓名(name)、语文成绩(chinese)、外语成绩(english)、数学成绩(mathematics)、总分(totalScore)、类型(type)。其中,类型可以是普通文理生、文科艺术生、理科艺术生、文科体育生和理科体育生。然后,我们创建几个普通文理生类(GeneralArtStudent、GeneralScienceStudent)、艺术生类(ArtStudent)、体育生类(PhysicalEducationStudent),它们都是学生类的子类,因此可以继承学生类中的属性和方法。每种类别的学生类中可以再定义一些特有的属性(如专业考试成绩)和方法(如计算总分)。
最后,我们在主函数中创建一组学生对象,存储学生信息,根据不同的类别和排序规则,对学生对象进行排序,然后输出排序结果。下面是详细的程序实现:
```java
import java.util.*;
class Student implements Comparable<Student>{
protected int id; // 考号
protected String name; // 姓名
protected int chinese; // 语文成绩
protected int english; // 外语成绩
protected int mathematics; // 数学成绩
protected int totalScore; // 总分
protected String type; // 类型
// 构造方法
public Student(int id, String name, int chinese, int english, int mathematics, String type) {
this.id = id;
this.name = name;
this.chinese = chinese;
this.english = english;
this.mathematics = mathematics;
this.type = type;
this.totalScore = chinese + english + mathematics;
}
// 实现Comparable接口中的compareTo方法,用于排序
@Override
public int compareTo(Student o) {
if(this.totalScore != o.totalScore) {
return o.totalScore - this.totalScore;
} else {
return this.id - o.id;
}
}
// 重写toString方法,方便输出
@Override
public String toString() {
return id + "," + name + "," + chinese + "," + english + "," + mathematics + "," + totalScore + "," + type;
}
}
// 普通文科生
class GeneralArtStudent extends Student {
public GeneralArtStudent(int id, String name, int chinese, int english, int mathematics) {
super(id, name, chinese, english, mathematics, "普通文科生");
}
}
// 普通理科生
class GeneralScienceStudent extends Student {
public GeneralScienceStudent(int id, String name, int chinese, int english, int mathematics) {
super(id, name, chinese, english, mathematics, "普通理科生");
}
}
// 艺术生
class ArtStudent extends Student {
private int professionalScore; // 专业考试成绩
public ArtStudent(int id, String name, int chinese, int english, int mathematics, int professionalScore) {
super(id, name, chinese, english, mathematics, "艺术生");
this.professionalScore = professionalScore;
this.totalScore += professionalScore; // 总分包括专业考试成绩
}
// 重写compareTo方法,按照专业考试成绩排序
@Override
public int compareTo(Student o) {
if(this.totalScore != o.totalScore) {
return o.totalScore - this.totalScore;
} else {
if(o.type.equals("艺术生") && this.type.equals("艺术生")) {
ArtStudent artStudent1 = (ArtStudent)this;
ArtStudent artStudent2 = (ArtStudent)o;
if(artStudent1.professionalScore != artStudent2.professionalScore) {
return artStudent2.professionalScore - artStudent1.professionalScore;
} else {
return artStudent1.id - artStudent2.id;
}
} else { // 其他情况与父类一致
return super.compareTo(o);
}
}
}
// 重写toString方法,方便输出
@Override
public String toString() {
return super.toString() + "," + professionalScore;
}
}
// 体育生
class PhysicalEducationStudent extends Student {
private int professionalScore; // 专业考试成绩
public PhysicalEducationStudent(int id, String name, int chinese, int english, int mathematics, int professionalScore) {
super(id, name, chinese, english, mathematics, "体育生");
this.professionalScore = professionalScore;
this.totalScore += professionalScore; // 总分包括专业考试成绩
}
// 重写compareTo方法,按照专业考试成绩排序
@Override
public int compareTo(Student o) {
if(this.totalScore != o.totalScore) {
return o.totalScore - this.totalScore;
} else {
if(o.type.equals("体育生") && this.type.equals("体育生")) {
PhysicalEducationStudent peStudent1 = (PhysicalEducationStudent)this;
PhysicalEducationStudent peStudent2 = (PhysicalEducationStudent)o;
if(peStudent1.professionalScore != peStudent2.professionalScore) {
return peStudent2.professionalScore - peStudent1.professionalScore;
} else {
return peStudent1.id - peStudent2.id;
}
} else { // 其他情况与父类一致
return super.compareTo(o);
}
}
}
// 重写toString方法,方便输出
@Override
public String toString() {
return super.toString() + "," + professionalScore;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Student> studentList = new ArrayList<>();
// 输入普通文科生人数和信息
int n1 = sc.nextInt();
for(int i = 0; i < n1; i++) {
int id = sc.nextInt();
String name = sc.next();
int chinese = sc.nextInt();
int english = sc.nextInt();
int mathematics = sc.nextInt();
GeneralArtStudent student = new GeneralArtStudent(id, name, chinese, english, mathematics);
studentList.add(student);
}
// 输入普通理科生人数和信息
int n2 = sc.nextInt();
for(int i = 0; i < n2; i++) {
int id = sc.nextInt();
String name = sc.next();
int chinese = sc.nextInt();
int english = sc.nextInt();
int mathematics = sc.nextInt();
GeneralScienceStudent student = new GeneralScienceStudent(id, name, chinese, english, mathematics);
studentList.add(student);
}
// 输入艺术生人数和信息
int n3 = sc.nextInt();
for(int i = 0; i < n3; i++) {
int id = sc.nextInt();
String name = sc.next();
int chinese = sc.nextInt();
int english = sc.nextInt();
int mathematics = sc.nextInt();
int professionalScore = sc.nextInt();
ArtStudent student = new ArtStudent(id, name, chinese, english, mathematics, professionalScore);
studentList.add(student);
}
// 输入体育生人数和信息
int n4 = sc.nextInt();
for(int i = 0; i < n4; i++) {
int id = sc.nextInt();
String name = sc.next();
int chinese = sc.nextInt();
int english = sc.nextInt();
int mathematics = sc.nextInt();
int professionalScore = sc.nextInt();
PhysicalEducationStudent student = new PhysicalEducationStudent(id, name, chinese, english, mathematics, professionalScore);
studentList.add(student);
}
// 对学生信息进行排序
Collections.sort(studentList);
// 输出排序结果
for(Student student : studentList) {
System.out.println(student);
}
}
}
```