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

At this point, students should create new classes ProductPart5 and ProductTester

ID: 3755104 • Letter: A

Question

At this point, students should create new classes ProductPart5 and ProductTesterPart5 that will add onto the functionality of the project. (copy and paste part 4 code into new part 5 classes) Topic(s): Adding a subclass, using extends, using super(), overriding methods from a superclass.

1. Create a subclass of the ProductPart5 class that has two additional variables. (For example, a DVD subclass could use movie title and length).

2. In the subclass, override the method to calculate the value of the inventory of a product with the same name as that method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.

3. Override the toString() method from the ProductPart5 class so that all information about new subclass objects can be printed to the output.

4. Modify ProductTesterPart5 so that an array of objects of the new subclass can be created from user input. Display the subclass products using a for loop.

Here is the code:

package javaInventory;

public class ProductPart5 {

private int itemnumber; //unique value for identification

private String name; //name of the product

private int qtyinstock; //quantity in stock

private double price; //price of the product

//default constructor which initializes instance variables

//numeric values are 0 (zero) and String values are "" (null)

public ProductPart5()

{

itemnumber = 0;

name = "";

qtyinstock = 0;

price = 0;

}

//overload the constructor to allow setting values for Products

public ProductPart5(int i, String n, int q, double p)

{

itemnumber = i;

name = n;

qtyinstock = q;

price = p;

}

//item number setter

public void setItemNumber(int i)

{

itemnumber = i;

}

//name setter

public void setName(String n)

{

name = n;

}

//quantity in stock setter

public void setQtyInStock(int q)

{

qtyinstock = q;

}

//price setter

public void setPrice(int p)

{

price = p;

}

//add a quantity to qtyinstock

//receiving a shipment

public void addToInventory(int q)

{

qtyinstock += q;

}

//subtract a quantity from qtyinstock

//sales

public void deductFromInventory(int q)

{

qtyinstock -= q;

}

//itemnumber getter

public int getItemNumber()

{

return itemnumber;

}

//name getter

public String getName()

{

return name;

}

//quantityinstock getter

public int getQtyInStock()

{

return qtyinstock;

}

//price getter

public double getPrice()

{

return price;

}

//get total value of inventory for this Product

public double getInventoryValue()

{

return price * qtyinstock;

}

//override toString() Method from the Object class

//to allow display of each object to the console

public String toString()

{

return " Item Number: " + getItemNumber() + " Name: " + getName() +

" Inventory: " + getQtyInStock() + " Price: " +

getPrice() + " Inventory Value for this product: " + getInventoryValue();

}

}

Explanation / Answer

import java.util.Scanner;

class ProductPart5 {
private int itemnumber; //unique value for identification
private String name; //name of the product
private int qtyinstock; //quantity in stock
private double price; //price of the product
//default constructor which initializes instance variables
//numeric values are 0 (zero) and String values are "" (null)
public ProductPart5()
{
itemnumber = 0;
name = "";
qtyinstock = 0;
price = 0;
}
//overload the constructor to allow setting values for Products
public ProductPart5(int i, String n, int q, double p)
{
itemnumber = i;
name = n;
qtyinstock = q;
price = p;
}
//item number setter
public void setItemNumber(int i)
{
itemnumber = i;
}
//name setter
public void setName(String n)
{
name = n;
}
//quantity in stock setter
public void setQtyInStock(int q)
{
qtyinstock = q;
}
//price setter
public void setPrice(int p)
{
price = p;
}
//add a quantity to qtyinstock
//receiving a shipment
public void addToInventory(int q)
{
qtyinstock += q;
}
//subtract a quantity from qtyinstock
//sales
public void deductFromInventory(int q)
{
qtyinstock -= q;
}
//itemnumber getter
public int getItemNumber()
{
return itemnumber;
}
//name getter
public String getName()
{
return name;
}
//quantityinstock getter
public int getQtyInStock()
{
return qtyinstock;
}
//price getter
public double getPrice()
{
return price;
}
//get total value of inventory for this Product
public double getInventoryValue()
{
return price * qtyinstock;
}
//override toString() Method from the Object class
//to allow display of each object to the console
public String toString()
{
return " Item Number: " + getItemNumber() + " Name: " + getName() +
" Inventory: " + getQtyInStock() + " Price: " +
getPrice() + " Inventory Value for this product: " + getInventoryValue();
}
}

class DVD extends ProductPart5 // subclass of ProductPart5
{
private String movieTitle;
private double length;

public DVD(int i, String n, int q, double p,String mt,double l)
{
super(i,n,q,p); // call to base class constructor
movieTitle = mt;
length = l;
}
public double getInventoryValue()   // override getInventoryValue() method
{
return getPrice() * getQtyInStock()*(1+0.05); // add 5% restocking fees
}
//override toString() Method from the Object class
//to allow display of each DVD object to the console
public String toString()
{
return super.toString() + " DVD Movie Title : "+movieTitle +" Length in hours : "+length;
}

}
class ProductTesterPart5
{
public static void main (String[] args)
{

DVD[] dvd = new DVD[5];

Scanner input = new Scanner(System.in);

for(int i=0;i<5;i++)
{
System.out.println("Enter Item Number: ");
int item = input.nextInt();
System.out.println(" Enter Name: " );
String n = input.next();
System.out.println(" Enter Inventory value for this product : ");
int v = input.nextInt();
System.out.println(" Enter Price: " );
double p = input.nextDouble();
System.out.println(" Enter movie Title : ");
String t = input.next();
System.out.println("Enter length of movie in hours : ");
double l = input.nextDouble();
dvd[i] = new DVD(item,n,v,p,t,l);
}

for(int i=0;i<5;i++)
System.out.println(dvd[i]);

}
}

Output:

Item Number: 1001
Name: DVD
Inventory: 56
Price: 4.55
Inventory Value for this product: 267.54
DVD Movie Title : Incredibles2
Length in hours : 2.15


Item Number: 1002
Name: DVD
Inventory: 32
Price: 3.55
Inventory Value for this product: 119.28
DVD Movie Title : Cinderella
Length in hours : 1.5


Item Number: 1003
Name: DVD
Inventory: 102
Price: 5.88
Inventory Value for this product: 629.748
DVD Movie Title : Gladiator
Length in hours : 2.2


Item Number: 1004
Name: DVD
Inventory: 24
Price: 3.25
Inventory Value for this product: 81.9
DVD Movie Title : Frozen
Length in hours : 1.43


Item Number: 1005
Name: DVD
Inventory: 67
Price: 1.55
Inventory Value for this product: 109.04250000000002
DVD Movie Title : Cars
Length in hours : 2.0

DO ask if any doubt. Please upvote.