应用ArrayList实现某专业所有学生的成绩管理,要求构建学生(Student)类和成绩(AcademicRecord)类等,能够对不同年级的学生按总成绩排序并输出. Student(name,studID,major,grade, ArrayList< AcademicRecord > scores); AcademicRecord(studID,courseID,score) 要求学生类实现Comparable接口,排序按年级、成绩排; 并实现学生相同的判断(重写equals),当学生姓名和学号一样时,则学生相同。
时间: 2024-02-03 07:12:04 浏览: 104
设计一个学生类Student(学生学号、姓名、数学、英语、计算机成绩;)
5星 · 资源好评率100%
首先,我们需要定义两个类,分别是学生类Student和成绩类AcademicRecord。具体代码如下:
```java
public class Student implements Comparable<Student> {
private String name;
private String studID;
private String major;
private int grade;
private ArrayList<AcademicRecord> scores;
public Student(String name, String studID, String major, int grade, ArrayList<AcademicRecord> scores) {
this.name = name;
this.studID = studID;
this.major = major;
this.grade = grade;
this.scores = scores;
}
public String getName() {
return name;
}
public String getStudID() {
return studID;
}
public String getMajor() {
return major;
}
public int getGrade() {
return grade;
}
public ArrayList<AcademicRecord> getScores() {
return scores;
}
public void setName(String name) {
this.name = name;
}
public void setStudID(String studID) {
this.studID = studID;
}
public void setMajor(String major) {
this.major = major;
}
public void setGrade(int grade) {
this.grade = grade;
}
public void setScores(ArrayList<AcademicRecord> scores) {
this.scores = scores;
}
@Override
public int compareTo(Student o) {
if (this.grade != o.grade) {
return this.grade - o.grade;
} else {
int totalScore1 = getTotalScore(this.scores);
int totalScore2 = getTotalScore(o.scores);
return totalScore2 - totalScore1;
}
}
private int getTotalScore(ArrayList<AcademicRecord> scores) {
int total = 0;
for (AcademicRecord record : scores) {
total += record.getScore();
}
return total;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (obj instanceof Student) {
Student s = (Student) obj;
return this.name.equals(s.name) && this.studID.equals(s.studID);
}
return false;
}
}
public class AcademicRecord {
private String studID;
private String courseID;
private int score;
public AcademicRecord(String studID, String courseID, int score) {
this.studID = studID;
this.courseID = courseID;
this.score = score;
}
public String getStudID() {
return studID;
}
public String getCourseID() {
return courseID;
}
public int getScore() {
return score;
}
public void setStudID(String studID) {
this.studID = studID;
}
public void setCourseID(String courseID) {
this.courseID = courseID;
}
public void setScore(int score) {
this.score = score;
}
}
```
在学生类中,我们实现了Comparable接口,重写了compareTo方法,并且重写了equals方法,用于判断学生是否相同。在compareTo方法中,我们先按年级排序,如果年级相同,则按总成绩排序。
接下来,我们需要编写一个成绩管理类,用于对不同年级的学生按总成绩排序并输出。具体代码如下:
```java
import java.util.ArrayList;
import java.util.Collections;
public class ScoreManager {
private ArrayList<Student> students;
public ScoreManager() {
this.students = new ArrayList<>();
}
public void addStudent(Student student) {
this.students.add(student);
}
public void sortByTotalScore(int grade) {
ArrayList<Student> tmp = new ArrayList<>();
for (Student student : students) {
if (student.getGrade() == grade) {
tmp.add(student);
}
}
Collections.sort(tmp);
for (Student student : tmp) {
System.out.println(student.getName() + " " + student.getStudID() + " " + getTotalScore(student.getScores()));
}
}
private int getTotalScore(ArrayList<AcademicRecord> scores) {
int total = 0;
for (AcademicRecord record : scores) {
total += record.getScore();
}
return total;
}
}
```
在成绩管理类中,我们提供了一个方法sortByTotalScore,用于对某个年级的学生按总成绩排序并输出。具体实现是先将该年级的学生筛选出来,然后调用Collections.sort方法进行排序,最后输出学生的姓名、学号和总成绩。
最后,我们来演示一下如何使用这些类:
```java
public class Main {
public static void main(String[] args) {
ScoreManager manager = new ScoreManager();
ArrayList<AcademicRecord> scores1 = new ArrayList<>();
scores1.add(new AcademicRecord("001", "001", 90));
scores1.add(new AcademicRecord("001", "002", 80));
scores1.add(new AcademicRecord("001", "003", 70));
ArrayList<AcademicRecord> scores2 = new ArrayList<>();
scores2.add(new AcademicRecord("002", "001", 80));
scores2.add(new AcademicRecord("002", "002", 70));
scores2.add(new AcademicRecord("002", "003", 60));
ArrayList<AcademicRecord> scores3 = new ArrayList<>();
scores3.add(new AcademicRecord("003", "001", 70));
scores3.add(new AcademicRecord("003", "002", 60));
scores3.add(new AcademicRecord("003", "003", 50));
manager.addStudent(new Student("张三", "001", "计算机科学与技术", 1, scores1));
manager.addStudent(new Student("李四", "002", "计算机科学与技术", 2, scores2));
manager.addStudent(new Student("王五", "003", "计算机科学与技术", 3, scores3));
manager.sortByTotalScore(2);
}
}
```
在main方法中,我们先创建一个成绩管理类ScoreManager,并且创建三个学生对象,分别属于不同的年级。然后将三个学生对象添加到成绩管理类中,并且调用sortByTotalScore方法进行排序和输出。这里我们演示的是对第二年级的学生按总成绩进行排序和输出。
阅读全文