package com.day13.exer1;public class Course {
private String name;

public Course(String name){
this.name=name;
}
}
-----------------------Course类存学生选课课程的
package com.day13.exer1;
import java.util.HashSet;
public class Student {
private String name;
private HashSet<Course>  course;

public Student(String name){
super();
this.name=name;
course=new HashSet();  
}

public HashSet addCourse(Course c){
this.course.add(c);
return course;
}
public void removeCourse(String name){
this.course.remove(name);
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public HashSet<Course> getCourse() {
return course;
} public void setCourse(HashSet<Course> course) {
this.course = course;
}

}
-------------------------------------------Student类,包含一个属性是一个HashSet的的course存每个学生选的科目。还提供了增删课程方法
package com.day13.exer1;
import java.util.HashSet;
import java.util.Map;
import java.util.HashMap;
public class SchoolClass {
private HashSet<Student> student;

public SchoolClass(){
super();
student=new HashSet();
}

public HashSet addStudent(Student s){
this.student.add(s);
return student;
}

public void removeStudent(String name){
this.student.remove(name);
}


public Map account(){                   //这里不会写。统计每门课程的选课人数 Map account() Map<Course,Integer> map=new HashMap<Course,Integer>();//返回个map key代表课程value代表人数
for(int i=0;i<student.size();i++){

}




return map;
}

}

解决方案 »

  1.   

    头疼!帮你格式化哈代码 他们好看点package com.day13.exer1;public class Course {
    private String name;public Course(String name){
    this.name=name;
    }
    }
    -----------------------Course类存学生选课课程的
    [code=Java]package com.day13.exer1;
    import java.util.HashSet;
    public class Student {
    private String name;
    private HashSet<Course> course;public Student(String name){
    super();
    this.name=name;
    course=new HashSet();   
    }public HashSet addCourse(Course c){
    this.course.add(c);
    return course;
    }
    public void removeCourse(String name){
    this.course.remove(name);
    }public String getName() {
    return name;
    }public void setName(String name) {
    this.name = name;
    }public HashSet<Course> getCourse() {
    return course;
    }public void setCourse(HashSet<Course> course) {
    this.course = course;
    }}
    -------------------------------------------Student类,包含一个属性是一个HashSet的的course存每个学生选的科目。还提供了增删课程方法
    package com.day13.exer1;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.HashMap;
    public class SchoolClass {
    private HashSet<Student> student;public SchoolClass(){
    super();
    student=new HashSet();
    }public HashSet addStudent(Student s){
    this.student.add(s);
    return student;
    }public void removeStudent(String name){
    this.student.remove(name);
    }
    public Map account(){ //这里不会写。统计每门课程的选课人数 Map account()  Map<Course,Integer> map=new HashMap<Course,Integer>();//返回个map key代表课程value代表人数
    for(int i=0;i<student.size();i++){}
    return map;
    }}
    [/code]
      

  2.   


    package com.day13.exer1;
    public class Course {
    private String name;

    public Course(String name){
    this.name=name;
    }
    }
      

  3.   

    package com.day13.exer1;public class Course {
    private String name;

    public Course(String name){
    this.name=name;
    }
    }
    package com.day13.exer1;
    import java.util.HashSet;
    public class Student {
    private String name;
    private HashSet<Course>  course;

    public Student(String name){
    super();
    this.name=name;
    course=new HashSet();   
    }

    public HashSet addCourse(Course c){
    this.course.add(c);
    return course;
    }
    public void removeCourse(String name){
    this.course.remove(name);
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public HashSet<Course> getCourse() {
    return course;
    } public void setCourse(HashSet<Course> course) {
    this.course = course;
    }

    }
    package com.day13.exer1;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.HashMap;
    public class SchoolClass {
    private HashSet<Student> student;

    public SchoolClass(){
    super();
    student=new HashSet();
    }

    public HashSet addStudent(Student s){
    this.student.add(s);
    return student;
    }

    public void removeStudent(String name){
    this.student.remove(name);
    }


    public Map account(){
    Map<Course,Integer> map=new HashMap<Course,Integer>();
    for(int i=0;i<student.size();i++){
    for(int j=0;j<this.)//这里就不会了统计每门课程的选课人数 Map  }                           //Map account();
                        //一个Map,key代表课程,value代表人数



    return map;
    }

    }
      

  4.   

        这个问题问题,如果放在数据库中使用Sql来做会简单一些。一张学生表,一张课程表,再做一张关联表,查询关联表就可以得到楼主需要的结果。如果一定希望操作类来完成这个统计也是可以的。不过这里就缺少了一些条件,或者说是约定。
        这里没有所有课程的列表,如果有课程没有学生选,则不会出现在最后的统计结果中。除非我们约定,没有学生选的课程不用列出来。不过这样一来我们就不知道有多少课程没有学生选了。所以最好加上一个所有课程的集合。用一个两重循环,在循环中使用Set的contains方法即可。但是,这里要注意的是,contains方法,会调用对象的equals方法,所以课程对象,应当重载equals方法,否则将比较的是地址。而这里根据对业务的理解,两个课程,只要name相同就认为是相同的课程。所以应当实现如下的equals方法
    public boolean equals(Object obj){
       if(!(obj instansof Course))
          return false;
       return this.name==((Course)obj).name;
    }public Map account(){
            HashSet allCourse = getAllCourse();//getAllCourse用于获取所有的课程
            Map<Course,Integer> map=new HashMap<Course,Integer>();
            int count = 0;
            for(Course cs in allCourse){
                count = 0;
                for(Student stu in student){
                    if(stu.course.contains(cs)
                      count++;
                }
                map.add(cs,count);
            } 
            return map;
    }
    如果考虑效率的话,可以考虑将课程列表按顺序排列。并使用列表来存储,这样可以改进算法提高效率。
      

  5.   

    那个连接数据库还没学 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代表人数。
      

  6.   

    如果是JDK6,应该可以这样实现    public Map account(){
            Map<Course,Integer> map=new HashMap<Course,Integer>();
            //一个Map,key代表课程,value代表人数
             for(Student s : student){
                for(Course c : s.getCourse()){
                    Integer count = map.get(c);
                    if(count==null){
                        map.put(c,0);
                    }
                    map.put(c,map.get(c)+1);
                }
            }        
            return map;
        }