Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Book class in a file named Book.java. Later, we will use the class to he

ID: 3690342 • Letter: W

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

Explanation / Answer

//Bookprogram

import java.util.Scanner;

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

/**

*

* @author RAM

*/

public class Book {

    private String Title;

    private String Author;

    private boolean borrowed;

    public String getTitle() {

        return Title;

    }

    public void setTitle(String Title) {

        this.Title = Title;

    }

    public String getAuthor() {

        return Author;

    }

    public void setAuthor(String Author) {

        this.Author = Author;

    }

    public boolean isBorrowed() {

        return borrowed;

    }

    public void setBorrowed(boolean borrowed) {

        this.borrowed = borrowed;

    }

    //instance methods

    public void borrowBook(){

        System.out.println("Borrowed ?");

        Scanner s = new Scanner(System.in);

        boolean flag = s.nextBoolean();

        System.out.println("Borrowed "+flag);  

    }

    public void returnBook(){

        System.out.println("return Book");

    }

    public String toString(){

        String status ="True";

        return status;

    }

}

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package book;

/**

*

* @author RAM

*/

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() );*/

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote