Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the following tasks: a. Design a class named Book that holds a stock nu

ID: 666795 • Letter: C

Question

Complete the following tasks:

a. Design a class named Book that holds a stock number, author, title, price, andnumber of pages. Include methods to set and get the values for each data field.Also include a displayInfo() method that displays each of the Book ’ s datafields with explanations.

b. Design a class named TextBook that is a child class of Book . Include a new datafield for the grade level of the book. Override the Book class displayInfo() method to accommodate the new grade-level field.

c. Design an application that instantiates an object of each type and demonstratesall the methods.

Explanation / Answer

public class Book {
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(int a)
{pages=a;
}
public void setPrice(double a)
{price=a;
}
}

-------------------------------------

import java.util.*;
public class BookShelf
{
public static void main(String[] args)
{Scanner input = new Scanner(System.in);
Book b1=new Book();
Book b2=new Book();
getdata(b1,input);
getdata(b2,input);
System.out.println("stock number author title price pages");

printdata(b1);
printdata(b2);


}
public static void getdata(Book b,Scanner input)
{String title,author;
double price;
int pages,stockNumber;

System.out.print("Enter stock Number: ");
stockNumber=input.nextInt();
input.nextLine();
b.setStockNumber(stockNumber);
System.out.print("Enter title: ");
title=input.nextLine();
b.setTitle(title);
System.out.print("Enter author: ");
author=input.nextLine();
b.setAuthor(author);
System.out.print("Enter price: ");
price=input.nextDouble();
b.setPrice(price);
System.out.print("Enter number of pages: ");
pages=input.nextInt();
b.setPages(pages);
}
public static void printdata(Book b)
{String title,author;
double price;
int pages,stockNumber;

title=b.getTitle();
stockNumber=b.getStockNumber();
author=b.getAuthor();
price=b.getPrice();
pages=b.getPages();
System.out.println(stockNumber+" "+author+" "+title+" $"+price+" "+pages);
}
}