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

package dessertshop ; public class DessertShop { public static void main( String

ID: 3692457 • Letter: P

Question

package dessertshop;

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", 1.05 );

Sundae item4 = new Sundae( "Chocolate Chip Ice Cream", 1.45, "Hot Fudge", 0.50 );

System.out.println( item1 );

System.out.println( item2 );

System.out.println( item3 );

System.out.println( item4 );

}   

}

Assignment Objective The objective of this assignment is to utilizing abstract and derived classes. The user will develop the DessertShop application with the details below mine the proper implementation of inheritance Assignment Taskts) Define the DessertItem abstract superclass from which specific types of Dessertltem's can be derived. This abstract class contains one instance variable, name. .This class should contain a default Constructor This class should contain a constructor which can be used to pass in the name of the item This class should contain the getName0 method to retrieve the name instance variable This class should contain the setName0 method to set the name instance variable The class should contain an abstract method, getCost). which is not defined in this class because determining the costs varies based on the type of the item. The DessertShop application should contain a number of derived classes. The Candy class should be derived from the Dessertltem class. A Candy item has a weight and a price per pound which is used to determine its cost. For example. 2.30 lbs of fudge @ 0.891b. = 205 cents. The cost should be rounded to the nearest cent The Cookie class should be derived from the DessertItem class. ACookie item has a number and a price per dozen which are used to determine its cost. For example. 4 cookies @ 399 cents / dozen 133 cents. The cost should be rounded to the nearest cent. The IceCream class should be derived from the Dessertltem class. An IceCream item simply has a cost. .The Sundae class should be derived from the DessertItem class. The cost of a Sundae is the cost of the IceCream plus the cost of a topping Each class should contain constructors, which call the constructor of the superclass. Each class should implement a toString0 method to provide a formatted output of the item and the price. The toString) must use the derived getCost method to properly calculate the ndividual prices

Explanation / Answer

package dessertshop;

public abstract class DessertItem{
String name;
public DessertItem(){}
public DessertItem(String n)
{
name = n;
}
public String getName()
{
return name;
}
public void setName(String n)
{
name = n;
}
public abstract double getCost();
}

class Candy extends DessertItem
{
double weight;
double price;
Candy(String n, double w, double p)
{
super(n);
weight = w;
price = p;
}
public double getCost()
{
double cost = weight*price;
int costinCents = (int)cost*100;
cost = costinCents /100.0;
return cost;
}
public String toString()
{
return name+" " + getCost() + " " + weight + " lbs @ $" + price;
}
}


class Cookie extends DessertItem
{
int number;
double price;
Cookie(String n, int num, double p)
{
super(n);
number = num;
price = p;
}
public double getCost()
{
double cost = number*price;
int costinCents = (int)cost*100;
cost = costinCents /100.0;
return cost;
}
public String toString()
{
return name+" " + getCost() + " " + number + " cookies @ $" + price;
}
}

class IceCream extends DessertItem
{
double price;
IceCream(String n, double p)
{
super(n);
price = p;
}
public double getCost()
{
double cost = price;
int costinCents = (int)cost*100;
cost = costinCents /100.0;
return cost;
}
public String toString()
{
return name+" " + getCost();
}
}

class Sundae extends DessertItem
{
double weight;
String variety;
double price;
Sundae (String n, double w, String v, double p)
{
super(n);
weight = w;
variety = v;
price = p;
}
public double getCost()
{
double cost = weight*price;
int costinCents = (int)cost*100;
cost = costinCents /100.0;
return cost;
}
public String toString()
{
return name+" " + getCost() + " " + variety+ " @ $" + price;
}
}

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", 1.05 );
Sundae item4 = new Sundae( "Chocolate Chip Ice Cream", 1.45, "Hot Fudge", 0.50 );
  
System.out.println( item1 );
System.out.println( item2 );
System.out.println( item3 );
System.out.println( item4 );
}   
}