应用ArrayList实现某专业所有学生的成绩管理,要求构建学生(Student)类和成绩(AcademicRecord)类等,能够对不同年级的学生按总成绩排序并输出. Student(name,studID,major,grade, ArrayList< AcademicRecord > scores); AcademicRecord(studID,courseID,score) 要求学生类实现Comparable接口,排序按年级、成绩排; 并实现学生相同的判断(重写equals),当学生姓名和学号一样时,则学生相同。
时间: 2024-03-18 22:41:46 浏览: 256
好的,我已经理解了您的问题。下面是一个示例代码,可以实现您的需求:
```
import java.util.ArrayList;
import java.util.Collections;
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 setScores(ArrayList<AcademicRecord> scores) {
this.scores = scores;
}
@Override
public int compareTo(Student student) {
if (this.grade != student.grade) {
return this.grade - student.grade;
} else {
int sumScore1 = 0, sumScore2 = 0;
for (AcademicRecord academicRecord : this.scores) {
sumScore1 += academicRecord.getScore();
}
for (AcademicRecord academicRecord : student.scores) {
sumScore2 += academicRecord.getScore();
}
return sumScore2 - sumScore1;
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Student student = (Student) o;
return studID.equals(student.studID) && name.equals(student.name);
}
@Override
public int hashCode() {
return studID.hashCode() + name.hashCode();
}
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
ArrayList<AcademicRecord> scores1 = new ArrayList<>();
scores1.add(new AcademicRecord("1001", "math", 90));
scores1.add(new AcademicRecord("1001", "english", 80));
scores1.add(new AcademicRecord("1001", "chinese", 70));
Student student1 = new Student("Tom", "1001", "Computer Science", 3, scores1);
ArrayList<AcademicRecord> scores2 = new ArrayList<>();
scores2.add(new AcademicRecord("1002", "math", 85));
scores2.add(new AcademicRecord("1002", "english", 90));
scores2.add(new AcademicRecord("1002", "chinese", 80));
Student student2 = new Student("Jerry", "1002", "Computer Science", 3, scores2);
ArrayList<AcademicRecord> scores3 = new ArrayList<>();
scores3.add(new AcademicRecord("1003", "math", 95));
scores3.add(new AcademicRecord("1003", "english", 85));
scores3.add(new AcademicRecord("1003", "chinese", 90));
Student student3 = new Student("Lily", "1003", "Computer Science", 2, scores3);
students.add(student1);
students.add(student2);
students.add(student3);
Collections.sort(students);
for (Student student : students) {
System.out.println(student.getName() + ", " + student.getStudID() + ", " + student.getGrade());
}
}
}
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;
}
}
```
在这个示例代码中,我们定义了一个`Student`类和一个`AcademicRecord`类。每个`Student`包含姓名、学号、专业、年级和成绩等信息,每个`AcademicRecord`包含学号、课程号和成绩等信息。我们使用`ArrayList`来存储所有学生的信息。
`Student`类实现了`Comparable`接口,并且重写了`compareTo`方法。在排序时,我们按照年级和总成绩的顺序进行排序。在判断学生是否相同时,我们重写了`equals`和`hashCode`方法,当学生姓名和学号一样时,则判定为学生相同。
在`main`方法中,我们创建了三个学生,并将它们添加到`ArrayList`中。然后,我们使用`Collections.sort`方法对学生进行排序,并输出学生的姓名、学号和年级。
阅读全文