We are now going to write a class to represent a LibraryBook. You will implement
ID: 645449 • Letter: W
Question
We are now going to write a class to represent a LibraryBook. You will implement the following API in your class:
Constructor
The constructor should set title and author based on the provided arguments, setcheckedOut to false, set daysUntilDue to 0, and set whoCheckedOut to the empty string.
checkOut(String whoCheckedOut)
The checkOut(String whoCheckedOut) method should first check to see if the book is already checked out by looking at the value of checkedOut.
-If the book is already checked out, return false to indicate the book could not successfully be checked out.
-If the book is not already checked out, set checkedOut to true, set daysUntilDue to 14 (two weeks), set whoCheckedOut to the given parameter, and return true to indicate the book was successfully checked out.
advanceDay()
The advanceDay() method should first check to see if the book is already checked out by looking at the value of checkedOut.
- If the book is checked out, the advanceDay() method should subtract one fromdaysUntilDue.
-If daysUntilDue is now 0, set checkedOut to false and whoCheckedOutto the empty string, and return true.
-If daysUntilDue is greater than 0, return false.
-If the book is not checked out, return false.
toString()
If the book is not checked out, the toString() method will return a string in the format
LibraryBook - title : String- author : String
- checkedOut : boolean
- daysUntilDue : int
- whoCheckedOut : String + LibraryBook(title: String, author: String)
+ setTitle(title: String) : void
+ getTitle() : String
+ setAuthor(author: String) : void
+ getAuthor() : String
+ isCheckedOut() : boolean
+ getDaysUntilDue() : int
+ getWhoCheckedOut() : String
+ checkOut(whoCheckedOut: String) : boolean
+ advanceDay() : boolean
+ toString() : String
Explanation / Answer
Answers:
import java.io.*;
import java.lang.String.*;
import java.util.*;
class LibraryBook
{
public String title;
public String author;
public boolean checkedOut;
public int daysUntilDue;
public String whoCheckedOut;
public String str;
LibraryBook(String title,String author)
{
this.title=title;
this.author=author;
checkedOut=false;
daysUntilDue=0;
whoCheckedOut=" ";
}
void setTitle(String title)
{
this.title=title;
}
public String getTitle()
{
return title;
}
public void setAuthor(String author)
{
this.author=author;
}
public String getAuthor()
{
return author;
}
public boolean isCheckedOut()
{
return checkedOut;
}
public int getDaysUntilDue()
{
return daysUntilDue;
}
public String getWhoCheckedOut()
{
return whoCheckedOut;
}
public boolean checkOut(String whoCheckedOut)
{
if(checkedOut==false)
{
checkedOut=true;
daysUntilDue=14;
return true;
}
else
return false;
}
public boolean advanceDay()
{
if(checkedOut==true)
{
daysUntilDue=daysUntilDue-1;
if(daysUntilDue==0)
{
checkedOut=false;
whoCheckedOut=" ";
return true;
}
}
return false;
}
public String toString()
{
if(checkedOut==false)
{
str=title+
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.