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

The objective of this assignment is to determine the proper implementation of in

ID: 3691937 • Letter: T

Question

The objective of this assignment is to determine the proper implementation of inheritance; utilizing abstract and derived classes. The user will develop the DessertShop application with the details below. Please use Java code.

Define the DessertItem abstract superclass from which specific types of DessertItem’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 getName() method to retrieve the name instance variable

This class should contain the setName() 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 DessertItem 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.89/lb. = 205 cents. The cost should be rounded to the nearest cent.

The Cookie class should be derived from the DessertItem class. A Cookie 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 DessertItem 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 toString() method to provide a formatted output of the item and the price. The toString() must use the derived getCost() method to properly calculate the individual prices.

Test Harness:

The source code application below MUST be used as your test harness

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 );

   }  

}

Sample 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                  $1.05

Chocolate Chip Ice Cream            $1.95

      Hot Fudge @ $0.50

Your assignment will be graded based upon all tasks being performed and handed in with all required documentation.

Description

Points

Definition of the abstract DessertItem superclass

5 points

Definition of the Candy derived class

4 points

Definition of the Cookie derived class

4 points

Definition of the IceCream derived class

4 points

Definition of the Sundae derived class

4 points

Definition of the toString method for each of the derived classes

4 points

Define the DessertItem abstract superclass from which specific types of DessertItem’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 getName() method to retrieve the name instance variable

This class should contain the setName() 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 DessertItem 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.89/lb. = 205 cents. The cost should be rounded to the nearest cent.

The Cookie class should be derived from the DessertItem class. A Cookie 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 DessertItem 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 toString() method to provide a formatted output of the item and the price. The toString() must use the derived getCost() method to properly calculate the individual prices.

Explanation / Answer

TestClasses.java
public class TestClasses {

    public static void main(String[] args) {


        DessertItem[] test = new DessertItem[4];
        test[0] = new Candy("Lollipop", 2.23, .78);
        test[1] = new Cookie("Cookie", 4, 399);
        test[2] = new IceCream("vanilla", 105);
        test[3] = new Sundae("vanilla", 105, "Fudge", 115);


        for (int i = 0; i < test.length; i++) {
            System.out.println(test[i]);
            System.out.println(test[i].getCost());
        }


        Checkout checkout = new Checkout();

        checkout.enterItem(new Candy("Peanut Butter Fudge", 2.25, 399));
        checkout.enterItem(new IceCream("Vanilla Ice Cream", 105));
        checkout.enterItem(new Sundae("Choc. Chip Ice Cream", 145, "Hot Fudge", 50));
        checkout.enterItem(new Cookie("Oatmeal Raisin Cookies", 4, 399));

        System.out.println(" Number of items: " + checkout.numberOfItems() + " ");
        System.out.println(" Total cost: " + checkout.totalCost() + " ");
        System.out.println(" Total tax: " + checkout.totalTax() + " ");
        System.out.println(" Cost + Tax: " + (checkout.totalCost() + checkout.totalTax()) + " ");
        System.out.println(checkout);

        checkout.clear();

        checkout.enterItem(new IceCream("Strawberry Ice Cream", 145));
        checkout.enterItem(new Sundae("Vanilla Ice Cream", 105, "Caramel", 50));
        checkout.enterItem(new Candy("Gummy Worms", 1.33, 89));
        checkout.enterItem(new Cookie("Chocolate Chip Cookies", 4, 399));
        checkout.enterItem(new Candy("Salt Water Taffy", 1.5, 209));
        checkout.enterItem(new Candy("Candy Corn", 3.0, 109));

        System.out.println(" Number of items: " + checkout.numberOfItems() + " ");
        System.out.println(" Total cost: " + checkout.totalCost() + " ");
        System.out.println(" Total tax: " + checkout.totalTax() + " ");
        System.out.println(" Cost + Tax: " + (checkout.totalCost() + checkout.totalTax()) + " ");
        System.out.println(checkout);
    }

}

Candy.java

public class Candy extends DessertItem {
//note that price per pound is in cents but output of getCost is cents
    private double weight;
    private double price_per_pound;


    public Candy(String Candyname, double amount, double ppp){
       super(Candyname);
       weight = amount;
       price_per_pound = ppp;
    }

    public String toString() {
        StringBuilder outString = new StringBuilder();
        int test = DessertShoppe.MAX_ITEM_NAME_SIZE;
        int int_weight = (int)weight;
        int int_ppp = (int)price_per_pound;
        String format_weight = Double.toString(weight);
        String format_price = DessertShoppe.cents2dollarsAndCents(int_ppp);
        outString.append(String.format("%-"+DessertShoppe.MAX_ITEM_NAME_SIZE+"s%n",format_weight+" lbs. @ "+format_price+" /lb."));
        outString.append(String.format("%-"+DessertShoppe.MAX_ITEM_NAME_SIZE+"s", this.name));
        return outString.toString();
    }

    public int getCost() {
        return ((int)(Math.round(weight * price_per_pound)));
    }

}


Cookie.java
public class Cookie extends DessertItem {
    private int number;
    private int price_per_dozen;

    public Cookie(String cookieName, int number, int price_per_dozen) {
        super(cookieName);
        this.number = number;
        this.price_per_dozen = price_per_dozen;
    }

    public String toString() {
        StringBuilder outString = new StringBuilder();
        String format_number = Integer.toString(number);
        String format_price = DessertShoppe.cents2dollarsAndCents(price_per_dozen);
        outString.append(String.format("%-"+DessertShoppe.MAX_ITEM_NAME_SIZE+"s %n", format_number+" @ "+format_price+" /dz."));
        outString.append(String.format("%-"+DessertShoppe.MAX_ITEM_NAME_SIZE+"s",this.name));
        return outString.toString();

    }

    public int getCost() {
        double dozen_fraction = ((number*1.0)/12);
        return ((int)(dozen_fraction*price_per_dozen));
    }
}


DessertItem.java
public abstract class DessertItem {

    protected String name;

    public DessertItem() {
        this("");
    }

    public DessertItem(String name) {
        if (name.length() <= DessertShoppe.MAX_ITEM_NAME_SIZE)
            this.name = name;
        else
            this.name = name.substring(0,DessertShoppe.MAX_ITEM_NAME_SIZE);
    }

    public final String getName() {
        return name;
    }

    public abstract int getCost();

}


DessertShoppe.java
public class DessertShoppe {

    public final static double TAX_RATE = 6.5;        // 6.5%
    public final static String STORE_NAME = "M & M Dessert Shoppe";
    public final static int MAX_ITEM_NAME_SIZE = 25;
    public final static int COST_WIDTH = 6;

    public static String cents2dollarsAndCents(int cents) {
        String s = "";

        if (cents < 0) {
            s += "-";
            cents *= -1;
        }

        int dollars = cents/100;
        cents = cents % 100;

        if (dollars > 0)
            s += dollars;

        s +=".";

        if (cents < 10)
            s += "0";

        s += cents;
        return s;
    }

}


IceCream.java
public class IceCream extends DessertItem {
    private int cost;

    IceCream(String flavor, int cost){
        super(flavor);
        this.cost = cost;
    }

    public String toString() {
        StringBuilder outString = new StringBuilder(String.format("%-"+DessertShoppe.MAX_ITEM_NAME_SIZE+"s",this.name));
        return outString.toString();
    }

    public int getCost() {
        return cost;
    }

}


Sundae.java
public class Sundae extends IceCream {
    private String topping;
    private int costTopping;

    public Sundae(String flavorIceCream, int costIceCream, String topping, int costTopping) {
        super(flavorIceCream, costIceCream);
        this.topping = topping;
        this.costTopping = costTopping; }

    public String toString() {
        StringBuilder outString = new StringBuilder();
        outString.append(String.format("%-"+DessertShoppe.MAX_ITEM_NAME_SIZE+"s %n%-"+DessertShoppe.MAX_ITEM_NAME_SIZE+"s",topping+" Sundae with", this.name));
        return outString.toString();


    }

    public int getCost() {
        int baseCost = super.getCost();
        return (baseCost + costTopping);
    }

    }
  
  
   Checkout.java
import java.util.Formatter;

public class Checkout {
    private int pointerItems = -1;
    public DessertItem[] itemArray;

    public Checkout() {
       itemArray = new DessertItem[100];
    }

    public int numberOfItems(){
    //returns number of dessertitems in the list
        return (pointerItems+1);
    }

    public void enterItem(DessertItem item) {
        //adds new dessert item to end of list of items
        pointerItems += 1;
        itemArray[(pointerItems)] = item;

    }

    public void clear(){
        //clears checkout
        itemArray = new DessertItem[100];
        pointerItems = -1;
    }

    public int totalCost(){
        //returns total cost of items in cents without tax
        if (pointerItems < 0) {
            return 0;}
        int Cost = 0;
        for (int i =0; i<= pointerItems; i++) {
            Cost += itemArray[i].getCost();

        }
        return Cost;
    }

    public int totalTax(){
        return ((int)(Math.round(.01 *this.totalCost()*DessertShoppe.TAX_RATE)));
        //returns total tax on items in cents
    }

    public String toString(){
        //returns a receipt representing current list
        //Calculate Length of the correct header spacings
        int outWidth = DessertShoppe.MAX_ITEM_NAME_SIZE+1+DessertShoppe.COST_WIDTH;
        int spacers = (outWidth - DessertShoppe.STORE_NAME.length())/2 -1;
        StringBuilder outString = new StringBuilder();

        outString.append(String.format("%-"+spacers+"s%s%s%n", " ", DessertShoppe.STORE_NAME, " "));
        //build line seperator
        String lineSep ="-";
        for (int i=1; i < DessertShoppe.STORE_NAME.length(); i++) {
            lineSep+="-";
        }

        outString.append(String.format("%-"+spacers+"s%s%s%n", " ", lineSep, " "));
        for (int i=0; i<= pointerItems; i++) {
            outString.append(String.format("%-"+DessertShoppe.MAX_ITEM_NAME_SIZE+"s", itemArray[i].toString()+" "));
            outString.append(String.format("%"+DessertShoppe.COST_WIDTH+"s", DessertShoppe.cents2dollarsAndCents(itemArray[i].getCost())+" "));
        }

        outString.append(String.format("%n%-"+DessertShoppe.MAX_ITEM_NAME_SIZE+"s%"+DessertShoppe.COST_WIDTH+"s%n", "Total Tax:", DessertShoppe.cents2dollarsAndCents(this.totalTax())));
        outString.append(String.format("%-"+DessertShoppe.MAX_ITEM_NAME_SIZE+"s%"+DessertShoppe.COST_WIDTH+"s%n", "Total Cost:", DessertShoppe.cents2dollarsAndCents(this.totalTax()+this.totalCost())));
       return outString.toString();
    }

}

sample output

2.23 lbs. @ .00 /lb.                                                                                                                                        
Lollipop                                                                                                                                                    
2                                                                                                                                                           
4 @ 3.99 /dz.                                                                                                                                               
Cookie                                                                                                                                                      
133                                                                                                                                                         
vanilla                                                                                                                                                     
105                                                                                                                                                         
Fudge Sundae with                                                                                                                                           
vanilla                                                                                                                                                     
220                                                                                                                                                         
                                                                                                                                                            
Number of items: 4                                                                                                                                          
                                                                                                                                                            
Total cost: 1331                                                                                                                                            
                                                                                                                                                            
                                                                                                                                                            
Total tax: 87                                                                                                                                               
                                                                                                                                                            
                                                                                                                                                            
Cost + Tax: 1418                                                                                                                                            
                                                                                                                                                            
     M & M Dessert Shoppe                                                                                                                                   
     --------------------                                                                                                                                   
2.25 lbs. @ 3.99 /lb.                                                                                                                                       
Peanut Butter Fudge        8.98                                                                                                                             
Vanilla Ice Cream          1.05                                                                                                                             
Hot Fudge Sundae with                                                                                                                                       
Choc. Chip Ice Cream       1.95                                                                                                                             
4 @ 3.99 /dz.                                                                                                                                               
Oatmeal Raisin Cookies     1.33                                                                                                                             
                                                                                                                                                            
Total Tax:                  .87                                                                                                                             
Total Cost:               14.18                                                                                                                             
                                                                                                                                                            
                                                                                                                                                            
Number of items: 6                                                                                                                                          
                                                                                                                                                            
                                                                                                                                                            
Total cost: 1192                                                                                                                                            
                                                                                                                                                            
                                                                                                                                                            
Total tax: 77                                                                                                                                               
                                                                                                                                                            
                                                                                                                                                            
Cost + Tax: 1269                                                                                                                                            
     M & M Dessert Shoppe                                                                                                                                   
     --------------------                                                                                                                                   
Strawberry Ice Cream       1.45                                                                                                                             
Caramel Sundae with                                                                                                                                         
Vanilla Ice Cream          1.55                                                                                                                             
1.33 lbs. @ .89 /lb.                                                                                                                                        
Gummy Worms                1.18                                                                                                                              

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote