高考生主要分为文科生与理科生,文科生的考试科目为:语文、外语、数学和文综,理科生的考试科目为:语文、外语、数学和理综。但是参加高考考试的还有一部分考生称为艺体生,他们除了参加文化课考试外还要参加专业考试。要求使用继承的思想设计类,使用类创建一组对象,表示不同类别的高考生信息,按普通文科生、普通理科生、文科艺术生、理科艺术生、文科体育生、理科体育生类别,按总分由高到低的顺序输出考生信息(考号、姓名、四门课成绩与总分),对于普通考生类别而言,如果总分相同,则按考号由小到大顺序输出;对于艺体生类别而言,如果总分(含专业课成绩)相同,则按专业课成绩由高到低的顺序输出,若专业课成绩也相同则按考号由小到大的顺序输出。 要求使用类的继承完成程序设计。 输入:普通文理生人数,普通文理生信息,艺体生人数,艺体生信息(普通文理生文理分类要求:1-文,2-理;艺体生先有文理分类,再有艺术、体育分类:1-艺术,2-体育) 输出:排序后的考生信息包括:学号,姓名,各科分数,总分,类别。利用Java语言编写程序完整版
时间: 2023-05-26 12:02:14 浏览: 355
编写程序,输入并保存5名学生的信息,计算出平均分,再按照成绩从高到低的顺序输出他们的信息 学生信息包括学号、姓名、成绩
首先,我们需要创建一个学生类(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);
}
}
}
```
阅读全文