2014-02-25 33 views
1
public class Book { 
String title; 
boolean borrowed; 
// Creates a new Book 
public Book(String bookTitle){ 
    bookTitle= "The Da Vinci Code"; 
} 

// Marks the book as rented 
public void borrowed() { 


} 

// Marks the book as not rented 

public void returned() { 


} 

基本功課如何使方法我不得不做出一本書類,這些都是方法是,我不知道如何來填補一部分。我找不出如何使方法將本書標記爲借閱並返回,以便我可以將它們用於布爾方法,因爲我想自己找出其餘部分,所以我沒有發佈。該標籤的對象

+0

當有人叫借,你應該改變你的類的內部結構,也許表示,這本書是借來的屬性, – nachokk

+1

@GlaucoNeves不要粗魯..你不好笑,你是不是幫助,你的評論是不需要的 – nachokk

+0

你還需要一種方法來告訴書借用何時只是返回'借來的'變量的值... – MadProgrammer

回答

0

後面所有這一切的想法是,一種方法可修改一個對象的內部結構,

傳遞對象的狀態到另一個新的狀態。

例子:

public class Book{ 

private boolean isRented; 

public void borrow(){ 
    isRented = true; // you change your internal structure and in the new state is borrowed 
} 

public void returned(){ 
    isRented = false; // the same here 
} 

} 

現在:

public static void main(String args []){ 
    //create a new book 
    Book book = new Book(); 

    //rent the book 
    book.borrow(); 
    //now i want to return 
    book.returned(); 

} 

現在,如果你想提供如果一本書isRented()返回一個布爾方法發生什麼事?如果你能想象自己,那麼你就明白這一點。

+0

那主要部分是完全不同的。假設獲得不同的輸出結果,但是要感謝第一個代碼。它幫助很多 – user3296193

0

嘗試一個數組列表,添加/刪除人員簽出/退回書籍。爲每本書打上號碼。

這個論壇是不做功課的。如果你想在家庭作業上尋求幫助,你需要提出一個具體的問題,而不是:我不知道該怎麼做,給我一些代碼。請閱讀你的書和學習,它只會幫助你。

+1

這應該是一個評論不是答案...你只是解釋了有題 !! – zee

0
public class Book { 
    private boolean isOut; 

    ... 

    public setBorrowed(boolean is_out) { 
     isOut = is_out; 
    } 

    public isBorrowed() { 
     return isOut; 
    } 
} 

那麼你可能會做

Book bookIt = new Book("It by Stephen King"); 
bookIt.setBorrowed(true); //Taken out of the library. 
0

你應該使用帶有布爾標記的數據類型的索引存儲與否書創造通過數組的你有書籍數組,然後循環租用。 然後根據索引值打印消息。

int rentedAtIndex = -1; 

for(int i = 0; i < bookObj.length; i++) { 
    if(bookObj[i].getName().equals(input)) { 

     rentedAtIndex = i; // Store the index for a future reference 
     break;    // break if the condition is met 
    } 
    } 
     if(rentedAtIndex >= 0) 
      System.out.println("The Book is Avavailbe for borrwoing !"); 
     else 
      System.out.println("The Book Is rented, Please try some other time!"); 
} 
+0

我只爲一本書做它,它應該教授課程。我知道如何去做大部分工作,問題在於製作一種方法,將書籍標記爲返回並借閱。我的老師在這方面確實很模糊。 – user3296193