Then copy the UseBook.java file from the class directory:
cp ~ cs17000a / share / hw5 / UseBook . java
The Assignment:
Write a Book class in a file named Book.java. Later, we will use the class to help up implement a library or
patron. But for this assignment, we’re just working on books. This class will be the template (as discussed
in class) for book objects.
The class needs to have the following instance variables to store the \state" of the book:
Title (String): the book’s title
Author (String): the book’s author
borrowed (boolean): indicates whether or not the book is currently borrowed (checked out).
The class also need to implement the following instance methods:
a constructor: takes two String arguments: the first is the title and the second is the author
borrowBook(): marks a book as borrowed
returnBook(): marks a book as NOT borrowed
getTitle(): returns the title of a book
getAuthor(): returns the author of a book
getBorrowed(): returns whether or not a book is currently borrowed
toString(): displays the book’s author, title and borrowed status in a nice format.
Spend some time thinking about these methods. What should they return? To help you out, take a look at
the UseBook.java file. This file has one method (main) which instantiates a book and then invokes methods
on that object to test its behavior. You should not change UseBook.java other than to comment out portions as you work on your methods in Book.java.
When your Book class is implemented (i.e. all of the instance variables and instance methods are working
correctly), the output of that program should be:
Title ( should be The Hobbit ): The Hobbit
Author ( should be J.R.R. Tolkien ): J.R.R. Tolkien
Borrowed ? ( should be false ): false
Borrowed ? ( should be true ): true
Borrowed ? ( should be false ): false
Notes and hints:
• You should get a small part working at a time. Start by commenting out the entire main method in
UseBook.java except for the first line. Since this line of code tests the constructor, you’ll need to
get the constructor working first. Then uncomment the next small section and get that working in
UseBook.java.
• Do NOT modify the main method in any substantial way.
• Don’t forget to implement the toString method and test it. this is the only method not tested in
UseBook.java, so you will need to add the line of code:
System . out . println (b1 );
to the main method. If your toString method is implemented correctly, the above line will print out
the information about the book.
Submitting:
Note: be sure to include yoru collaboration statement. It is also important to include comments in your
code. Failure to do either of these things will result in a point deduction.
Then from your cs170/hw5 directory, run the command:
/ home / cs17000a / turnin Book . java hw5
You will see a message containing \+++ALLOWED" if your submission was successful. If you had previously submitted, you will be asked to verify that you wish to overwrite a previous submission.
Be sure to submit Book.java, not UseBook.java.