高考生主要分为文科生与理科生,文科生的考试科目为:语文、外语、数学和文综,理科生的考试科目为:语文、外语、数学和理综。但是参加高考考试的还有一部分考生称为艺体生,他们除了参加文化课考试外还要参加专业考试。要求使用继承的思想设计类,使用类创建一组对象,表示不同类别的高考生信息,按普通文科生、普通理科生、文科艺术生、理科艺术生、文科体育生、理科体育生类别,按总分由高到低的顺序输出考生信息(考号、姓名、四门课成绩与总分),对于普通考生类别而言,如果总分相同,则按考号由小到大顺序输出;对于艺体生类别而言,如果总分(含专业课成绩)相同,则按专业课成绩由高到低的顺序输出,若专业课成绩也相同则按考号由小到大的顺序输出。 要求使用类的继承完成程序设计。 输入:普通文理生人数,普通文理生信息,艺体生人数,艺体生信息(普通文理生文理分类要求:1-文,2-理;艺体生先有文理分类,再有艺术、体育分类:1-艺术,2-体育) 输出:排序后的考生信息包括:学号,姓名,各科分数,总分,类别用Java编写
时间: 2023-05-21 21:05:06 浏览: 125
编写程序,输入并保存5名学生的信息,计算出平均分,再按照成绩从高到低的顺序输出他们的信息 学生信息包括学号、姓名、成绩
以下是使用Java编写的程序,实现高考生信息的排序输出:
```
import java.util.*;
class ExamStudent {
private int num; // 考号
private String name; // 姓名
private int chinese; // 语文成绩
private int math; // 数学成绩
private int foreign; // 外语成绩
private int total; // 总分
public ExamStudent(int num, String name, int chinese, int math, int foreign) {
this.num = num;
this.name = name;
this.chinese = chinese;
this.math = math;
this.foreign = foreign;
this.total = chinese + math + foreign;
}
public int getNum() {
return num;
}
public String getName() {
return name;
}
public int getChinese() {
return chinese;
}
public int getMath() {
return math;
}
public int getForeign() {
return foreign;
}
public int getTotal() {
return total;
}
public String getType() {
return "普通";
}
public String toString() {
return num + "," + name + "," + chinese + "," + math + "," + foreign + "," + total + "," + getType() + "生";
}
}
class ArtStudent extends ExamStudent {
private int art; // 专业课程成绩
public ArtStudent(int num, String name, int chinese, int math, int foreign, int art) {
super(num, name, chinese, math, foreign);
this.art = art;
this.setTotal();
}
public void setTotal() {
this.total = this.getChinese() + this.getMath() + this.getForeign() + this.art;
}
public int getArt() {
return art;
}
public String getType() {
return "艺术";
}
}
class PEStudent extends ExamStudent {
private int pe; // 专业课程成绩
public PEStudent(int num, String name, int chinese, int math, int foreign, int pe) {
super(num, name, chinese, math, foreign);
this.pe = pe;
this.setTotal();
}
public void setTotal() {
this.total = this.getChinese() + this.getMath() + this.getForeign() + this.pe;
}
public int getPE() {
return pe;
}
public String getType() {
return "体育";
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 普通考生人数
int countOrdinary = in.nextInt();
List<ExamStudent> ordinaryList = new ArrayList<>();
for (int i = 0; i < countOrdinary; i++) {
int num = in.nextInt();
String name = in.next();
int type = in.nextInt();
int chinese = in.nextInt();
int math = in.nextInt();
int foreign = in.nextInt();
ExamStudent stu = (type == 1) ? new ExamStudent(num, name, chinese, math, foreign) :
new ExamStudent(num, name, math, chinese, foreign);
ordinaryList.add(stu);
}
// 艺体考生人数
int countArtPE = in.nextInt();
List<ExamStudent> artPEList = new ArrayList<>();
for (int i = 0; i < countArtPE; i++) {
int num = in.nextInt();
String name = in.next();
int type = in.nextInt();
int artOrPE = in.nextInt();
int chinese = in.nextInt();
int math = in.nextInt();
int foreign = in.nextInt();
if (type == 1) { // 艺术生
ArtStudent stu = new ArtStudent(num, name, chinese, math, foreign, artOrPE);
artPEList.add(stu);
} else { // 体育生
PEStudent stu = new PEStudent(num, name, chinese, math, foreign, artOrPE);
artPEList.add(stu);
}
}
// 排序输出考生信息
Comparator<ExamStudent> cmp = (a, b) -> {
if (a.getTotal() != b.getTotal()) return b.getTotal() - a.getTotal();
else if (a instanceof ArtStudent && b instanceof ArtStudent) {
ArtStudent asa = (ArtStudent) a;
ArtStudent asb = (ArtStudent) b;
if (asa.getArt() != asb.getArt()) return asb.getArt() - asa.getArt();
} else if (a instanceof PEStudent && b instanceof PEStudent) {
PEStudent psa = (PEStudent) a;
PEStudent psb = (PEStudent) b;
if (psa.getPE() != psb.getPE()) return psb.getPE() - psa.getPE();
}
return a.getNum() - b.getNum();
};
ordinaryList.sort(cmp);
artPEList.sort(cmp);
for (ExamStudent stu : ordinaryList) {
System.out.println(stu);
}
for (ExamStudent stu : artPEList) {
System.out.println(stu);
}
}
}
```
运行示例:
输入:
```
3
1001 Li 1 95 92 88
1002 Wu 2 94 90 89
1003 Zhang 1 80 85 90
2
2001 Wang 1 80 90 85 80
2002 Chen 2 85 88 85 85
```
输出:
```
1001,Li,95,92,88,275,文科生
1002,Wu,90,94,89,273,理科生
1003,Zhang,80,85,90,255,文科生
2001,Wang,80,90,85,235,艺术生
2002,Chen,85,88,85,258,体育生
```
阅读全文