Question & Answer: Class design problem for JAVA.(2 CLASSES 1 DRIVER) Please Follow the directions. This is an intro to computer science class…..

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)

Expert Answer

 PROGRAM CODE:

Donut.java

package donut;

public class Donut {

 

private static String TYPES[] = {“Plain”, “Filled”, “Glazed”};

private static double PRICES[] = {0.99, 1.49, 1.29};

private String name;

private String type;

private double price;

 

public Donut(String name, String type) {

setName(name);

setType(type);

setPrice();

}

 

public Donut() {

name = “Classic”;

type = TYPES[0];

setPrice();

}

public String getName() {

return name;

}

private void setName(String name) {

this.name = name;

}

public String getType() {

return type;

}

private void setType(String type) {

boolean isAMatch = false;

for(String defaultType: TYPES)

{

if(defaultType.equals(type))

{

isAMatch = true;

break;

}

}

if(isAMatch)

this.type = type;

else this.type = TYPES[0];

}

public double getPrice() {

return price;

}

private void setPrice() {

for(int i=0; i<TYPES.length; i++)

{

if(TYPES[i].equals(type))

{

this.price = PRICES[i];

break;

}

}

}

 

@Override

public String toString() {

return name + “, ” + type + “, $” + price;

}

 

@Override

public boolean equals(Object obj) {

Donut d = (Donut)obj;

return d.getName().equals(this.name) && d.getType().equals(this.type);

}

}

DonutBox.java

package donut;

public class DonutBox {

 

private Donut donuts[];

private int count;

private static int MAX = 12;

private enum BoxType {SMALL, MEDIUM, LARGE};

 

public DonutBox() {

donuts = new Donut[MAX];

count = 0;

}

 

public int getCount()

{

return count;

}

 

public void addDonut (Donut order)

{

donuts[count++] = order;

}

 

public double getPrice()

{

double bill = 0;

double discount = 0;

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

discount = 10;

else if(count == 12)

discount = 20;

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

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

bill -= (bill*discount)/100;

 

return bill;

}

 

@Override

public String toString() {

String result = “”;

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

result += donuts[i] + “n”;

return result;

}

 

public BoxType getSize()

{

if(count == 1)

return BoxType.SMALL;

 

else if(count <=6)

return BoxType.MEDIUM;

 

else return BoxType.LARGE;

}

 

}

DonutBoxDriver.java

package donut;

public class DonutBoxDriver {

 

public static String names[] = {“Cream Crumble”, “Raspberry Dream”, “Strawberry Heaven”, “Death By chocolate”};

 

public static void fillDonuts(int count, DonutBox box)

{

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

box.addDonut(new Donut());

}

 

public static void fillGlazedDonuts(DonutBox box)

{

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

box.addDonut(new Donut(names[i], “Glazed”));

}

 

public static void fillFilledDonuts(DonutBox box)

{

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

box.addDonut(new Donut(names[i], “Filled”));

}

 

public static void main(String args[])

{

DonutBox boxof1 = new DonutBox();

fillDonuts(1, boxof1);

System.out.println(boxof1);

System.out.println(“Total Price: ” + boxof1.getPrice() + “n”);

 

DonutBox boxof6 = new DonutBox();

fillFilledDonuts(boxof6);

fillDonuts(2, boxof6);

System.out.println(boxof6);

System.out.println(“Total Price: ” + boxof6.getPrice() + “n”);

 

DonutBox boxof8 = new DonutBox();

fillGlazedDonuts(boxof8);

fillFilledDonuts(boxof8);

System.out.println(boxof8);

System.out.println(“Total Price: ” + boxof8.getPrice() + “n”);

 

DonutBox boxof12 = new DonutBox();

fillFilledDonuts(boxof12);

fillGlazedDonuts(boxof12);

fillDonuts(4, boxof12);

System.out.println(boxof12);

System.out.println(“Total Price: ” + boxof12.getPrice() + “n”);

 

}

}

OUTPUT:

Classic, Plain, $0.99

Total Price: 0.99

Cream Crumble, Filled, $1.49

Raspberry Dream, Filled, $1.49

Strawberry Heaven, Filled, $1.49

Death By chocolate, Filled, $1.49

Classic, Plain, $0.99

Classic, Plain, $0.99

Total Price: 7.146000000000001

Cream Crumble, Glazed, $1.29

Raspberry Dream, Glazed, $1.29

Strawberry Heaven, Glazed, $1.29

Death By chocolate, Glazed, $1.29

Cream Crumble, Filled, $1.49

Raspberry Dream, Filled, $1.49

Strawberry Heaven, Filled, $1.49

Death By chocolate, Filled, $1.49

Total Price: 10.008000000000001

Cream Crumble, Filled, $1.49

Raspberry Dream, Filled, $1.49

Strawberry Heaven, Filled, $1.49

Death By chocolate, Filled, $1.49

Cream Crumble, Glazed, $1.29

Raspberry Dream, Glazed, $1.29

Strawberry Heaven, Glazed, $1.29

Death By chocolate, Glazed, $1.29

Classic, Plain, $0.99

Classic, Plain, $0.99

Classic, Plain, $0.99

Classic, Plain, $0.99

Total Price: 13.572

Still stressed from student homework?
Get quality assistance from academic writers!