Use your completed book-exercise project from BlueJ. Add another new class, Medi
ID: 3667804 • Letter: U
Question
Use your completed book-exercise project from BlueJ. Add another new class, MediaDemo to your Book exercise. • Add a field to your class for a Book object. • Write a private method that will build a book object with all the fields populated, and return that book object. • Write a constructor for your MediaDemo class that will initialize the Book field by calling the private method you just wrote. • Add a new method, testBook, that will print out the initial state of your Book field, then change values for the book’s fields – print out what you are changing, and print the final state of your book object. • Run your method, testBook and take a screen shot of the results. This is a review assignment, and I guess I am so lost on where to start. I completed a book exercise That I have attached below:
class Book
{
// The fields.
private String author;
private String title;
private int pages;
private int borrowed;
private String refNumber;
private boolean courseText;
/**
* Set the author and title fields when this object
* is constructed. */
public Book(String bookAuthor, String bookTitle,int bookPages, String refNumber, boolean immutable)
{
author = bookAuthor;
title = bookTitle;
pages = bookPages;
refNumber = "";
courseText = immutable;
}
/**
* Return the name of the author
*/
public String getAuthor()
{
return author;
}
/**
* Return the title of the book
*/
public String getTitle()
{
return title;
}
/**
* Print the name of the author
*/
public void printAuthor()
{
System.out.println ("The Author is " + author);
}
/**
* Print the title of the book
*/
public void printTitle()
{
System.out.println ("The Title is " + title);
}
/**
* Return the number of pages in the book
*/
public int getPages()
{
return pages;
}
/**
* Set the reference number and get it
*/
public void SetRefNumber(String ref)
{
if(ref.length()>=3)
{
refNumber = ref;
}
else
{
System.out.println("Sorry, Reference number needs to be at least 3 characters.");
}
}
/**
* Get reference number and return value
*/
public String getRefNumber()
{
return refNumber;
}
/**
* This shows how many times the book has been borrowed
*/
public void borrow()
{
borrowed = borrowed + 1;
}
/**
* Get the number of times the book was borrowed
*/
public int getBorrowed()
{
// put your code here
return borrowed;
}
/**
*Accessor method which returns the value
*/
public boolean isCourseText()
{
// put your code here
return courseText;
}
public void printDetails()
{
{
System.out.println ("Title: " +title);
System.out.println ("Author: " + author);
System.out.println ("Number of Pages: " +pages);
System.out.println ("This book has been borrowed: " +borrowed+ " times.");
System.out.println("Reference number: " + refNumber);
if (refNumber.length() > 0)
{
System.out.println("Reference number: " + refNumber);
}
else
{
System.out.println("Reference number: ZZZ");
}
}
}
}
Explanation / Answer
class Book
{
// The fields.
private String author;
private String title;
private int pages;
private int borrowed;
private String refNumber;
private boolean courseText;
/**
* Set the author and title fields when this object
* is constructed. */
public Book(String bookAuthor, String bookTitle,int bookPages, String refNumber, boolean immutable)
{
author = bookAuthor;
title = bookTitle;
pages = bookPages;
this.refNumber = refNumber;
courseText = immutable;
}
/**Default constructo to set default values*/
public Book()
{
author = "";
title = "";
pages = 0;
refNumber = "";
courseText = false;
}
/**
* Return the name of the author
*/
public String getAuthor()
{
return author;
}
/**
* Return the title of the book
*/
public String getTitle()
{
return title;
}
/**
* Print the name of the author
*/
public void printAuthor()
{
System.out.println ("The Author is " + author);
}
/**
* Print the title of the book
*/
public void printTitle()
{
System.out.println ("The Title is " + title);
}
/**
* Return the number of pages in the book
*/
public int getPages()
{
return pages;
}
/**
* Set the reference number and get it
*/
public void SetRefNumber(String ref)
{
if(ref.length()>=3)
{
refNumber = ref;
}
else
{
System.out.println("Sorry, Reference number needs to be at least 3 characters.");
}
}
/**
* Get reference number and return value
*/
public String getRefNumber()
{
return refNumber;
}
/**
* This shows how many times the book has been borrowed
*/
public void borrow()
{
borrowed = borrowed + 1;
}
/**
* Get the number of times the book was borrowed
*/
public int getBorrowed()
{
// put your code here
return borrowed;
}
/**
*Accessor method which returns the value
*/
public boolean isCourseText()
{
// put your code here
return courseText;
}
public void printDetails()
{
{
System.out.println ("Title: " +title);
System.out.println ("Author: " + author);
System.out.println ("Number of Pages: " +pages);
System.out.println ("This book has been borrowed: " +borrowed+ " times.");
System.out.println("Reference number: " + refNumber);
if (refNumber.length() > 0)
{
System.out.println("Reference number: " + refNumber);
}
else
{
System.out.println("Reference number: ZZZ");
}
}
}
}
------------------------------------------------------------------------------------------------------------
/**The java program MediaDemo that demonstrates the Book class.
* Then prompts user to enter the values for attributes and
* print the old and new changed values of Book object.*/
//MediaDemo.java
import java.util.Scanner;
public class MediaDemo
{
//Create a static book variable
private static Book book;
//Constructor that prompts book details
public MediaDemo()
{
Scanner scanner=new Scanner(System.in);
System.out.println("Enter Book Author");
String bookAuthor=scanner.nextLine();
System.out.println("Enter Book Title");
String bookTitle=scanner.nextLine();
System.out.println("Enter number of pages");
int bookPages=Integer.parseInt(scanner.nextLine());
System.out.println("Enter reference number");
String refNumber=scanner.nextLine();
boolean immutable=true;
//Call the method createBookObject
book= createBookObject(bookAuthor, bookTitle, bookPages, refNumber, immutable);
}
public static void main(String[] args)
{
//create a defautl constructor object
book=new Book();
//print initial state of the book object
book.printDetails();
//Create an object of MediaDemo
MediaDemo mediaDemo=new MediaDemo();
//call testBook method
testBook();
}
//create an object of user created attributes
public static Book createBookObject(String bookAuthor,
String bookTitle,int bookPages,
String refNumber, boolean immutable)
{
return new Book(bookAuthor, bookTitle, bookPages, refNumber, immutable);
}
//testBook method that calls the printDetails method to display
//new chaged fields of book object
public static void testBook()
{
book.printDetails();
}
}
---------------------------------------------------------
Sample Output:
Title:
Author:
Number of Pages: 0
This book has been borrowed: 0 times.
Reference number:
Reference number: ZZZ
Enter Book Author
APJ kalam
Enter Book Title
Indomitable Spirit
Enter number of pages
350
Enter reference number
123-345-789
Title: Indomitable Spirit
Author: APJ kalam
Number of Pages: 350
This book has been borrowed: 0 times.
Reference number: 123-345-789
Reference number: 123-345-789
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.