2017-03-02 290 views
0

我應該讓教授成爲課程的教師。該方法上面的評論也說明了目標。不過,如果我想知道如何解決這個問題,我很困惑。我是用java編程的新手,所以我決定通過做一個「實例」比較來檢查教授是否在課程中。我是朝着正確的方向前進還是錯過了某些東西?謝謝!添加和刪除元素

  • 接近尾聲我添加了2個其他方法,我剛剛遇到,我相信我實現了我的getStudents錯誤。最後兩種方法要求添加學生並刪除。我不確定這個學生的關鍵價值對是什麼。

    公共類課程實現可比{

    /** 
    * 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用戶名學生
    • @return是否加入學生與否的用戶名 */ 公共布爾傳遞addStudent(字符串username){// TODO 返回false; }

    /**

    • 從課程在固定時間刪除一個學生,如果他們註冊,
    • 。 *
    • @param用戶名學生如果被刪除,假如果學生在使用過程中 */ 公共布爾removeStudent(字符串username){ 不是學生的用戶名刪除
    • @返回真//TODO return false; }
+0

爲'addProfessor'方法的評論說,以前的教授將由新教授被覆蓋,所以你真的有檢查教授是否已經在課程中? – badjr

+0

要實現添加和刪除學生方法,請查看Set上的添加和刪除方法。 http://docs.oracle.com/javase/8/docs/api/java/util/Set.html –

回答

1

您使用instanceof關鍵字來檢查對象是否在運行特定類型的。在這種情況下,評論指出,如果之前設置了一個值,它就會丟失。這意味着,你可以簡單地設置給定值作爲教授沒有做任何檢查

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

這是默認情況下,如果有一個教授刪除了以前的教授? –

+0

是的,它只會覆蓋以前的值 –

+0

不確定您是否看到剛纔添加的部分,但可以解釋Student如何成爲HashSet?我很困惑什麼是鍵值對。 –

1

可以調用的getter上professor檢查它是否已經設置。

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

方法註釋聲明,如果之前已將值設置爲教授,則無關緊要。 –

+0

@AaronDavis,是的。我認爲這是OP面臨的問題。這就是爲什麼他把它作爲對該方法的評論。 – VHS

+0

這似乎是你添加了一個額外的參數,並刪除了空白。有沒有另一種方法來實現這一點,而無需編輯原始方法?不知道我的教授是否以我們的任何方式修改它。 –

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; 
}