Java1. 应用ArrayList实现某专业所有学生的成绩管理,要求构建学生(Student)类和成绩(AcademicRecord)类等,能够对不同年级的学生按总成绩排序并输出. Student(name,studID,major,grade, ArrayList< AcademicRecord > scores); AcademicRecord(studID,courseID,score) 要求学生类实现Comparable接口,排序按年级、成绩排; 并实现学生相同的判断(重写equals),当学生姓名和学号一样时,则学生相同。
时间: 2024-02-26 15:59:33 浏览: 149
JAVA-《Java程序设计B》大作业-学生成绩管理系统
好的,这是一个非常典型的Java面向对象编程问题,需要你构建两个类:Student和AcademicRecord,并且让Student类实现Comparable接口,重写equals方法。下面是一个基本的实现:
```java
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 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;
}
// 计算总成绩
public int getTotalScore() {
int totalScore = 0;
for (AcademicRecord record : scores) {
totalScore += record.getScore();
}
return totalScore;
}
// 实现Comparable接口,按年级、成绩排序
@Override
public int compareTo(Student o) {
if (this.grade != o.grade) {
return this.grade - o.grade;
} else {
return o.getTotalScore() - this.getTotalScore();
}
}
// 重写equals方法,当姓名和学号一致时则相同
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Student) {
Student s = (Student) obj;
if (this.name.equals(s.name) && this.studID.equals(s.studID)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
// 构造学生和成绩数据
ArrayList<AcademicRecord> scores1 = new ArrayList<>();
scores1.add(new AcademicRecord("1001", "语文", 80));
scores1.add(new AcademicRecord("1001", "数学", 90));
scores1.add(new AcademicRecord("1001", "英语", 85));
Student s1 = new Student("张三", "1001", "计算机科学", 3, scores1);
ArrayList<AcademicRecord> scores2 = new ArrayList<>();
scores2.add(new AcademicRecord("1002", "语文", 75));
scores2.add(new AcademicRecord("1002", "数学", 95));
scores2.add(new AcademicRecord("1002", "英语", 80));
Student s2 = new Student("李四", "1002", "计算机科学", 2, scores2);
ArrayList<AcademicRecord> scores3 = new ArrayList<>();
scores3.add(new AcademicRecord("1003", "语文", 85));
scores3.add(new AcademicRecord("1003", "数学", 70));
scores3.add(new AcademicRecord("1003", "英语", 90));
Student s3 = new Student("王五", "1003", "计算机科学", 4, scores3);
// 构造学生列表
ArrayList<Student> students = new ArrayList<>();
students.add(s1);
students.add(s2);
students.add(s3);
// 对学生列表按总成绩排序并输出
Collections.sort(students);
for (Student student : students) {
System.out.println(student.getName() + " " + student.getStudID() + " " + student.getMajor() + " " + student.getGrade() + " " + student.getTotalScore());
}
}
}
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;
}
}
```
在这个例子中,我们构造了两个类:Student和AcademicRecord。Student类包含了姓名、学号、专业、年级和成绩列表等属性,其中成绩列表是一个ArrayList类型的数据结构,存储了该学生的各门课程的成绩。Student类实现了Comparable接口,并重写了compareTo方法,实现了按年级、成绩排序的功能。另外,我们还重写了equals方法,实现了当姓名和学号一致时则相同的功能。
我们在main方法中构造了三个学生对象,并将它们放入一个ArrayList中,然后使用Collections.sort方法对这个ArrayList进行排序,排序的依据是按照学生的年级和总成绩。最后,我们遍历这个ArrayList,输出排序后的学生信息。
阅读全文