编写一个Java程序, 用List集合管理学生对象。设计学生Student类和学生管理类StudentManager。Student类包含学号、姓名、上学期所学课程成绩(不少于3门)、总分。StudentManager类的功能包括添加学生、查询学生、删除学生、统计学生成绩等。分别用LinkedList和ArrayList集合来管理若干学生实例。另外还需要设计测试类Test来验证StudentManager的功能。
时间: 2024-02-22 08:57:40 浏览: 110
好的,我明白了。以下是对应的Java代码:
```
// 学生类
public class Student {
private int id; // 学号
private String name; // 姓名
private List<Double> scores; // 上学期所学课程成绩
private double totalScore; // 总分
public Student(int id, String name, List<Double> scores) {
this.id = id;
this.name = name;
this.scores = scores;
this.totalScore = 0;
for (Double score : scores) {
this.totalScore += score;
}
}
// 计算平均分
public double getAverageScore() {
return this.totalScore / this.scores.size();
}
// getter 和 setter
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Double> getScores() {
return scores;
}
public void setScores(List<Double> scores) {
this.scores = scores;
this.totalScore = 0;
for (Double score : scores) {
this.totalScore += score;
}
}
public double getTotalScore() {
return totalScore;
}
public void setTotalScore(double totalScore) {
this.totalScore = totalScore;
}
}
// 学生管理类
public class StudentManager {
private List<Student> students;
public StudentManager() {
this.students = new LinkedList<>(); // 或者 ArrayList
}
// 添加学生
public void addStudent(Student student) {
this.students.add(student);
}
// 查询学生
public Student getStudent(int id) {
for (Student student : this.students) {
if (student.getId() == id) {
return student;
}
}
return null;
}
// 删除学生
public void deleteStudent(int id) {
Iterator<Student> iterator = this.students.iterator();
while (iterator.hasNext()) {
Student student = iterator.next();
if (student.getId() == id) {
iterator.remove();
break;
}
}
}
// 统计学生成绩
public double getAverageScore() {
double totalScore = 0;
int count = 0;
for (Student student : this.students) {
totalScore += student.getTotalScore();
count += student.getScores().size();
}
return totalScore / count;
}
// getter 和 setter
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
// 测试类
public class Test {
public static void main(String[] args) {
Student s1 = new Student(1001, "张三", Arrays.asList(80.0, 85.0, 90.0));
Student s2 = new Student(1002, "李四", Arrays.asList(75.0, 88.0, 92.0));
Student s3 = new Student(1003, "王五", Arrays.asList(90.0, 91.0, 95.0));
StudentManager manager = new StudentManager();
manager.addStudent(s1);
manager.addStudent(s2);
manager.addStudent(s3);
// 查询学生
Student s = manager.getStudent(1002);
System.out.println(s.getName() + "的平均分是:" + s.getAverageScore());
// 删除学生
manager.deleteStudent(1001);
// 统计学生成绩
System.out.println("所有学生的平均分是:" + manager.getAverageScore());
}
}
```
以上代码使用List集合管理学生对象,Student类包含学号、姓名、上学期所学课程成绩(不少于3门)、总分。StudentManager类的功能包括添加学生、查询学生、删除学生、统计学生成绩等。分别用LinkedList和ArrayList集合来管理若干学生实例。另外还设计了测试类Test来验证StudentManager的功能。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)