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

Instructions: Read Chapter 11 on Inheritance. Following the instructions below t

ID: 3712066 • Letter: I

Question

Instructions: Read Chapter 11 on Inheritance. Following the instructions below to create a NetBeans project. In this project, you are required to define a Product class and a Book class whereas the Book class is a subclass of the Product class. For each class, define the data fields, constructors, and methods according to the following UML description. Note Product -description: String -price: double +Product ( +Product(newCode: String, newDescription: String, newPrice: double) +getPrice0: double +setPrice(price: double): void +setCode(code: String): void +getDescription(): String +setDescription(description: String): void +toString(): Strin Book author: String +Book() +Book(newAuthor:String, newCode:String, newDescription String, newPrice: double) +getAuthor(): String +setAuthor(author: String): void +toString(): String Main method requirement .Create an instance of the Product class with a specific code, description, and price .Create an instance of the Book class with a specific author, code, description, and price .Use a print statement and the toString methods to display these two instances

Explanation / Answer

Product.java

public class Product {
//Declaring instance variables
private String description;
private String code;
private double price;

//Parameterized constructor
public Product(String description, String code, double price) {
this.description = description;
this.code = code;
this.price = price;
}

// getters and setters
public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Description=" + description + ", code=" + code + ", price=" + price;
}

}

________________

Book.java

public class Book extends Product {
//Declaring instance variables
private String newAuthor;

//Parameterized constructor
public Book(String description, String code, double price, String newAuthor) {
super(description, code, price);
this.newAuthor = newAuthor;
}


// getters and setters
public String getNewAuthor() {
return newAuthor;
}

public void setNewAuthor(String newAuthor) {
this.newAuthor = newAuthor;
}

//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString() + " New Author=" + newAuthor;
}

}

_________________

Test.java

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

//Declaring variables

String description;

String code;

double price;

String newAuthor;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

//Getting the input entered by the user

System.out.println(":: Product Details ::");

System.out.print("Enter Description :");

description=sc.nextLine();

System.out.print("Enter Code :");

code=sc.next();

System.out.print("Enter Price :$");

price=sc.nextDouble();

//Creating an instance of Product class

Product p=new Product(description, code, price);

System.out.println(":: Book Details ::");

sc.nextLine();

System.out.print("Enter Description :");

description=sc.nextLine();

System.out.print("Enter Code :");

code=sc.next();

sc.nextLine();

System.out.print("Enter New Author :");

newAuthor=sc.nextLine();

//Creating an instance of Book class

Book b=new Book(description, code, price, newAuthor);

System.out.println(" __Displaying Product Details__");

System.out.println(p);

System.out.println(" __Displaying Book Details__");

System.out.println(b);

}

}

__________________

Output:

:: Product Details ::
Enter Description :Mobile
Enter Code :6777
Enter Price :$900
:: Book Details ::
Enter Description :Database Systems
Enter Code :8988
Enter New Author :Srinivasan

__Displaying Product Details__
Description=Mobile, code=6777, price=900.0

__Displaying Book Details__
Description=Database Systems, code=8988, price=900.0 New Author=Srinivasan

_______________Thank You