Java集合练习题 求学生对象的平均年龄及按班级求平均分数

时间:2024-02-24 19:24:09

有如下Student 对象,
private String name;
private int age;
private int score;
private String classNum;

其中,classNum 表示学生的班号,例如“class05”。 有如下List

List list = new ArrayList();

list.add(new Student(“Tom”, 18, 100, “class05”));
list.add(new Student(“Jerry”, 22, 70, “class04”));
list.add(new Student(“Owen”, 25, 90, “class05”));
list.add(new Student(“Jim”, 30,80 , “class05”));
list.add(new Student(“Steve”, 28, 66, “class06”));
list.add(new Student(“Kevin”, 24, 100, “class04”));

在这个list 的基础上,完成下列要求:
1) 计算所有学生的平均年龄
2) 计算各个班级的平均分

 

主类:

 1 import java.util.ArrayList;
 2 
 3 public class TestDay17_1_2 {
 4 
 5     public static void main(String[] args) {
 6         ArrayList<TestDay17_1_2_Student> al = new ArrayList<>();
 7 
 8         al.add(new TestDay17_1_2_Student("Tom", 18, 100, "class05"));
 9         al.add(new TestDay17_1_2_Student("Jerry", 22, 70, "class04"));
10         al.add(new TestDay17_1_2_Student("Owen", 25, 90, "class05"));
11         al.add(new TestDay17_1_2_Student("Jim", 30, 80, "class05"));
12         al.add(new TestDay17_1_2_Student("Steve", 28, 66, "class06"));
13         al.add(new TestDay17_1_2_Student("Kevin", 24, 100, "class04"));
14 
15         // 计算平均年龄
16         //定义总年龄及人数变量
17         int sumAge = 0;
18         int count = 0;
19         //遍历集合 总年龄累加、人数自增
20         for (TestDay17_1_2_Student s : al) {
21             sumAge += s.getAge();
22             count++;
23         }
24         //计算并输出平均年龄
25         double averageAge = (sumAge * 1.0) / count;
26         System.out.println("平均年龄是:" + averageAge);
27 
28         //计算各班级平均分
29         // 创建储存计算分数的集合
30         ArrayList<TestDay17_1_2_Score> alscore = new ArrayList<>();
31         // 遍历班级唯一插入成绩对象
32 
33         for (int i = 0; i < al.size(); i++) {
34             //过去当前学生对象
35             TestDay17_1_2_Student student = al.get(i);
36             boolean flag = false;//标记没有重复班级名称
37             for (int j = 0; j < alscore.size(); j++) {
38                 //获取当前班级成绩对象
39                 TestDay17_1_2_Score score = alscore.get(j);
40                 //判断班级名称是否重复
41                 if (student.getClassNum().equals(score.getClassName())) {
42                     //重复标记设为真
43                     flag = true;
44                     //修改当前成绩对象中分数和人数信息并跳出内循环
45                     score.setStudentNum(score.getStudentNum()+1);
46                     score.setSumScore(score.getSumScore() + student.getScore());
47                     break;
48                 }
49             }
50             
51             //判断flag如果为假(没有重复)则添加一个新的成绩对象
52             if (!flag) {
53                 alscore.add(new TestDay17_1_2_Score(student.getClassNum(),1,student.getScore()));
54             }
55         }
56         for (TestDay17_1_2_Score score : alscore) {
57             System.out.println(score.toString());
58         }
59     }
60 }

学生类:

 1 public class TestDay17_1_2_Student {
 2 
 3     private String name;  
 4     private int age;  
 5     private int score;  
 6     private String classNum;
 7     public TestDay17_1_2_Student() {
 8         super();
 9         // TODO Auto-generated constructor stub
10     }
11     public TestDay17_1_2_Student(String name, int age, int score, String classNum) {
12         super();
13         this.name = name;
14         this.age = age;
15         this.score = score;
16         this.classNum = classNum;
17     }
18     public String getName() {
19         return name;
20     }
21     public void setName(String name) {
22         this.name = name;
23     }
24     public int getAge() {
25         return age;
26     }
27     public void setAge(int age) {
28         this.age = age;
29     }
30     public int getScore() {
31         return score;
32     }
33     public void setScore(int score) {
34         this.score = score;
35     }
36     public String getClassNum() {
37         return classNum;
38     }
39     public void setClassNum(String classNum) {
40         this.classNum = classNum;
41     }
42 }

成绩类:

 1 public class TestDay17_1_2_Score {
 2 
 3     private String className;
 4     private int studentNum;
 5     private int sumScore;
 6     
 7     public TestDay17_1_2_Score() {
 8         super();
 9         // TODO Auto-generated constructor stub
10     }
11     public TestDay17_1_2_Score(String className, int studentNum, int sumScore) {
12         super();
13         this.className = className;
14         this.studentNum = studentNum;
15         this.sumScore = sumScore;
16     }
17     public String getClassName() {
18         return className;
19     }
20     public void setClassName(String className) {
21         this.className = className;
22     }
23     public int getStudentNum() {
24         return studentNum;
25     }
26     public void setStudentNum(int studentNum) {
27         this.studentNum = studentNum;
28     }
29     public int getSumScore() {
30         return sumScore;
31     }
32     public void setSumScore(int sumScore) {
33         this.sumScore = sumScore;
34     }
35     
36     public double getAverageScore() {
37         return (this.sumScore * 1.0) / this.studentNum;
38     }
39     
40     @Override
41     public String toString() {
42         double averageScore = this.getAverageScore();
43         String className = this.getClassName();
44         int studentNum = this.getStudentNum();
45         int sumScore = this.getSumScore();
46         return
47             (new StringBuffer()).append(className)
48             .append("班级有").append(studentNum)
49             .append("个同学,总分:").append(sumScore)
50             .append(",平均分:").append(averageScore).toString()
51         ;
52     }
53 }