Question: Book: - title: String - price: double +Book() +Book(String, double) +getTitle(): String +setTitle(String): void +getPrice(): double +setPrice(double): void +toString(): String The class has two attributes,

Book:

- title: String

- price: double

+Book()

+Book(String, double)

+getTitle(): String

+setTitle(String): void

+getPrice(): double

+setPrice(double): void

+toString(): String

The class has two attributes, title and price, and get/set methods for the two attributes. The first constructor doesnt have a parameter. It assigns to title and 0.0 to price; The second constructor uses passed-in parameters to initialize the two attributes. The toString() method returns values for the two attributes. Notation: - means private, and + means public.

1. Define the Book.java class.

2. define another class named BookApp.java which has a main() method.

3. create a Book object, named b1, by calling the first Book constructor. Then using the object b1 to call setTitle() and setPrice() methods to assign John Doe to title and 8.1 to price. using b1 to call toString() method and print the returned value from the toString() method. Create another Book object, named b2, by calling the second Book constructor and passing Ann Smith as the first argument and 9.2 as the second argument. Using b2 to call toString() method and print the returned value from the toString() method.

I need help with the second part of this question (the second part below).

Here's my code for the first part of this question(the one above) for reference.

Book.java:

public class Book { private String title; private double price; public Book() { title = ""; price = 0.0; } public Book(String title, double price) { this.title = title; this.price = price; } 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; } @Override public String toString() { return "title='" + title + '\'' + ", price=" + price; } } 

BookApp.java:

public class BookApp { public static void main(String[] args) { Book b1 = new Book(); b1.setTitle("John Doe"); b1.setPrice(8.1); System.out.println(b1.toString()); Book b2 = new Book("Ann Smith", 9.2); System.out.println(b2.toString()); } }

(I need help with this part of the question) Reuse Book.java as parent class(the one above), refer to the following requirements and UML diagram,

The first constructor, TextBook(), doesnt have a parameter. It assigns to title, 0.0 to price, 0 to courseID;

The second constructor, TextBook(String, double, int) uses three passed-in parameters to initialize the three attributes.

toString() in TextBook returns a string value for the three attributes, title, price, and courseID.

Book

- title: String

- price: double

+ Book()

+ Book(String, double)

+ getTitle(): String

+ setTitle(String): void

+ getPrice(): double

+ setPrice(double): void

+ toString(): String

^

|

TextBook - courseID: int

+ TextBook()

+ TextBook(String, double, int)

+ getCourseID(): int

+ setCourseID(int): void

+ toString(): String

Questions:

1) Define a child class, TextBook, based on above requirement.

2) Define another class, TextBookApp. This TextBookApp class interacts with the Book and TextBook classes as follows:

2.1) Create a Book object, named b, by calling the second Book constructor and passing Ann Smith as the first argument and 9.2 as the second argument. Use b to call toString() method and print the returned value from the toString() method.

2.2) Create a TextBook object, named tb1, by calling the first TextBook constructor. Then using the object tb1 to call methods to assign John Doe to title, 8.1 to price, and 2050 to courseID. Finally, using tb1 to call toString() method and print out the returned value.

2.3) Create another TextBook object, named tb2, by calling the second TextBook constructor and passing Ann Smith as the first argument, 9.2 as the second argument, and 3090 as the third parameter. Use tb2 to call toString() method and print out the returned value.

2.4) Run TextBookApp to make sure it works correctly.

In Java (Netbean IDE 8.2)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!