高考生主要分为文科生与理科生,文科生的考试科目为:语文、外语、数学和文综,理科生的考试科目为:语文、外语、数学和理综。但是参加高考考试的还有一部分考生称为艺体生,他们除了参加文化课考试外还要参加专业考试。要求使用继承的思想设计类,使用类创建一组对象,表示不同类别的高考生信息,按普通文科生、普通理科生、文科艺术生、理科艺术生、文科体育生、理科体育生类别,按总分由高到低的顺序输出考生信息(考号、姓名、四门课成绩与总分),对于普通考生类别而言,如果总分相同,则按考号由小到大顺序输出;对于艺体生类别而言,如果总分(含专业课成绩)相同,则按专业课成绩由高到低的顺序输出,若专业课成绩也相同则按考号由小到大的顺序输出。用Java语言编写
时间: 2023-05-20 22:05:26 浏览: 232
以下是使用Java语言编写的代码:
```
import java.util.*;
class ExamStudent {
String id;
String name;
int chinese;
int math;
int foreignLanguage;
int totalScore;
ExamStudent(String id, String name, int chinese, int math, int foreignLanguage) {
this.id = id;
this.name = name;
this.chinese = chinese;
this.math = math;
this.foreignLanguage = foreignLanguage;
this.totalScore = chinese + math + foreignLanguage;
}
public String toString() {
return id + " " + name + " " + chinese + " " + math + " " + foreignLanguage + " " + totalScore;
}
}
class ArtExamStudent extends ExamStudent {
int artScore;
ArtExamStudent(String id, String name, int chinese, int math, int foreignLanguage, int artScore) {
super(id, name, chinese, math, foreignLanguage);
this.artScore = artScore;
this.totalScore += artScore;
}
public String toString() {
return super.toString() + " " + artScore;
}
}
class SportsExamStudent extends ExamStudent {
int sportsScore;
SportsExamStudent(String id, String name, int chinese, int math, int foreignLanguage, int sportsScore) {
super(id, name, chinese, math, foreignLanguage);
this.sportsScore = sportsScore;
this.totalScore += sportsScore;
}
public String toString() {
return super.toString() + " " + sportsScore;
}
}
class ArtsExamStudentComparator implements Comparator<ArtExamStudent> {
public int compare(ArtExamStudent s1, ArtExamStudent s2) {
if (s1.totalScore != s2.totalScore) {
return s2.totalScore - s1.totalScore;
} else if (s1.artScore != s2.artScore) {
return s2.artScore - s1.artScore;
} else {
return s1.id.compareTo(s2.id);
}
}
}
class SportsExamStudentComparator implements Comparator<SportsExamStudent> {
public int compare(SportsExamStudent s1, SportsExamStudent s2) {
if (s1.totalScore != s2.totalScore) {
return s2.totalScore - s1.totalScore;
} else if (s1.sportsScore != s2.sportsScore) {
return s2.sportsScore - s1.sportsScore;
} else {
return s1.id.compareTo(s2.id);
}
}
}
public class ExamStudentTest {
public static void main(String[] args) {
List<ExamStudent> students = new ArrayList<>();
students.add(new ExamStudent("1001", "张三", 90, 80, 70));
students.add(new ExamStudent("1002", "李四", 80, 90, 70));
students.add(new ExamStudent("1003", "王五", 70, 80, 90));
students.add(new ArtExamStudent("2001", "赵六", 90, 80, 70, 85));
students.add(new ArtExamStudent("2002", "钱七", 80, 90, 70, 90));
students.add(new ArtExamStudent("2003", "孙八", 70, 80, 90, 80));
students.add(new SportsExamStudent("3001", "周九", 90, 80, 70, 95));
students.add(new SportsExamStudent("3002", "吴十", 80, 90, 70, 90));
students.add(new SportsExamStudent("3003", "郑十一", 70, 80, 90, 85));
Collections.sort(students, new Comparator<ExamStudent>() {
public int compare(ExamStudent s1, ExamStudent s2) {
if (s1.totalScore != s2.totalScore) {
return s2.totalScore - s1.totalScore;
} else {
return s1.id.compareTo(s2.id);
}
}
});
for (ExamStudent student : students) {
System.out.println(student);
}
}
}
```
在这个程序中,我们定义了一个 ExamStudent 类来表示普通的高考生,还定义了 ArtExamStudent 和 SportsExamStudent 类来表示艺术生和体育生。这些类都继承自 ExamStudent 类,因为它们都有一些共同的属性和方法。
我们还定义了两个比较器类 ArtsExamStudentComparator 和 SportsExamStudentComparator,用于比较艺术生和体育生的总分和专业课成绩。这些比较器类实现了 Comparator 接口,可以用于排序。
在 main 函数中,我们创建了一组不同类别的高考生对象,并将它们存储在一个 List 中。然后,我们使用 Collections.sort 方法对这个 List 进行排序,按照总分由高到低的顺序输出考生信息。对于艺术生和体育生,我们使用自定义的比较器类来排序,以满足题目要求。
最后,我们遍历排序后的 List,输出每个考生的信息。
阅读全文