Can i get help with this java question please? (2) The Perishableltem Class Crea
ID: 3791222 • Letter: C
Question
Can i get help with this java question please?
(2) The Perishableltem Class Create a subclass of Groceryltem called Perishableltem that represents an item that can spoil. Implement a public toString0 method that returns a String representation of the Perishableltem as follows (making sure to make full use of inheritance Smart-Ones Frozen Entrees weighing 0.311 with price 1.99 (perishable) Remove the perishable attribute from the Groceryltem class. You will need to modify the constructors (and delete one of them) as well as remove the get method. Adjust the ShopperTestProgram (from the last assignment) to make use of the Perishableltem class now for all items that were perishable. You will see a need to create a constructor in the Perishableltem class that makes ful use of inheritance. Adjust your unpackPerishables() method in the GroceryBag class so that it works property again by making proper use of inheritance. The method MUST return an array of Perishableltem objects now instead of Groceryltem objects.Explanation / Answer
2) Answer
//Base class GroceryItem
class GroceryItem
{
//Instance variables
float weight, price;
String name;
//Parameterized constructor
GroceryItem(String name, float weight, float price)
{
this.name = name;
this.weight = weight;
this.price = price;
}//End of constructor
//Method to return PerishableItem class object
static PerishableItem [] unpackPerishables()
{
//Creates an array of objects
PerishableItem gi[] = new PerishableItem[2];
//Initializes the object
gi[0] = new PerishableItem("Smart - Ones Frozen weighting", 0.311f, 1.99f);
gi[1] = new PerishableItem("Sealtest Milk ", 4.311f, 2.99f);
//Returns the array of object
return gi;
}//End of method
}//End of class
//Class PerishableItem derived from GroceryItem class
class PerishableItem extends GroceryItem
{
//Constructor for PerishableItem
PerishableItem(String name, float weight, float price)
{
//Calls the base class method
super(name, weight, price);
}//End of constructor
//Overrides toString() method
public String toString()
{
String msg = name + " " + weight + "kg with price $" + price + "(perishable)";
return msg;
}//End of method
}//End of class
//Driver class
public class ShopperTestProgram
{
//Main method
public static void main(String ss[])
{
//Declares an array for PerishableItem class
PerishableItem pi[];
//Calls the method unpackPerishables() and stores the return array of objects
pi = GroceryItem.unpackPerishables();
//Loops through all the objects and displays the information
for(int x = 0; x < 2; x++)
System.out.println(pi[x]);
}//End of main
}//End of class
Output
Smart - Ones Frozen weighting 0.311kg with price $1.99(perishable)
Sealtest Milk 4.311kg with price $2.99(perishable)
3) Answer
//Base class GroceryItem
class GroceryItem
{
//Instance variables
float weight, price;
String name;
//Parameterized constructor
GroceryItem(String name, float weight, float price)
{
this.name = name;
this.weight = weight;
this.price = price;
}//End of constructor
//Method to return PerishableItem class object
static GroceryItem [] unpackPerishables()
{
//Creates an array of objects
GroceryItem ri[] = new GroceryItem[5];
//Initializes the object for FreezerItem
ri[0] = new FreezerItem("Smart - Ones Frozen weighting", 0.311f, 1.99f);
//Initializes the objects for RefigeratorItem
ri[1] = new RefigeratorItem("Sealtest Milk ", 4.311f, 2.99f);
ri[2] = new RefigeratorItem("2L Sealtest Milk ", 2.99f, 2.06f);
ri[3] = new RefigeratorItem("Extra - Large Eggs ", 1.79f, 0.77f);
ri[4] = new RefigeratorItem("Yoplait Yogurt 6 - Pack ", 4.74f, 1.02f);
//Returns the array of object
return ri;
}//End of method
}//End of class
//abstract class PerishableItem derived from GroceryItem class
abstract class PerishableItem extends GroceryItem
{
//Constructor for PerishableItem
PerishableItem(String name, float weight, float price)
{
//Calls the base class method
super(name, weight, price);
}//End of constructor
//Overrides toString() method
abstract public String toString();
}//End of class
//Class RegigeratorItem derived from PerishableItem abstract class
class RefigeratorItem extends PerishableItem
{
//Parameterized constructor
RefigeratorItem(String name, float weight, float price)
{
//Calls base class constructor
super(name, weight, price);
}//End of constructor
//Overrides toString() method
public String toString()
{
String msg = name + " " + weight + "kg with price $" + price + "(perishable) " + this.getClass().getSimpleName();
return msg;
}//End of method
}//End of class
//Class FreezerItem derived from PerishableItem abstract class
class FreezerItem extends PerishableItem
{
//Parameterized constructor
FreezerItem (String name, float weight, float price)
{
//Calls base class constructor
super(name, weight, price);
}//End of constructor
//Overrides toString() method
public String toString()
{
String msg = name + " " + weight + "kg with price $" + price + "(perishable) " + this.getClass().getSimpleName();
return msg;
}//End of method
}//End of class
//Driver class
public class ShopperTestProgram
{
//Main method
public static void main(String ss[])
{
//Declares an array for GroceryItem class
GroceryItem pi[];
//Calls the method unpackPerishables() and stores the return array of objects
pi = GroceryItem.unpackPerishables();
//Loops through all the objects and displays the information
for(int x = 0; x < 5; x++)
System.out.println(pi[x]);
}//End of main
}//End of class
Output
Smart - Ones Frozen weighting 0.311kg with price $1.99(perishable) FreezerItem
Sealtest Milk 4.311kg with price $2.99(perishable) RefigeratorItem
2L Sealtest Milk 2.99kg with price $2.06(perishable) RefigeratorItem
Extra - Large Eggs 1.79kg with price $0.77(perishable) RefigeratorItem
Yoplait Yogurt 6 - Pack 4.74kg with price $1.02(perishable) RefigeratorItem