In: Computer Science
To be done in Java
Consider the partially complete book class given. Make the following additions to the book class.
/**
* 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;
}
}
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: