Question: Write a Book class in a file named Book.java. Later, we will use the class to help us implement a library or patron. But for

Write a Book class in a file named Book.java. Later, we will use the class to help us implement a library or patron. But for this part, we're just working on books. This class will be the template or cookie cutter (as discussed in class) for book objects.

The class needs to have the following private 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 a book is currently borrowed (checked-out)

The class also needs 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(): returns the "state" of a book (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

TestBook.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 TestBook.java

other than to comment out portions as you work on your methods in

Book.java.

When your Book class is implemented (ie all 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 methodin

TestBook.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 of code and get that working in TestBook.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 TestBook.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.

public class TestBook { public static void main(String[] args) { //test out constructor Book b1 = new Book("The Hobbit", "J.R.R. Tolkien"); /* //test out accessor methods for title and author System.out.println("Title (should be The Hobbit): " + b1.getTitle() ); System.out.println("Author (should be J.R.R. Tolkien): " + b1.getAuthor() ); //test out borrowing mechanism System.out.println("Is borrowed? (should be false): " + b1.getBorrowed() ); b1.borrowBook(); System.out.println("Is borrowed? (should be true): " + b1.getBorrowed() ); //test out returning mechanism b1.returnBook(); System.out.println("Is borrowed? (should be false): " + b1.getBorrowed() );*/ } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!