【发布时间】:2017-03-02 03:03:50
【问题描述】:
我应该让教授担任课程的讲师。该方法上方的评论也说明了目标。但是,我对如何去做这件事感到困惑。我是使用 java 编程的新手,所以我决定通过“instanceof”比较来检查教授是否在课程中。我是朝着正确的方向前进还是错过了什么?谢谢!
-
接近尾声时,我添加了我刚刚遇到的 2 个其他方法,我相信我错误地实现了我的 getStudents。最后两种方法要求添加和删除学生。我不确定学生的键值对是什么。
public class Course 实现 Comparable {
/** * course id (unique) */ private int id; /** * course name */ private String name; /** * course level */ private int level; /** * professor teaching the course, null if none */ private String professor; /** * students enrolled in the course (unique), empty if none */ private HashSet<String> students; /** * Create a course. Initially there is no professor or student for the * course. * * @param id course id * @param name course name * @param level course level */ public Course(int id, String name, int level) { this.id = id; this.name = name; this.level = level; this.professor = null; this.students = new HashSet<>(); } /** * Get the course id. * * @return course id */ public int getId() { return id; } /** * Get the course name. * * @return course name */ public String getName() { return name; } /** * Get the course level. * * @return course level */ public int getLevel() { return level; } /** * Get the professor teaching the course. * * @return the professor, if none, null */ public String getProfessor() { return professor; } /** * Get the students enrolled in the course. * * @return the students, organized by ascending hash codes. if there are no * students enrolled the list should be empty. */ public Collection<String> getStudents() { return students; } /** * Make a professor the instructor of this course. If another professor was * teaching it, that information is lost. * * @param username the username of the professor */ public void addProfessor(String username) { }} /**
- 将学生添加到课程中,如果他们没有注册,
- 在恒定时间内。 *
- @param username 学生的用户名
- @return 是否添加了学生 */ 公共布尔addStudent(字符串用户名){ //去做 返回假; }
/**
- 从课程中删除学生(如果他们已注册),
- 在恒定时间内。 *
- @param username 要删除的学生的用户名
- @return 如果学生被删除,则返回 true,如果学生不在课程中,则返回 false */ 公共布尔删除学生(字符串用户名){ // 去做 返回假; }
【问题讨论】:
-
addProfessor方法的注释说以前的教授会被新教授覆盖,所以你真的要检查教授是否已经在课程中了吗? -
关于添加和删除学生方法的实现,请看一下 Set 上的添加和删除方法。 docs.oracle.com/javase/8/docs/api/java/util/Set.html
标签: java