Question: Write a JAVA Program Step I: Pizza Class Create a class named Pizza that stores information about a single pizza. It should contain the following:
Write a JAVA Program
Step I: Pizza Class
Create a class named Pizza that stores information about a single pizza. It should contain
the following:
Private instance variables
the size of the pizza (either small, medium, or large) (String)
the number of cheese toppings (integer)
the number of pepperoni toppings (interger)
the number of ham toppings (integer)
Default Constructor without parameters.
Constructor 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().
Write test code in your driver class to create several pizzas and output their descriptions.
F
For example, a large pizza with one cheese, one pepperoni and two ham
toppings should cost a total of $22.
Step II: PizzaOrder Class
Create a PizzaOrder class that allows up to three pizzas to be saved in an order. Each
pizza saved should be a Pizza object as described in step I. It should contain the following:
Private instance variables
the number of the pizza
three Pizza objects: pizza1, pizza2, pizza3
Default Constructor without parameters, all instance variables could be set to 0
(for int) or null (for Pizza).
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. numPizzas must be between 1 and 3.
public void setPizza1(Pizza pizza1) sets the first pizza in the order, please use
deep copy.
public void setPizza2(Pizza pizza2) sets the second pizza in the order, please
use deep copy.
public void setPizza3(Pizza pizza3) sets the third pizza in the order, please
use deep copy.
public double calcTotal() returns the total cost of the order.
Extend this class with the following methods and constructor:
public int getNumPizzas() returns the number of pizzas in the order.
public Pizza getPizza1() returns the first pizza in the order or null if pizza1 is
not set, dont need deep copy return.
public Pizza getPizza2() returns the second pizza in the order or null if pizza2
is not set, dont need deep copy return.
public Pizza getPizza3() returns the third pizza in the order or null if pizza3 is
not set, dont need deep copy return.
A copy constructor that takes another PizzaOrder object and makes an independent
copy (deep copy) of its pizzas. This might be useful if using an old order
as a starting point for a new order.
Step III: Write a driver class to test your Pizza class and
PizzaOrder class.
The setPizza2 and setPizza3 methods will be used only if there are two or three pizzas in
the order, respectively. Sample code illustrating the methods is shown below. Note that
some lines are incomplete. You must complete them as part of the Programming Project.
Another thing you need to notice is that changing the pizzas in the new order should not
change the pizzas in the original order.
Pizza pizza1 = // Code to create a large pizza, 1 cheese, 1 ham
Pizza pizza2 = // Code to create a medium pizza, 2 cheese, 2 pepperoni
//print pizza1 information
System.out.println("pizza1 information:" + pizza1.getDescription());
//print pizza2 information
System.out.println("pizza2 information:" + pizza2.getDescription());
System.out.println("Create a new Pizza order1:");
PizzaOrder order1 = // Code to create an order
order1.setNumPizzas(2); // 2 pizzas in the order
order1.setPizza1(pizza1); // Set first pizza
order1.setPizza2(pizza2); // Set second pizza
System.out.println("The order1 total is " + order.calcTotal()); // Should be 18+20 = 38
//Copy a new order
System.out.println("Copy a new Pizza order2:");
PizzaOrder order2 = new PizzaOrder(order1); // Use copy constructor
System.out.println("Change order2s toppings to 3:");
order2.getPizza1().setNumCheeseToppings(3); // Change toppings
pizza1.setNumCheeseToppings(10); //check privacy leak
pizza2.setNumCheeseToppings(10); //check privacy leak
System.out.println("The order1 total is " + order.calcTotal());// Should still be 38
System.out.println("The order2 total is " + order2.calcTotal());// Should be 22 + 20 = 42
Output should be similar like this:
pizza1 information: ======
Size: Large, Cheese Toppings: 1 Pepperoni Toppings: 0 Ham Toppings: 1. Cost: 18.0
pizza2 information: ======
Size: Medium, Cheese Toppings: 2 Pepperoni Toppings: 2 Ham Toppings: 0. Cost: 20.0
3Create a new Pizza order1: ======
The order1 total is 38.0
Copy a new Pizza order2: ======
Change order2s pizza1s cheese toppings to 3:
The order1 total is 38.0
The order2 total is 42.0
Step IV:
Write the program using Pizza.java, PizzaOrder.java and Driver.java
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
