Question: For this lab, you will write a class that represents a book. The Book class will support the following hidden state: title as a Java

For this lab, you will write a class that represents a book. The Book class will support the following hidden state:
title as a Java String
author(s) as an ArrayList of String
ISBN as a Java String
number of pages as an integer
retail price as a double
publisher as a Java String
Your Book class must support the following interface:
public Book(String title, String isbn, int np, double rp, String publisher, String ... authors)
public String getTitle()
public String getIsbn()
public int getNumberOfPages()
public double getRetailPrice()
public String [] getAuthors()
public String toString()
For example, the Java statement statements:
Book b = new Book("Java How to Program",
"978-0131016217",
1400,19.97, "Prentice-Hall",
"Harvey M. Deitel", "Paul J. Deitel" );
System.out.printf("%s%n", b);
should produce the following output:
That is, the toString() should produce a string that has the book object state in this order
title
authors (comma separated with space after the comma inside []
number of pages
retail price
publisher
ISBn
inside <>.
If the constructor receives a null value for any of the String state, that state should be set to "Unknown". If no authors are given, the ArrayList should contain a single string "Unknown".
For example, the Java statement:
Book b = new Book(null, null, 5,20.10, null);
System.out.printf("%s%n", b);
should produce the output
import java.util.ArrayList;
import java.util.Arrays;
public class Book {
// Private fields for the book's state
private String title;
private String isbn;
private int numberOfPages;
private double retailPrice;
private String publisher;
private ArrayList authors;
// Constructor
public Book(String title, String isbn, int np, double rp, String publisher, String... authors){
this.title =(title != null)? title : "Unknown";
this.isbn =(isbn != null)? isbn : "Unknown";
this.numberOfPages = np;
this.retailPrice = rp;
this.publisher =(publisher != null)? publisher : "Unknown";
// Handling authors, default to "Unknown" if no authors are provided
if (authors != null && authors.length >0){
this.authors = new ArrayList<>(Arrays.asList(authors));
} else {
this.authors = new ArrayList<>();
this.authors.add("Unknown");
}
}
// Getters
public String getTitle(){
return title;
}
public String getIsbn(){
return isbn;
}
public int getNumberOfPages(){
return numberOfPages;
}
public double getRetailPrice(){
return retailPrice;
}
public String[] getAuthors(){
return authors.toArray(new String[0]);
}
// toString method
@Override
public String toString(){
return String.format("

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 Programming Questions!