为SchoolClass类写一个方法,统计每门课程的选课人数 Map account();该方法返回一个Map,key代表课程,value代表人数。

解决方案 »

  1.   

    import java.util.*;
    public class SchoolClass {
    private HashSet<Student> allStudent;
    public boolean addStudent(Student s){
    allStudent.add(s);
    return true;
    }
    public Student removeStudent(String name){
    Student stu=new Student(name);
    allStudent.remove(stu);
    return stu;
    }
    public SchoolClass(){
    allStudent=new HashSet<Student>();
    }
    public Map<Course,Integer> account(){
    Map<Course,Integer> map=new HashMap<Course,Integer>();
    Set<Course> courseSet=new HashSet<Course>();
    for(Student student:allStudent){
    for(Course course:student.getAllCourse()){
    courseSet.add(course);
    }
    }

    for(Course cou:courseSet){
    int j=0;
    for(Student stu:allStudent){
    if(stu.getAllCourse().contains(cou)){
    j++;
    }
    }
    map.put(cou, j);
    }

    return map;
    }
    }
    结贴吧
      

  2.   

    1,定义一个Course类,代表课程;定义一个Student类,代表学生,在Student类中包含一个属性是一个HashSet的对象,用来存储该学生所选的所有课程,并提供相应的addCourse(Course c)方法和removeCourse(String name)方法,表示添加一门选课和删除一门选课(删除选课时通过传课程名参数来确定)。
    2,定义一个类SchoolClass代表班级,该类中包含一个属性是一个HashSet的对象,用来存储该班级中所有的Student,并提供相应的addStudent(Student s)方法和removeStudent(String name)方法,表示添加一名学生和删除一名学生(删除学生时通过传学生姓名参数来确定)。
    3,为SchoolClass类写一个方法,统计每门课程的选课人数 Map account();该方法返回一个Map,key代表课程,value代表人数。