高考生主要分为文科生与理科生,文科生的考试科目为:语文、外语、数学和文综,理科生的考试科目为:语文、外语、数学和理综。但是参加高考考试的还有一部分考生称为艺体生,他们除了参加文化课考试外还要参加专业考试。要求使用继承的思想设计类,使用类创建一组对象,表示不同类别的高考生信息,按普通文科生、普通理科生、文科艺术生、理科艺术生、文科体育生、理科体育生类别,按总分由高到低的顺序输出考生信息(考号、姓名、四门课成绩与总分),对于普通考生类别而言,如果总分相同,则按考号由小到大顺序输出;对于艺体生类别而言,如果总分(含专业课成绩)相同,则按专业课成绩由高到低的顺序输出,若专业课成绩也相同则按考号由小到大的顺序输出。 要求使用类的继承完成程序设计。 输入:普通文理生人数,普通文理生信息,艺体生人数,艺体生信息(普通文理生文理分类要求:1-文,2-理;艺体生先有文理分类,再有艺术、体育分类:1-艺术,2-体育) 输出:排序后的考生信息包括:学号,姓名,各科分数,总分,类别 用Java语言编写程序
时间: 2023-05-23 17:07:00 浏览: 64
class Student {
private String stuNum;
private String stuName;
private int chineseScore;
private int mathScore;
private int foreignScore;
private int totalScore;
private String category;
public Student(String stuNum, String stuName, int chineseScore, int mathScore, int foreignScore) {
this.stuNum = stuNum;
this.stuName = stuName;
this.chineseScore = chineseScore;
this.mathScore = mathScore;
this.foreignScore = foreignScore;
this.totalScore = chineseScore + mathScore + foreignScore;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getStuNum() {
return stuNum;
}
public String getStuName() {
return stuName;
}
public int getChineseScore() {
return chineseScore;
}
public int getMathScore() {
return mathScore;
}
public int getForeignScore() {
return foreignScore;
}
public int getTotalScore() {
return totalScore;
}
}
class ArtStudent extends Student {
private int artScore;
public ArtStudent(String stuNum, String stuName, int chineseScore, int mathScore, int foreignScore, int artScore) {
super(stuNum, stuName, chineseScore, mathScore, foreignScore);
this.artScore = artScore;
this.setCategory("Art");
this.setTotalScore(chineseScore + mathScore + foreignScore + artScore);
}
public int getArtScore() {
return artScore;
}
public void setArtScore(int artScore) {
this.artScore = artScore;
}
}
class SportStudent extends Student {
private int sportScore;
public SportStudent(String stuNum, String stuName, int chineseScore, int mathScore, int foreignScore, int sportScore) {
super(stuNum, stuName, chineseScore, mathScore, foreignScore);
this.sportScore = sportScore;
this.setCategory("Sport");
this.setTotalScore(chineseScore + mathScore + foreignScore + sportScore);
}
public int getSportScore() {
return sportScore;
}
public void setSportScore(int sportScore) {
this.sportScore = sportScore;
}
}
class ArtScienceStudent extends Student {
private int artScienceScore;
public ArtScienceStudent(String stuNum, String stuName, int chineseScore, int mathScore, int foreignScore, int artScienceScore) {
super(stuNum, stuName, chineseScore, mathScore, foreignScore);
this.artScienceScore = artScienceScore;
this.setCategory("ArtScience");
this.setTotalScore(chineseScore + mathScore + foreignScore + artScienceScore);
}
public int getArtScienceScore() {
return artScienceScore;
}
public void setArtScienceScore(int artScienceScore) {
this.artScienceScore = artScienceScore;
}
}
class ScienceStudent extends Student {
public ScienceStudent(String stuNum, String stuName, int chineseScore, int mathScore, int foreignScore) {
super(stuNum, stuName, chineseScore, mathScore, foreignScore);
this.setCategory("Science");
this.setTotalScore(chineseScore + mathScore + foreignScore);
}
}
class LiteratureStudent extends Student {
public LiteratureStudent(String stuNum, String stuName, int chineseScore, int mathScore, int foreignScore) {
super(stuNum, stuName, chineseScore, mathScore, foreignScore);
this.setCategory("Literature");
this.setTotalScore(chineseScore + mathScore + foreignScore);
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int literatureCount = scanner.nextInt();
int scienceCount = scanner.nextInt();
int artCount = scanner.nextInt();
int sportCount = scanner.nextInt();
List<Student> studentList = new ArrayList<>();
// input literature student info
for (int i = 0; i < literatureCount; i++) {
String stuNum = scanner.next();
String stuName = scanner.next();
int chineseScore = scanner.nextInt();
int mathScore = scanner.nextInt();
int foreignScore = scanner.nextInt();
LiteratureStudent student = new LiteratureStudent(stuNum, stuName, chineseScore, mathScore, foreignScore);
studentList.add(student);
}
// input science student info
for (int i = 0; i < scienceCount; i++) {
String stuNum = scanner.next();
String stuName = scanner.next();
int chineseScore = scanner.nextInt();
int mathScore = scanner.nextInt();
int foreignScore = scanner.nextInt();
ScienceStudent student = new ScienceStudent(stuNum, stuName, chineseScore, mathScore, foreignScore);
studentList.add(student);
}
// input art student info
for (int i = 0; i < artCount; i++) {
String stuNum = scanner.next();
String stuName = scanner.next();
int chineseScore = scanner.nextInt();
int mathScore = scanner.nextInt();
int foreignScore = scanner.nextInt();
int artScore = scanner.nextInt();
ArtStudent student = new ArtStudent(stuNum, stuName, chineseScore, mathScore, foreignScore, artScore);
studentList.add(student);
}
// input sport student info
for (int i = 0; i < sportCount; i++) {
String stuNum = scanner.next();
String stuName = scanner.next();
int chineseScore = scanner.nextInt();
int mathScore = scanner.nextInt();
int foreignScore = scanner.nextInt();
int sportScore = scanner.nextInt();
SportStudent student = new SportStudent(stuNum, stuName, chineseScore, mathScore, foreignScore, sportScore);
studentList.add(student);
}
// sort student list
Collections.sort(studentList, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if (o1.getTotalScore() != o2.getTotalScore()) {
return o2.getTotalScore() - o1.getTotalScore();
} else {
if (o1 instanceof ArtStudent && o2 instanceof ArtStudent) {
ArtStudent a1 = (ArtStudent) o1;
ArtStudent a2 = (ArtStudent) o2;
if (a1.getArtScore() != a2.getArtScore()) {
return a2.getArtScore() - a1.getArtScore();
} else {
return a1.getStuNum().compareTo(a2.getStuNum());
}
}
if (o1 instanceof SportStudent && o2 instanceof SportStudent) {
SportStudent s1 = (SportStudent) o1;
SportStudent s2 = (SportStudent) o2;
if (s1.getSportScore() != s2.getSportScore()) {
return s2.getSportScore() - s1.getSportScore();
} else {
return s1.getStuNum().compareTo(s2.getStuNum());
}
}
return o1.getStuNum().compareTo(o2.getStuNum());
}
}
});
// output sorted student info
for (Student student : studentList) {
System.out.println(student.getStuNum() + " " + student.getStuName() + " " + student.getChineseScore()
+ " " + student.getMathScore() + " " + student.getForeignScore() + " " + student.getTotalScore() + " "
+ student.getCategory());
}
}
}
阅读全文