/*
某中学有若干学生(学生对象放在一个List中),每个学生都有一个姓名属性、班级名称属性(String)和考试成绩属性(int),某次考试结束后,每个学生都获得了一个考试成绩。请打印出每个班级的总分和平均分。
*/
转自:http://www.cnblogs.com/zebbin/archive/2013/01/22/2872025.html
1 package com.study.java11; 2 3 //Student类 4 public class Student 5 { 6 private String name; //学生姓名 7 private Team team; //学生所在班级 8 private int score; //学生成绩 9 public Student(String name,int score)10 {11 this.setName(name);12 this.setScore(score);13 }14 public String getName()15 {16 return name;17 }18 public void setName(String name)19 {20 this.name = name;21 }22 public int getScore()23 {24 return score;25 }26 public void setScore(int score)27 {28 this.score = score;29 }30 public Team getTeam()31 {32 return team;33 }34 public void setTeam(Team team)35 {36 this.team = team;37 }38 39 public String toString() //覆写toString方法40 {41 return "\t|-姓名:"+this.name +";成绩:"+this.score;42 }43 }
1 package com.study.java11; 2 3 //Team类 4 import java.util.ArrayList; 5 import java.util.Iterator; 6 import java.util.List; 7 8 9 public class Team10 {11 private String name; //班级名称12 private Listlist; //学生list(一个班级里有很多学生)13 14 public Team()15 {16 this.list = new ArrayList ();17 }18 19 public Team(String name)20 {21 this();22 this.setName(name);23 }24 25 public String getName()26 {27 return name;28 }29 30 public void setName(String name)31 {32 this.name = name;33 }34 35 public List getList()36 {37 return list;38 }39 40 public void setList(List list)41 {42 this.list = list;43 }44 public String toString()45 {46 return "班级名称"+this.name;47 }48 49 public double sum()50 {51 Iterator iter =list.iterator(); 52 double sum = 0;53 while(iter.hasNext())54 {55 Student s = iter.next();56 sum += s.getScore();57 }58 59 return sum;60 }61 62 public double average()63 {64 return this.sum()/list.size();65 }66 }
1 package com.study.java11; 2 3 import java.util.Iterator; 4 5 6 public class TestList 7 { 8 public static void main(String[] args) 9 {10 Student s1 = new Student("张三",46);11 Student s2 = new Student("李四",98);12 Student s3 = new Student("王五",47);13 Student s4 = new Student("任六",87);14 Student s5 = new Student("黑七",45);15 Student s6 = new Student("赵四",87);16 Student s7 = new Student("孙九",95);17 Student s8 = new Student("钱七",76);18 19 Team t1 = new Team("一班");20 Team t2 = new Team("二班");21 22 t1.getList().add(s1);23 t1.getList().add(s2);24 t1.getList().add(s6);25 t1.getList().add(s8);26 27 t2.getList().add(s3);28 t2.getList().add(s4);29 t2.getList().add(s5);30 t2.getList().add(s7);31 32 s1.setTeam(t1);33 s2.setTeam(t1);34 s6.setTeam(t1);35 s8.setTeam(t1);36 37 s3.setTeam(t2);38 s4.setTeam(t2);39 s5.setTeam(t2);40 s7.setTeam(t2);41 42 System.out.println(t1);43 Iteratoriter1 = t1.getList().iterator();44 while(iter1.hasNext())45 {46 Student s = iter1.next();47 System.out.println(s);48 }49 50 System.out.println("总成绩为:"+t1.sum()+",平均成绩为:"+t1.average());51 System.out.println("--------------------------------");52 53 System.out.println(t2);54 Iterator iter2 = t2.getList().iterator();55 while(iter2.hasNext())56 {57 Student s = iter2.next();58 System.out.println(s);59 }60 61 System.out.println("总成绩为:"+t2.sum()+",平均成绩为:"+t2.average());62 System.out.println("--------------------------------");63 64 }65 }