Question
Java Program: Overview: We want to define an abstract class called DessertItem. DesertItem is a class which can be built upon for multiple types of
Java Program:
Overview:
We want to define an abstract class called DessertItem. DesertItem is a class which can be built upon for multiple types of classes. This abstract class will contain a single instance variable name which will be common for all types of deserts. It will also contain an abstract method getCost(), which determines the cost of the item. The class is defined abstract because we will never directly instantiate an object from DessertItem.
Since this abstract class can be used by multiple subclasses and will be different for each one, the definition of getCost() should not be defined in the base class DessertItem. This method is declared abstract because each subclass that inherits the abstract class needs to define the getCost() method specific to its own data type.
In the abstract class, we should also define a getter method for the instance variable name.
abstract class DessertItem
{
protected String name;
public DessertItem() // default Constructor
{
this.name = "";
}
public DessertItem( String name ) // named Constructor
{
this.name = name;
}
public final String getName() // getter method for name
{
return name;
}
public abstract double getCost();
}
Task 1:
We now want to define a class called Candy, which will inherit the abstract class DessertItem. To define this class, we declare it as follows:
class Candy extends DessertItem
{
}
Task 2:
The abstract class contains the instance variable name, so we use this from the superclass. We now need to define two additional instance variables: weight and pricePerPound (both double).
private double weight;
private double pricePerPound;
Task 3:
We need define a Constructor for class Candy. This constructor needs to set initialization values for all three instance variables; name, weight and pricePerPound.
public Candy( String name, double unitWeight, double unitPrice )
{
super( name ); // call constructor of super class
this.weight = unitWeight; // Set weight variable
this.pricePerPound = unitPrice; // set unitPrice variable
}
Task 4:
Since the abstract class DessertItem contains an abstract method, getCost(), the implementation of getCost() for the class Candy must be defined. The cost of a candy item is calculated by the Unit Weight times the Unit Price.
@Override
public double getCost()
{
return( weight * pricePerPound );
}
Task 5
We should also create a toString() method to display Candy information.
public String toString()
{
NumberFormat formatter = NumberFormat.getCurrencyInstance();
return( name + "\t\t\t" + formatter.format( this.getCost()) + " \t" + this.weight + " lbs @ " + formatter.format( this.pricePerPound ));
}
The complete subclass Candy is included here:
class Candy extends DessertItem
{
private double weight;
private double pricePerPound;
public Candy( String name, double unitWeight, double unitPrice )
{
super( name );
this.weight = unitWeight;
this.pricePerPound = unitPrice;
}
@Override
public double getCost()
{
return( weight * pricePerPound );
}
public String toString()
{
NumberFormat formatter = NumberFormat.getCurrencyInstance();
return( name + "\t\t\t" + formatter.format( this.getCost()) + " \t" + this.weight + " lbs @ " + formatter.format( this.pricePerPound ));
}
}
Task 6
Define a class called Cookie, which will inherit the abstract class DessertItem. This subclass will include two instance variables, number of cookies and price per dozen. The Cookie class should contain a default constructor and a named constructor which initializes the three variables. The constructors should call the constructor from the super class. The class Candy must implement a getCost() method and a toString() method.
Task 7
Define a class called IceCream, which will inherit the abstract class DessertItem. This subclass will include two instance variables, number of scoops and price per scoop. The IceCream class should contain a default constructor and a named constructor which initializes the three variables. The constructors should call the constructor from the super class. The class IceCream must implement a getCost() method and a toString() method.
Test and Template
Use DessertShop to test your subclasses.
package dessertshop; import java.text.NumberFormat; abstract class DessertItem { protected String name; public DessertItem() { this.name = ""; } public DessertItem( String name ) { this.name = name; } public final String getName() { return name; } public abstract double getCost(); } class Candy extends DessertItem { private double weight; private double pricePerPound; public Candy( String name, double unitWeight, double unitPrice ) { super( name ); this.weight = unitWeight; this.pricePerPound = unitPrice; } @Override public double getCost() { return( weight * pricePerPound ); } public String toString() { NumberFormat formatter = NumberFormat.getCurrencyInstance(); return( name + "\t\t\t" + formatter.format( this.getCost()) + " \t" + this.weight + " lbs @ " + formatter.format( this.pricePerPound )); } } public class DessertShop { public static void main( String[] args ) { Candy item1 = new Candy( "Peanut Butter Fudge", 2.25, 3.99 ); Cookie item2 = new Cookie( "Oatmeal Raisin Cookies", 4, 3.99 ); IceCream item3 = new IceCream( "Vanilla Ice Cream", 2, 1.05 ); System.out.println( item1 ); System.out.println( item2 ); System.out.println( item3 ); } }
Output:
Peanut Butter Fudge $8.98
2.25 lbs @ $3.99
Oatmeal Raisin Cookies $1.33
4 cookies @ $3.99 per Dozen
Vanilla Ice Cream $2.10
End-Goal:
Submit your definition for both the Cookie class and the IceCream class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started