添加和删​​除元素

时间:2017-03-02 03:03:50

标签: java

我应该让教授成为一门课程的导师。方法上方的评论也说明了目标。但是我很困惑如何解决这个问题。我是java编程的新手,所以我决定通过“instanceof”比较检查教授是否在课程中。我是朝着正确的方向前进还是我错过了什么?谢谢!

  • 接近结束时,我添加了其他两个我刚遇到的方法,我相信我错误地实现了我的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是否添加了学生 * / public boolean addStudent(String username){ //去做 返回false; }

    / **

    • 从课程中删除学生,如果他们已注册,
    • 恒定时间。 *
    • @param username要移除的学生的用户名
    • @return如果学生被移除则为true,如果学生不在课程中,则为false * / public boolean removeStudent(String username){ // 去做 返回false; }

3 个答案:

答案 0 :(得分:1)

您可以在professor上调用getter来检查它是否已设置。

public void addProfessor(String professor) {
     if(this.getProfessor() == null || this.getProfessor().isEmpty()) {
         this.setProfessor(professor);
     }
}

答案 1 :(得分:1)

只需实现方法addProfessor -

public void addProfessor(String username) {
    /*
    This assignment will replace any other professor
    who are teaching of this course.
    If you assign the class variable **professor**,
    when you call getProfessor(), that will return
    username this professor.
    */
    this.professor = username;
}

答案 2 :(得分:1)

使用instanceof关键字检查对象在运行时是否属于特定类型。在这种情况下,评论指出如果先前设置了一个值,那么它就会丢失。这意味着你可以简单地将给定值设置为教授而不进行任何检查

public void addProfessor(String userName) {
    professor = userName;
}