Question

In: Computer Science

To be done in Java Consider the partially complete book class given.    Make the following...

To be done in Java

Consider the partially complete book class given.    Make the following additions to the book class.

  • Add a getAuthor() and a getTitle() method to the book class. They should take no parameters and should return a String.
  • Change the 3-parameter constructor for the class so that it correctly initializes the member variables.
  • Write an accessor method called getPages().
  • Add a zero parameter getDetails() method to the Book class. It should assemble the information for a book as a single string that is returned by the method. Format the string that is returned exactly as follows, with no leading or trailing spaces or hard returns, where the words inside the brackets represent the data from the object. Do not include the square brackets in the string.
  • [title] by [author]([pages]pp)

/**

* A class that maintains information on a book.

* This might form part of a larger application such

* as a library system, for instance.

* @author (Insert your name here.)

* @version (Insert today's date here.)

*/

public class Book

{

    // The fields.

    private String author;

    private String title;

    private int pages;

    /**

    * Zero parameter constructor calls the 3-parameter constructor with default values

    * when this object is constructed

    */

    public Book()

    {

        this("n/a", "n/a",0);

    }

    /**

     * Set the author and title fields when this object

     * is constructed.

     */

    public Book(String bookAuthor, String bookTitle, int numPages)

    {

        author = bookAuthor;

        title = bookTitle;

    }

}

Solutions

Expert Solution

Explanation:I have added the code as mentioned and have provided a Demo class and have also shown the output by using the main method, please find the image attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation.

//code

/**
*
* A class that maintains information on a book.
*
* This might form part of a larger application such
*
* as a library system, for instance.
*
* @author (Insert your name here.)
*
* @version (Insert today's date here.)
*
*/
public class Book
{

   // The fields.

   private String author;
   private String title;
   private int pages;

   /**
   *
   * Zero parameter constructor calls the 3-parameter constructor with default
   * values
   *
   * when this object is constructed
   *
   */
   public Book()
   {
       this("n/a", "n/a", 0);
   }

   /**
   *
   * Set the author and title fields when this object
   *
   * is constructed.
   *
   */
   public Book(String bookAuthor, String bookTitle, int numPages)
   {
       author = bookAuthor;
       title = bookTitle;
       pages = numPages;
   }

   /**
   * accessor method that returns the author of the book
   *
   */
   public String getAuthor()
   {
       return author;
   }

   /**
   * accessor method that returns the title of the book
   *
   */
   public String getTitle()
   {
       return title;
   }

   /**
   * accessor method that returns the number of pages of the book
   *
   */
   public int getPages()
   {
       return pages;
   }

   /**
   * returns the details of book
   *
   */
   public String getDetails()
   {
       return title + " by " + author + "(" + pages + "pp)";
   }
}

//Demo class

public class Demo
{

   public static void main(String[] args) throws InterruptedException
   {
       Book book = new Book("Mr.Roy", "Title of book", 400);
       System.out.println(book.getDetails());
   }
}

Output:


Related Solutions

Consider a class Book that contains information about a Book. The class should has the following...
Consider a class Book that contains information about a Book. The class should has the following attributes: • The title of the book • The author of the book • Year of publication (e.g. 1994) • The number of people that have rated this book as a 1 (Terrible) • The number of people that have rated this book as a 2 (Bad) • The number of people that have rated this book as a 3 (OK) • The number...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {   ...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {    private String title, author, edition;    private int numPages;    public Book { //part (a) } //default    public Book(String t, String a, String e, int np) { //part (b) }    public String getTitle() {…} //returns the title of this Book    public String getAuthor() {…} //returns the author of this Book    public String getEdition() {…} //returns the edition of this Book...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {   ...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {    private String title, author, edition;    private int numPages;    public Book { //part (a) } //default        public Book(String t, String a, String e, int np) { //part (b) }    public String getTitle() {…} //returns the title of this Book    public String getAuthor() {…} //returns the author of this Book    public String getEdition() {…} //returns the edition of this...
I need this done in JAVA. Define a class named Cash. The class contains the following...
I need this done in JAVA. Define a class named Cash. The class contains the following public elements: A Double stored property that contains the amount of money (dollars and cents) described by an object of the class. A read-only, computed property. It calculates and returns the minimum number of U.S. bills and coins that add up to the amount in the stored property.  The return value is an Int array of length 9 that contains (beginning with index 0 of...
Suppose you are given the following partially complete table for the coming month. You have a...
Suppose you are given the following partially complete table for the coming month. You have a meeting with the chief financial officer in fifteen minutes and he is expecting this information in its entirety. Note: all labor units are paid equally and labor is the firm’s only variable input.                  a) Labor Q Fixed Cost Variable Cost Total Cost 0 0 $0 1 5,500 2 8,500 3 9,000 4 9,200 $1,000 5 9,000 $1,250          b) Suppose the marginal resource...
Suppose you are given the following partially complete table for the coming month. You ​have a...
Suppose you are given the following partially complete table for the coming month. You ​have a meeting with the chief financial officer in fifteen minutes and he is expecting this completed ​information. Note: Labor units are evenly and equally paid. The only varible input is labor. ​ Labor Q Fixed Cost Variable Cost Total Cost _________________________________________________________________ 0 0 ______ $0 ______ 1 5,500 ______ ______ ______ 2 8,500 ______ ______ ______ 3 9,000 ______ ______ ______ 4 9,200 ______ $1,000...
Suppose you are given the following partially complete table. You have a meeting with the chief...
Suppose you are given the following partially complete table. You have a meeting with the chief financial officer in fifteen minutes and he is expecting this information in its entirety. Note: all labor units are paid equally and labor is the firm’s only variable input.                   Labor               Q              Fixed Cost       Variable Cost              Total Cost                                =========================================================                       0                      0                      ______                        $0                    ______                        1                      1,500               ______                        ______            ______             2                      4,500               ______                        ______            ______             3                      6,000               ______                        ______            ______...
TO BE DONE IN JAvA Complete the calculator program so that it prompts the user to...
TO BE DONE IN JAvA Complete the calculator program so that it prompts the user to enter two integers and an operation and then outputs the answer. Prompt the user three times, don't expect all the inputs on one line. Possible operations are + - * / No other words or symbols should be output, just the answer. Requested files import java.util.Scanner; /** * This is a simple calculator Java program. * It can be used to calculate some simple...
to be done in java Your task is to finish the StockItem class so that it...
to be done in java Your task is to finish the StockItem class so that it meets the following criteria • The StockItem class will have 4 attributes: a stock number; a name; the price of the item; the total number of items currently in stock • The first three of the above characteristics will need to be set at the time a StockItem object is created, with the total number of items set to 0 at this time. The...
4. Suppose you are given the following partially complete table for the coming month. You have...
4. Suppose you are given the following partially complete table for the coming month. You have a meeting with the chief financial officer in fifteen minutes and he is expecting this information in its entirety. Note: all labor units are paid equally and labor is the firm’s only variable input.                  a) Labor Q Fixed Cost Variable Cost Total Cost 0 0 $0 1 5,500 2 8,500 3 9,000 4 9,200 $1,000 5 9,000 $1,250          b) Suppose the marginal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT