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;
}
}
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;
}
}
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]
package com.day13.exer1;
public class Course {
private String name;
public Course(String name){
this.name=name;
}
}
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;
}
}
这里没有所有课程的列表,如果有课程没有学生选,则不会出现在最后的统计结果中。除非我们约定,没有学生选的课程不用列出来。不过这样一来我们就不知道有多少课程没有学生选了。所以最好加上一个所有课程的集合。用一个两重循环,在循环中使用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;
}
如果考虑效率的话,可以考虑将课程列表按顺序排列。并使用列表来存储,这样可以改进算法提高效率。
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;
}