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

Class design problem for JAVA.(2 CLASSES 1 DRIVER) Please Follow the directions.

ID: 3845391 • Letter: C

Question

Class design problem for JAVA.(2 CLASSES 1 DRIVER)

Please Follow the directions. This is an intro to computer science class.

NO ARRAYLIST, NO IMPORTS(ONLY SCANNER).KEEP EVERYTHING AS SIMPLE AS YOU CAN.

Class Design: Donut (Suggested Time Spent: < 15 minutes)

The Donut class is intended to be an abstract and simplified representation of a yummy edible donut.

Class Properties (MUST be private)

• Name of the donut

• Type of donut

• Price of the donut Class Invariants

• Donut name must not be empty (Default: “Classic”)

• Type of donut can only be one of the following: “Plain,” “Filled,” or “Glazed” (Default: “Plain”)

Class Components

• Public Getter and Private Setter for name, type, and price – enforce invariants

• A constructor that takes in as argument the name and type of the donut (enforce invariants)

o Determine the price of the donut based on the type:

$0.99 for plain

$1.29 for glazed

$1.49 for filled

• A toString() method that returns the name, type, and price of the donut.

• An equals() method that returns true if it’s the same name and type (regardless of price).

Class Design: DonutBox (Suggested Time Spent: < 15 minutes)

The DonutBox class is intended to be an abstract and simplified representation of a box of donuts. Among other things, this object should be able to list the price of the donuts.

Class Properties (MUST be private)

• Donuts (Donut[]) – an array of donuts

• Count – number of donuts in the box Class Invariants

• A box can have at most a dozen donuts Class Components

• Public getter ONLY for count • public void addDonut (Donut order) {}

• public double getPrice() {}

o Returns the total price (before tax) of the donuts in the box, with applicable discount:

Under 6 donuts, no discount

6 to 12 donuts, 10% discount

12 donuts exactly, 20% discount

• public String toString() {} – returns a String containing all the donuts in the box

• (Extra Credit: 1 point)

An enum for box type (Small, Medium, Large)

• (Extra Credit: 1 point)

A public method that returns the size of the box as an enum constant.

o Small box fits exactly one donut, medium fits up to six donuts, large fits up to 12.

Driver Class: DonutBoxDriver (Suggested Time Spent: < 10 minutes)

The DonutBoxDriver class is intended to be a simple driver to test the Donut and DonutBox objects and should include a main() method as well as other helper methods. At minimum, the driver class should:

• Create a box with only one donut

• Create a box of up to six donuts

• Create a box of more than six but less than twelve donuts

• Create a box of dozen donuts

• For each box created

o Print out the contents

o Print out the price (before tax) of the donuts in the box

Grading Criteria

• Donut class object (Total: 15 Points)

o [1 Point] Proper class definition syntax

o [3 Points] Implements all required properties

o [1 Points] Implements required getters

o [2 Points] All invariants properly enforced

o [3 Points] Constructor properly implemented

o [3 Points] toString method properly implemented

o [2 Points] equals method properly implemented

• DonutBox class object (Total: 15 Points) o

[1 Point] Proper class definition syntax

o [2 Points] Implements all required properties

o [1 Point] Implements required getter

o [2 Points] All invariants properly enforced

o [9 Points] Methods (addDonut, getPrice, toString) properly implemented (3 points per method)

• DonutBoxDriver program (Total: 10 Points)

o [1 Point] Proper class definition syntax

o [1 Point] Proper main method syntax

o [8 Points] Create, print contents and price of the four boxes (2 points per box created)

Explanation / Answer

Here is the code for the question. Please do not forget to rate the answer if it helped. Thank you very much.

Donut.java

public class Donut {

   private String name;

   private String type;

   private double price;

   public Donut(String name, String type)

   {

       setName(name);

       setType(type);

       if(type.equals("Plain"))

           setPrice(0.99);

       else if(type.equals("Glazed"))

           setPrice(1.29);

       else if(type.equals("Filled"))

           setPrice(1.49);

   }

  

   //public getters

   public String getName() {

       return name;

   }

  

   public String getType() {

       return type;

   }

  

   public double getPrice() {

       return price;

   }

   //private setters

   private void setName(String name) {

       if(name == null || name.equals(""))

           this.name = "Classic";

       else

           this.name = name;

   }

   private void setType(String type) {

       if(type.equals("Plain") || type.equals("Filled") || type.equals("Glazed"))

           this.type = type;

       else

           this.type = "Plain";

   }

  

   private void setPrice(double p)

   {

       this.price = p;

   }

  

   public String toString()

   {

       return "Name: "+name+

               " Type: "+type+

               " Price: "+price;

      

   }

  

   public boolean equals(Donut other)

   {

       if(name.equals(other.name) && type.equals(other.type))

           return true;

       else

           return false;

   }

}

DonutBox.java

public class DonutBox {

   private Donut donuts[];

   private int count;

   public enum Size{SMALL, MEDIUM, LARGE}

   public DonutBox()

   {

       donuts = new Donut[12]; // a dozen donuts

       count = 0;

   }

  

   public int getCount()

   {

       return count;

   }

  

   public void addDonut(Donut order)

   {

       if(count < 12) //check if there is space

       {

           donuts[count] = order;

           count++;

       }

   }

  

   public double getPrice()

   {

       double total = 0;

       for(int i = 0; i< count; i++)

           total = total + donuts[i].getPrice();

      

       //apply discount

       if(count == 12)

           total = total - 0.20 * total;

       else if(count >=6 && count <12)

           total = total - 0.10 * total;

  

       return total;

   }

  

   public String toString()

   {

       String str="";

       for(int i = 0; i<count; i++)

           str += " " + donuts[i].toString();

       return str;

   }

  

   public Size getSize()

   {

       if(count == 1)

           return Size.SMALL;

       else if(count <= 6)

           return Size.MEDIUM;

       else

           return Size.LARGE;

   }

}

DonutBoxDriver.java

public class DonutBoxDriver {

   public static void main(String[] args) {

       DonutBox box1 = new DonutBox();

       DonutBox box2 = new DonutBox();

       DonutBox box3 = new DonutBox();

       DonutBox box4 = new DonutBox();

      

      

       box1.addDonut(new Donut("Classic","Plain"));

       box1.addDonut(new Donut("Classic","Glazed"));

       box1.addDonut(new Donut("Classic","Filled"));

       box1.addDonut(new Donut("Sugar Ice","Plain"));

       box1.addDonut(new Donut("Coco coo","Glazed"));

       box1.addDonut(new Donut("Snow white","Plain"));

      

       box2.addDonut(new Donut("Classic","Plain"));

      

       box3.addDonut(new Donut("Jacky Chunk","Plain"));

       box3.addDonut(new Donut("Black Jack","Plain"));

       box3.addDonut(new Donut("Classic","Plain"));

       box3.addDonut(new Donut("Classic","Glazed"));

       box3.addDonut(new Donut("Classic","Filled"));

       box3.addDonut(new Donut("Sugar Ice","Plain"));

       box3.addDonut(new Donut("Coco coo","Glazed"));

       box3.addDonut(new Donut("Snow white","Plain"));

      

       box4.addDonut(new Donut("Jacky Chunk","Plain"));

       box4.addDonut(new Donut("Black Jack","Plain"));

       box4.addDonut(new Donut("Classic","Plain"));

       box4.addDonut(new Donut("Classic","Glazed"));

       box4.addDonut(new Donut("Classic","Filled"));

       box4.addDonut(new Donut("Sugar Ice","Plain"));

       box4.addDonut(new Donut("Coco coo","Glazed"));

       box4.addDonut(new Donut("Snow white","Plain"));

       box4.addDonut(new Donut("Jacky Chunk","Plain"));

       box4.addDonut(new Donut("Black Jack","Plain"));

       box4.addDonut(new Donut("Classic","Plain"));

       box4.addDonut(new Donut("Classic","Glazed"));

      

       System.out.println("Box 1"+box1 +" Price: " + box1.getPrice());

       System.out.println("Box 2"+box2 +" Price: " + box2.getPrice());

       System.out.println("Box 3"+box3 +" Price: " + box3.getPrice());

       System.out.println("Box 4"+box4 +" Price: " + box4.getPrice());

      

   }

}

output

Box 1

Name: Classic Type: Plain Price: 0.99

Name: Classic Type: Glazed Price: 1.29

Name: Classic Type: Filled Price: 1.49

Name: Sugar Ice Type: Plain Price: 0.99

Name: Coco coo Type: Glazed Price: 1.29

Name: Snow white Type: Plain Price: 0.99

Price: 6.336

Box 2

Name: Classic Type: Plain Price: 0.99

Price: 0.99

Box 3

Name: Jacky Chunk Type: Plain Price: 0.99

Name: Black Jack Type: Plain Price: 0.99

Name: Classic Type: Plain Price: 0.99

Name: Classic Type: Glazed Price: 1.29

Name: Classic Type: Filled Price: 1.49

Name: Sugar Ice Type: Plain Price: 0.99

Name: Coco coo Type: Glazed Price: 1.29

Name: Snow white Type: Plain Price: 0.99

Price: 8.118000000000002

Box 4

Name: Jacky Chunk Type: Plain Price: 0.99

Name: Black Jack Type: Plain Price: 0.99

Name: Classic Type: Plain Price: 0.99

Name: Classic Type: Glazed Price: 1.29

Name: Classic Type: Filled Price: 1.49

Name: Sugar Ice Type: Plain Price: 0.99

Name: Coco coo Type: Glazed Price: 1.29

Name: Snow white Type: Plain Price: 0.99

Name: Jacky Chunk Type: Plain Price: 0.99

Name: Black Jack Type: Plain Price: 0.99

Name: Classic Type: Plain Price: 0.99

Name: Classic Type: Glazed Price: 1.29

Price: 10.624