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

1st Part: Create a class named Pizza that stores information about a single pizz

ID: 3825521 • Letter: 1

Question

1st Part: Create a class named Pizza that stores information about a single pizza. It should contain the following:

• Private instance variables to store the following:

o The size of the pizza (either small, medium, or large)

o The number of cheese toppings o The number of pepperoni toppings

o The number of ham toppings

• Constructor(s) that set all of the instance variables.

• Public methods to get and set the instance variables.

• A public method named calcCost() that returns a double that is the cost of the pizza.

Pizza cost is determined by: Small: $10 + $2 per topping Medium: $12 + $2 per topping Large: $14 + $2 per topping • A public method named getDescription() that returns a String containing the pizza size, quantity of each topping, and the pizza cost as calculated by calcCost() method. Now create another class named PizzaOrder that allows up to three pizzas to be saved in an order. Each pizza saved should be a Pizza object. In addition to appropriate instance variables and constructors, add the following methods: • public void setNumPizzas(int numPizzas) – sets the number of pizzas in the order. It must be between 1 and 3. • public void setPizza1(Pizza pizza1) – sets the first pizza in the order. • public void setPizza2(Pizza pizza2) – sets the second pizza in the order. • public void setPizza3(Pizza pizza3) – sets the third pizza in the order. • public double calcTotal() -- returns the total cost of the order. • public void printReceipt() – prints the order receipt in the console.

2nd Part: Now, write two JUnit Test classes (PizzaTest and PizzaOrderTest) to test the Pizza and PizzaOrder classes and their methods.

Explanation / Answer

// Pizza.java

public class Pizza

{

private String pizzaSize;

private int cheeseCount;

private int pepperoniCount;

private int hamCount;

public Pizza()

{

this.pizzaSize = "";

this.cheeseCount = 0;

this.pepperoniCount = 0;

this.hamCount = 0;

}

public Pizza(String pizzaSize, int cheeseCount,

int pepperoniCount, int hamCount)

{

this.pizzaSize = pizzaSize;

this.cheeseCount = cheeseCount;

this.pepperoniCount = pepperoniCount;

this.hamCount = hamCount;

}

public String getPizzaSize()

{

return pizzaSize;

}

public void setPizzaSize(String pizzaSize)

{

this.pizzaSize = pizzaSize;

}

public int getNumCheeseToppings()

{

return cheeseCount;

}

public void setNumCheeseToppings(int cheeseCount)

{

this.cheeseCount = cheeseCount;

}

public int getNumPepperoniToppings()

{

return pepperoniCount;

}

public void setNumPepperoniToppings(int pepperoniCount)

{

this.pepperoniCount = pepperoniCount;

}

public int getNumHmaToppings()

{

return hamCount;

}

public void setNumHmaToppings(int hamCount)

{

this.hamCount = hamCount;

}

public double calcCost()

{                             

if(pizzaSize.equalsIgnoreCase("small"))

{

return 10 + (cheeseCount + pepperoniCount + hamCount) * 2;

}

else if(pizzaSize.equalsIgnoreCase("medium"))

{

return 12 + (cheeseCount + pepperoniCount + hamCount) * 2;

}

else if(pizzaSize.equalsIgnoreCase("large"))

{

return 14 + (cheeseCount + pepperoniCount + hamCount) * 2;

}

else

{

return 0.0;

}

}

public String getDescription()

{

return "Pizza size: " + pizzaSize + " Cheese toppings: " + cheeseCount + " Pepperoni toppings: "+ pepperoniCount + " Ham toppings: " + hamCount+ " Pizza cost: $" + calcCost() + " ";

}

}

// PizzaTest.java

public class PizzaTest

{

public static void main(String[] args)

{

Pizza p1 = new Pizza("large", 1, 1, 2);

Pizza p2 = new Pizza("small", 2, 1, 1);

Pizza p3 = new Pizza("medium", 1, 2, 1);

System.out.println(p1.getDescription());

System.out.println(p2.getDescription());

System.out.println(p3.getDescription());

}

}

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