In: Computer Science
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
public int getNumPages() {…} //returns numPages of this Book
public void printBookInfo() {
System.out.print(title + “ written by “ + author);
}
}
1a) Write the default constructor for the Book class. Our default book will have the following values:
title: Surviving CSC330 in Times of Crisis
author: Richard Weir
edition: 1
numPages = 100
1b) Write the parameterized constructor for the Book class. All instance variables need to be assigned values.
1c) Declare an instance of the Book class called myBook. myBook can have any values you would like (that are appropriate!).
Program Code Screenshot
Sample Output
Program Code to Copy
class Book { private String title, author, edition; private int numPages; public Book() { title = "Surviving CSC330 in Times of Crisis"; author = "Richard Weir"; edition = "1"; numPages = 100; } //default public Book(String t, String a, String e, int np) { this.title = t; this.author = a; this.edition = e; this.numPages = np; } public String getTitle() { return title; } //returns the title of this Book public String getAuthor() { return author; } //returns the author of this Book public String getEdition() { return edition; } //returns the edition of this Book public int getNumPages() { return numPages; } //returns numPages of this Book public void printBookInfo() { System.out.print(title + " written by " + author); } } class Main{ public static void main(String[] args) { Book book = new Book("Harry Potter","J K Rowling","5",1298); book.printBookInfo(); } }