1 package collection.map; 2 3 4 public class Student { 5 private String name; 6 private String classNo; 7 private double score; 8 9 10 public Student(String name, String classNo, double score) {11 super();12 this.name = name;13 this.classNo = classNo;14 this.score = score;15 }16 public String getName() {17 return name;18 }19 public void setName(String name) {20 this.name = name;21 }22 public String getClassNo() {23 return classNo;24 }25 public void setClassNo(String classNo) {26 this.classNo = classNo;27 }28 public double getScore() {29 return score;30 }31 public void setScore(double score) {32 this.score = score;33 }34 35 36 }
Package collection. map; import Java. util. arraylist; import Java. util. list; public class classroom {private string roomno; private list <student> stus; // student list private double total; // total public classroom () {stus = new arraylist <student> ();} public classroom (string roomno) {this (); this. roomno = roomno;} Public String getroomno () {return roomno;} public void setroomno (string roomno) {This. roomno = roomno;} public list <student> getstus () {return stus;} public void setstus (list <student> stus) {This. stus = stus;} public double gettotal () {return total;} public void settotal (double total) {This. total = total ;}}
1 package collection. map; 2 3 Import Java. util. arraylist; 4 Import Java. util. hashmap; 5 import Java. util. iterator; 6 Import Java. util. list; 7 Import Java. util. map; 8 Import Java. util. set; 9 10 11/* 12 * defines a student class. attributes: name, classnumber, and score 13 *. Now, several student objects are put into the list, calculate the total score and average score of each class, print out 14 * solutions with object-oriented thinking 15*16 */17 public class mapdemo02 {18 19 // put several student into list20 public static void exam (list <student> List) 21 {22 list. add (new student ("AAA", "301", 88); 23 list. add (new student ("BBB", "301", 84); 24 list. add (new student ("CCC", "302", 85); 25 list. add (new student ("DDD", "302", 86); 26 List. add (new student ("eee", "201", 46); 27 List. add (new student ("fff", "201", 68); 28 list. add (new student ("AAA", "301", 88); 29} 30 31 // measure the score of 32 public static void count (Map <string, classroom> rooms, list <student> List) 33 {34 for (student STU: List) 35 {36 string NO = Stu. getclassno (); 37 Double score = Stu. getscore (); 38 // check whether map exists in this class sorting idea by class number 39 classroom room = rooms. get (NO); 40 if (null = room) 41 {42 room = new classroom (NO); 43 room. put (No, room); 44} 45 // total storage score 46 room. settotal (room. gettotal () + score); 47 room. getstus (). add (Stu); 48} 49} 50 51 // print the total score and average score 52 public static void printscoreandavg (Map <string, classroom> rooms) 53 {54 set <map. entry <string, classroom> entryset = rooms. entryset (); 55 iterator <map. entry <string, classroom> iter = entryset. iterator (); 56 while (ITER. hasnext () {57 map. entry <string, classroom> entry = ITER. next (); 58 classroom room = entry. getvalue (); 59 double AVG = math. round (room. gettotal ()/room. getstus (). size (); 60 system. out. println ("shift:" + room. getroomno () + ", total score:" + room. gettotal () + "average score:" + avg); 61} 62} 63 64 public static void main (string [] ARGs) {65 list <student> List = new arraylist <student> (); 66 exam (list); 67 68 // count 69 Map <string, classroom> room = new hashmap <string, classroom> (); 70 count (room, list); 71 printscoreandavg (room); 72 73} 74}
Calculate the total score and average score of each class.