集合题目:
1,定义一个Course类,代表课程;定义一个Student类,代表学生,在Student类中包含一个属性是一个HashSet的对象,用来存储该学生所选的所有课程,并提供相应的addCourse(Course c)方法
2,定义一个类SchoolClass代表班级,该类中包含一个属性是一个HashSet的对象,用来存储该班级中所有的Student,并提供相应的addStudent(Student s)方法
3,在主方法中生成一个SchoolClass对象,添加若干个学生,并且为每个学生添加若干门课程,最后要统计出每门课程的选课人数。(随机)
Course类:
package exercise3; public class Course { private String cname; public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public Course(String cname) { this.cname = cname; } public boolean equals(Object o) { if (o instanceof Course) { Course c = (Course) o; if (c.cname.equals(this.cname)) { return true; } } return false; } public int hashCode() { return this.cname.hashCode(); } @Override public String toString() { StringBuffer sb = new StringBuffer(); return sb.append(cname).toString(); } }
Student类:
package exercise3; import java.util.HashSet; public class Student { private String sname; private HashSet<Course> courses; // 构造方法 public Student(String sname) { this.sname = sname; courses = new HashSet<Course>(); } // 增加课程 public boolean addCourse(Course c) { return courses.add(c); } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public HashSet<Course> getCourses() { return courses; } public void setCourses(HashSet<Course> courses) { this.courses = courses; } public boolean equals(Object o) { if (o instanceof Student) { Student s = (Student) o; if (s.sname.equals(this.sname)) { return true; } } return false; } public int hashCode() { return this.sname.hashCode(); } @Override public String toString() { StringBuffer sb = new StringBuffer(); return sb.append("Student [Sname=").append(sname).append(courses) .append("]").toString(); } }
SchoolClass类:
package exercise3; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; public class SchoolClass { private String name; private HashSet<Student> students;// 班级中所有的学生 public SchoolClass(String name) { this.name = name; students = new HashSet<Student>(); } public boolean addStudent(Student s) { return students.add(s); } public Map<Course, Integer> account() {// 统计每门课程的选课人数,key代表课程,value代表人数。 Map<Course, Integer> map = new HashMap<Course, Integer>(); Iterator<Student> it = students.iterator(); while (it.hasNext()) {// 遍历所有学生 Student stu = it.next(); Iterator<Course> is = stu.getCourses().iterator(); while (is.hasNext()) {// 遍历每个学生所选的课程 Course c = is.next(); map.put(c, map.get(c) == null ? 1 : map.get(c) + 1); } } return map; } public void printCount() { Set<Course> set = account().keySet(); Iterator<Course> it = set.iterator(); while (it.hasNext()) { Object key = it.next(); System.out.println(key + "," + account().get(key)); } } }
Test测试类:
package exercise3; import java.util.HashSet; import java.util.Iterator; import java.util.Random; public class Test { /** * @param args */ public static void main(String[] args) { Course[] course = { new Course("Java语言"), new Course("高数"), new Course("英语"), new Course("数据库") }; SchoolClass class1 = new SchoolClass("Class1"); for (int i = 0; i < 20; i++) { Student stu = new Student("Student-" + (i + 1)); int num = (int) (Math.random() * course.length); for (int j = 0; j < num; j++) { stu.addCourse(course[(int) (Math.random() * course.length)]); } class1.addStudent(stu); System.out.println(stu); } System.out.println("=================================="); class1.printCount(); } }
运行结果:
Student [Sname=Student-1[]] Student [Sname=Student-2[]] Student [Sname=Student-3[Java语言]] Student [Sname=Student-4[数据库]] Student [Sname=Student-5[Java语言, 英语, 高数]] Student [Sname=Student-6[英语, 数据库]] Student [Sname=Student-7[]] Student [Sname=Student-8[Java语言, 英语]] Student [Sname=Student-9[数据库]] Student [Sname=Student-10[高数]] Student [Sname=Student-11[英语, 高数]] Student [Sname=Student-12[Java语言]] Student [Sname=Student-13[]] Student [Sname=Student-14[英语]] Student [Sname=Student-15[数据库, 英语]] Student [Sname=Student-16[英语, Java语言, 数据库]] Student [Sname=Student-17[]] Student [Sname=Student-18[]] Student [Sname=Student-19[英语]] Student [Sname=Student-20[英语]] ================================== 数据库,5 Java语言,5 英语,9 高数,3