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

1.) Design a class named Book that holds a stock number, author, title, price, a

ID: 3643931 • Letter: 1

Question

1.) Design a class named Book that holds a stock number, author, title, price, and number of pages for a book. Include a constructor that prompts the user for the data and a toString method. Also include methods to set and get the values for each data field.

2.)Design a class named TextBook that is a child class of Book. Include a new data field for the grade level of the book. Include a constructor that calls the parent constructor before prompting for the grade level and include a toString method that also calls the parent toString mehod. Add get and set methods for the new field.

3.)Design an application that declares at least one Book objects and one TextBook object book and sets and displays their values.

Explanation / Answer

Here are the 3 classes. The important concept for this one is inheritance and the use of "super" which allows a child class to call methods of the parent class, including the constructor. Book.java public class Book { int stockNumber; String auth; String title; double price; int pageCount; public String toString() { return "Stock: " + stockNumber + ", Author: " + auth + ", Title: " + title + ", Price: $" + price + ". " + pageCount + " pages."; } public Book(){} public Book(int SN, String auth, String title, double price, int pageCount) { this.stockNumber = SN; this.auth = auth; this.title = title; this.price = price; this.pageCount = pageCount; } public int getStockNumber() { return stockNumber; } public void setStockNumber(int stockNumber) { this.stockNumber = stockNumber; } public String getAuth() { return auth; } public void setAuth(String auth) { this.auth = auth; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; }; } TextBook.java public class TextBook extends Book{ int gradeLevel; TextBook() {}; public TextBook(int SN, String auth, String title, double price, int pageCount, int gradeLevel) { super(SN, auth, title, price, pageCount); this.gradeLevel = gradeLevel; } public String toString() { return (super.toString() + " Grade Level: " + gradeLevel); } public int getGradeLevel() { return gradeLevel; } public void setGradeLevel(int gradeLevel) { this.gradeLevel = gradeLevel; } } and BookDriver.java public class BookDriver { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Book book = new Book(1, "Orson Scott Card", "Enders Game", 11.99, 353); TextBook tbook = new TextBook(2, "Pearson Publishing", "Introduction to Geometry", 42.99, 450, 6); System.out.println(book.toString()); System.out.println(tbook.toString()); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote