I need to: a. Design a class named TextBook that is a child class of Book. Inclu
ID: 3640279 • Letter: I
Question
I need to:a. Design a class named TextBook that is a child class of Book. Include a new data field for the grade level of the book and get and set methods for the new field.
b. Design an application that instantiates an object of each class (book and textbook) and demonstrates all the methods.
Here is the original Book class:
public class Book {
public static void main(String[] args) {
}
private int stockNumber;
private String author;
private String title;
private double price;
private int pages;
public Book( int s,String a,String t,double p, int g)
{
stockNumber=s;
author=a;
title=t;
price=p;
pages=g;
}
public Book( )
{
}
public String getAuthor( ) {
return author;
}
public String getTitle( ) {
return title;
}
public int getStockNumber( ) {
return stockNumber;
}
public double getPrice( ) {
return price;
}
public int getPages( ) {
return pages;
}
public void setAuthor(String a)
{author=a;
}
public void setTitle(String a)
{title=a;
}
public void setStockNumber(int a)
{stockNumber=a;
}
public void setPages(double a)
{price=a;
}
public void setPrice(int a)
{pages=a;
}
}
Explanation / Answer
Here's the textbook: public class textBook extends Book{ int Grade; textBook( int s,String a,String t,double p, int g, int grade) { super(s, a, t, p, g); this.Grade = grade; } public int getGrade() { return Grade; } public void setGrade(int G) { this.Grade = G; } } And an example driver: public class BookMaker { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub textBook tb = new textBook(1, "John Smith", "My First Book", 9.99, 50, 5); System.out.println ("In Stock: " + tb.getStockNumber()); System.out.println ("Author: " + tb.getAuthor()); System.out.println ("Title: " + tb.getTitle()); System.out.println ("Price: " + tb.getPrice()); System.out.println ("Page Count: " + tb.getPages()); System.out.println ("Grade: " + tb.getGrade()); } }