当前位置: 代码迷 >> Java相关 >> 速求。
  详细解决方案

速求。

热度:175   发布时间:2007-01-23 19:05:00.0
速求。
1,定义一个Course类,代表课程;定义一个Student类,代表学生,在Student类中包含一个属性是一个HashSet的对象,用来存储该学生所选的所有课程,并提供相应的addCourse(Course c)方法和removeCourse(String name)方法,表示添加一门选课和删除一门选课(删除选课时通过传课程名参数来确定)。
2,定义一个类SchoolClass代表班级,该类中包含一个属性是一个HashSet的对象,用来存储该班级中所有的Student,并提供相应的addStudent(Student s)方法和removeStudent(String name)方法,表示添加一名学生和删除一名学生(删除学生时通过传学生姓名参数来确定)。
3,在主方法中生成一个SchoolClass对象,添加若干个学生,并且为每个学生添加若干门课程(课程允许重复,即a课程既可以让s1学生选,也可以让s2学生选),最后要统计出每门课程的选课人数。

高手,指导。
搜索更多相关的解决方案: 课程  

----------------解决方案--------------------------------------------------------
高手们呢阿。。。一道新手题吧?
帮助下了,不需要复杂。。大概说下,整个思想就好。。
----------------解决方案--------------------------------------------------------
HashSet就是不包含重复元素且无序的对象集合吧,查一下API很快就能找到该用的方法。
----------------解决方案--------------------------------------------------------
帮我写出来,我看下吧!!!急求现在。。。谢了阿!
----------------解决方案--------------------------------------------------------
Set s=new HashSet();
s.add(Object)
----------------解决方案--------------------------------------------------------

最后一个要求是不,需要循环一下?


----------------解决方案--------------------------------------------------------
当然要用循环,要不你要一个一个写进去
----------------解决方案--------------------------------------------------------

我帮你写了一个,所有功能都能实现,但是最后计算每门课程有多少学生选修了..

实现的方法好像不太好..请问下会的朋友对于这种情况有没有更好的方法来判断

每门课有多少学生选修了.谢谢!
(下面代码蓝色部份为判断每门课有多少学生选修的方法)


public class Course

{
private String name;
private int count = 0; //此门课程选修人数

public Course()
{}

public void addCourse() //添加课程选修总数
{
count++;
}

public void removeCourse() //删除课程总数
{
if(count>0)
count--;
else
System.out.println("已经没有课程可删了");
}

public int getCount() //得到报此课程的人数;
{
return count;
}


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

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}
}


import java.util.HashSet;

public class Student
{
private HashSet<Course> hsCourse = new HashSet<Course>(); //所有课程

public HashSet getAllHsCourse() //得到所有课程
{
return hsCourse;
}

public void addCourse(Course c) //添加课程
{
hsCourse.add(c);
c.addCourse();
}

public void removeCourse(String name) //删除课程
{
for(Course c : hsCourse)
{
if(c.getName().equals(name))
{
hsCourse.remove(name);
c.removeCourse();
}
}
}


public void printAllCourse() //显示此学生所选中的所有课程
{
for(Course c : hsCourse)
{
System.out.println(c.getName());
}
}

}


import java.util.HashSet;

public class SchoolClass
{
HashSet<Student> hsStudent = new HashSet<Student>();

public void addStudent(Student s) //添加学生
{
hsStudent.add(s);
}

public void removeStudent(String name) //删除学生
{
hsStudent.remove(name);
}

public void printAllStudent() //显示所有学生
{
for(Student stu : hsStudent)
{
System.out.println(stu);
}
}
}



public class Test
{
public static void main(String[] args)
{
SchoolClass objSchool = new SchoolClass();

Course chineseCourse = new Course("chinese");
Course englishCourse = new Course("english");

Student stu1 = new Student();
Student stu2 = new Student();

objSchool.addStudent(stu1); //添加学生

stu1.addCourse(chineseCourse);//学生1加课程
stu1.addCourse(englishCourse);

stu2.addCourse(chineseCourse);

stu1.printAllCourse(); //打印学生1选的所有课程

stu1.removeCourse("english"); //学生1删除选项中课程
stu1.printAllCourse();

System.out.println("语文课有" + chineseCourse.getCount()+"位学生选修!");
System.out.println("英语课有" + englishCourse.getCount()+"位学生选修!");
}
}


----------------解决方案--------------------------------------------------------
  相关解决方案