using java and no scanners Part I Write a Book class in a file named Book.java.
ID: 3691419 • Letter: U
Question
using java and no scanners
Part I
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.
Part II - Implementing a Patron class:
Write a Patron class in a file named Patron.java. This class will utilize the Book class you wrote for Part I. This class models someone who can check books out of a library or book lending facility. For the purposes of this assignment, a Patron has a name and can have an unlimited number unique books checked-out at any given time. You should implement any instance variables as private.
The class also needs to implement the following instance methods:
a constructor: takes a String argument: the patron's first and last name
checkout(Book b): assigns a book to a specific patron and marks the book as borrowed
returnBook(Book b): unassigns a book from a patron and marks a book as NOT borrowed
getBooks(): returns an array of Book objects representing the books the patron currently haschecked out. The array should not contain any empty entries. In other words, if the patron has 2 bookschecked out, the array returned should be of length 2 and contain only 2 books.
toString(): displays information about the Patron, including name and books checked out.
Spend some time thinking about these methods. What should they return? What should they do in cases of weird input (for example, what happens if you try to return a book which a patron has never checked out? What happens if you try to check out a book twice?) To help you out, take a look at the TestPatron.java file. This file has one method (main) which instantiates several books and a patron and then invokes methods to test the behavior. You should not change TestPatron.java
other than to comment out portions as you work on your methods in Patron.java.
Notes and hints:
•Begin by implementing the instance variables you need. At minimum, you'll need a String to store the patron's name and a way to store Book objects.
•Do NOT modify the main method in any substantial way.
•Don't forget to implement the toString method and test it.
Explanation / Answer
Book.java
class Book
{
private String title;
private String author;
private boolean isBorrowed;
public Book(String bTitle,String bAuthor)
{
title=bTitle;
author=bAuthor;
isBorrowed=false;
}
public void borrowBook()
{
isBorrowed=true;
}
public void returnBook()
{
isBorrowed=false;
}
public String getTitle()
{
return title;
}
public String getAuthor()
{
return author;
}
public boolean getBorrowed()
{
return isBorrowed;
}
public String toString()
{
String s="Title: "+title+" "+"Author: "+author+" Borrowed: "+isBorrowed;
return s;
}
}
Patron.java
import java.util.*;
import java.io.*;
class Patron
{
private String name;
private ArrayList<Book> books;
public Patron(String n)
{
name=n;
books=new ArrayList<Book>();
}
public void checkout(Book b)
{
if(books.contains(b))
{
System.out.println("Book is already checkedout to you");
return;
}
books.add(b);
b.borrowBook();
}
public void returnBook(Book b)
{
if(books.contains(b))
{
books.remove(b);
b.returnBook();
}
else
{
System.out.println("No such book is checkedout by you");
}
}
public Book[] getBooks()
{
Book bookArray[]=books.toArray(new Book[books.size()]);
return bookArray;
}
}
output:
Should display 1 book (Hobbit):
[Title: The Hobbit
Author: J.R.R. Tolkien
Borrowed: true]
Book is already checkedout to you
Should still display 1 book (Hobbit):
[Title: The Hobbit
Author: J.R.R. Tolkien
Borrowed: true]
Should display 3 books: (Hobbit, Alas,Babylon, and I, Robot)
[Title: The Hobbit
Author: J.R.R. Tolkien
Borrowed: true, Title: Alas Babylon
Author: Pat Frank
Borrowed: true, Title: I Robot
Author: Isaac Assimov
Borrowed: true]
Should display 3 books (Hobbit, I,Robot, and Winnie the Pooh):
[Title: The Hobbit
Author: J.R.R. Tolkien
Borrowed: true, Title: I Robot
Author: Isaac Assimov
Borrowed: true, Title: Winnie The Pooh
Author: A.A. Milne
Borrowed: true]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.