public class Pizza { private String size; private int numCheeseToppings; private
ID: 3871300 • Letter: P
Question
public class Pizza
{
private String size;
private int numCheeseToppings;
private int numHamToppings;
private int numPepperoniToppings;
public Pizza()
{
size = "Large";
numCheeseToppings = 1;
numHamToppings = 0;
numPepperoniToppings = 0;
}
public Pizza(String pizzaSize, int cheese,
int ham, int pepperoni)
{
size = pizzaSize;
numCheeseToppings = cheese;
numHamToppings = ham;
numPepperoniToppings = pepperoni;
}
public void setPizzaInfo(String newSize, int cheese,
int ham, int pepperoni)
{
size = newSize;
numCheeseToppings = cheese;
numHamToppings = ham;
numPepperoniToppings = pepperoni;
}
public String getSize()
{
return size;
}
public void setNumCheeseToppings(int toppings)
{
numCheeseToppings = toppings;
}
public int getNumCheeseToppings()
{
return numCheeseToppings;
}
public int getNumHamToppings()
{
return numHamToppings;
}
public void setNumHamToppings(int toppings)
{
numHamToppings = toppings;
}
public int getNumPepperoniToppings()
{
return numPepperoniToppings;
}
public void setNumPepperoniToppings(int toppings)
{
numPepperoniToppings = toppings;
}
public double calcCost()
{
double baseCost = 10;
if (size.equals("Small"))
{
baseCost = 10;
}
else if (size.equals("Medium"))
{
baseCost = 12;
}
else if (size.equals("Large"))
{
baseCost = 14;
}
else
{
System.out.println("Error, unknown size.");
return 0;
}
return baseCost +
(numHamToppings + numCheeseToppings +
numPepperoniToppings)*2;
}
public String getDescription()
{
return "Size: " + size + ", Cheese Toppings: "
+ numCheeseToppings +
" Pepperoni Toppings: " + numPepperoniToppings +
" Ham Toppings: " + numHamToppings +
". Cost: " + calcCost();
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class PizzaOrder
{
private Pizza pizza1, pizza2, pizza3;
private int numPizzas;
public PizzaOrder()
{
numPizzas = 0;
pizza1 = null;
pizza2 = null;
pizza3 = null;
}
public void setNumPizzas(int num)
{
numPizzas = num;
}
public void setPizza1(Pizza pizza)
{
pizza1 = pizza;
}
public void setPizza2(Pizza pizza)
{
pizza2 = pizza;
}
public void setPizza3(Pizza pizza)
{
pizza3 = pizza;
}
public double calcTotal()
{
double total = 0;
if (numPizzas >= 1)
total += pizza1.calcCost();
if (numPizzas >= 2)
total += pizza2.calcCost();
if (numPizzas >= 3)
total += pizza3.calcCost();
return total;
}
// Sample main
public static void main(String[] args)
{
Pizza pizza1 = new Pizza("Large",1,1,0);
Pizza pizza2 = new Pizza("Medium",2,0,2);
System.out.println(pizza1.getDescription());
System.out.println(pizza2.getDescription());
PizzaOrder order = new PizzaOrder();
order.setNumPizzas(2); // 2 pizzas in the order
order.setPizza1(pizza1); // Set first pizza
order.setPizza2(pizza2); // Set second pizza
double total = order.calcTotal(); // Should be 18+20 = 38
System.out.println("The order total is " + total);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ABOVE TWO CODES ARE GIVEN NEED TO ADD THE BELOW WRITTEN STEP 3 AND ALSO PLEASE INCLUDE THE BELOW COMMENTS IN CODE WHERE ITS WRITTEN PLEASE
Step III: Extend PizzaOrder Class
In project 3, Step II asked you to create a PizzaOrder class that stores an order consisting of up to three pizzas. 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 rst pizza in the order or null if pizza1 is not set. • public Pizza getPizza2() – returns the second pizza in the order or null if pizza2 is not set. • public Pizza getPizza3() – returns the third pizza in the order or null if pizza3 is not set. • A copy constructor that takes another PizzaOrder object and makes an independent copy of its pizzas. This might be useful if using an old order as a starting point for a new order.
In your main method to test the new methods. Changing the pizzas in the new order should not change the pizzas in the original order. For example,
Pizza pizza1 = // Code to create a large pizza, 1 cheese, 1 ham Pizza pizza2 = // Code to create a medium pizza, 2 cheese, 2 pepperoni 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 double total = order1.calcTotal(); // Should be 18+20 = 38 PizzaOrder order2 = new PizzaOrder(order1); // Use copy constructor order2.getPizza1().setNumCheeseToppings(3); // Change toppings double total = order2.calcTotal(); // Should be 22 + 20 = 42 double origTotal = order1.calcTotal(); // Should still be 38
1
Note that the rst three lines of code are incomplete. You must complete them as part of the Programming Project
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
COMMENTS TO INCLUDE in program
1. Comments/style in the whole program
(1) Comments about the author information.
(2) Description of the program.
(3) Comments for pizza class.
(4) Comments for pizzaorder class.
(5) The style of the program is clear and easy read.
2. Variables declartion:
The name of the variables make sense
3. PizzaOrder class:
(1) public int getNumPizzas() : returns the number of pizzas in the order.
(2) mpublic Pizza getPizza1() : returns the first pizza in the order or null if pizza1 is not set.
(3) public Pizza getPizza2() : returns the second pizza in the order or null if pizza2 is not set.
(4) public Pizza getPizza3() : returns the third pizza in the order or null if pizza3 is not set. (5 points)
(5) A copy constructor that takes another PizzaOrder object and makes an independent copy of its pizzas. (10 points)
(a) Pizza number is copied correctly
(b) Pizza1 is deep copied
(c) Pizza2 is deep copied
(d) Pizza3 is deep copied
(6) Main method to test the Pizza class and PizzaOrder class:
Pizza/PizzaOrder objects create correctly
Changing the pizzas in the new order should not change the pizzas in the original order.
Explanation / Answer
//Pizza.java
public class Pizza
{
private String size;
private int numCheeseToppings;
private int numHamToppings;
private int numPepperoniToppings;
//Constructor
public Pizza()
{
size = "Large";
numCheeseToppings = 1;
numHamToppings = 0;
numPepperoniToppings = 0;
}
//Copy constructor
public Pizza(Pizza piz)
{
if(piz == null)
return;
this.size = piz.getSize();
this.numCheeseToppings = piz.getNumCheeseToppings();
this.numHamToppings = piz.getNumHamToppings();
this.numPepperoniToppings = piz.getNumPepperoniToppings();
}
public Pizza(String pizzaSize, int cheese,
int ham, int pepperoni)
{
size = pizzaSize;
numCheeseToppings = cheese;
numHamToppings = ham;
numPepperoniToppings = pepperoni;
}
//Method to set size,cheese,ham and pepperoni
public void setPizzaInfo(String newSize, int cheese,
int ham, int pepperoni)
{
size = newSize;
numCheeseToppings = cheese;
numHamToppings = ham;
numPepperoniToppings = pepperoni;
}
//Returns the size of the pizza
public String getSize()
{
return size;
}
//Set toppings
public void setNumCheeseToppings(int toppings)
{
numCheeseToppings = toppings;
}
//Returns the cheese toppings
public int getNumCheeseToppings()
{
return numCheeseToppings;
}
//Method to get ham toppings
public int getNumHamToppings()
{
return numHamToppings;
}
//Method to set ham toppings to class
public void setNumHamToppings(int toppings)
{
numHamToppings = toppings;
}
//Method to get toppings
public int getNumPepperoniToppings()
{
return numPepperoniToppings;
}
//Method to set toppings to class
public void setNumPepperoniToppings(int toppings)
{
numPepperoniToppings = toppings;
}
//Method returns the cost of the pizza
public double calcCost()
{
double baseCost = 10;
if (size.equals("Small"))
{
baseCost = 10;
}
else if (size.equals("Medium"))
{
baseCost = 12;
}
else if (size.equals("Large"))
{
baseCost = 14;
}
else
{
System.out.println("Error, unknown size.");
return 0;
}
return baseCost +
(numHamToppings + numCheeseToppings +
numPepperoniToppings)*2;
}
//Return description of the pizza object
public String getDescription()
{
return "Size: " + size + ", Cheese Toppings: "
+ numCheeseToppings +
" Pepperoni Toppings: " + numPepperoniToppings +
" Ham Toppings: " + numHamToppings +
". Cost: " + calcCost();
}
}
------------------------------------------------------------------------------------------------
//PizzaOrder.java
public class PizzaOrder
{
//private data variables of type Pizza
private Pizza pizza1, pizza2, pizza3;
//integer type numPizzas
private int numPizzas;
//constructor to set 0
//for numPizzas and null
//for pizza objects
public PizzaOrder()
{
numPizzas = 0;
pizza1 = null;
pizza2 = null;
pizza3 = null;
}
//parameter constructor to set pizza objects
public PizzaOrder(int numPizzas, Pizza pizza1,
Pizza pizza2, Pizza pizza3)
{
setNumPizzas(numPizzas);
setPizza1(pizza1);
setPizza2(pizza2);
setPizza3(pizza3);
}
//Copy constructor to copy order object
public PizzaOrder(PizzaOrder order)
{
if(order == null)
return;
numPizzas = order.numPizzas;
if(order.pizza1 == null)
pizza1 = null;
else
pizza1 = new Pizza(order.pizza1);
if(order.pizza2 == null)
pizza2 = null;
else
pizza2 = new Pizza(order.pizza2);
if(order.pizza3 == null)
pizza3 = null;
else
pizza3 = new Pizza(order.pizza3);
}
public void setNumPizzas(int num)
{
numPizzas = num;
}
//Method to set pizza for pizza1
public void setPizza1(Pizza pizza)
{
pizza1 = pizza;
}
//Method to set pizza for pizza2
public void setPizza2(Pizza pizza)
{
pizza2 = pizza;
}
//Method to set pizza for pizza3
public void setPizza3(Pizza pizza)
{
pizza3 = pizza;
}
//Retunrs the number of pizzas in the order
public int getNumPizzas()
{
return numPizzas;
}
//Method to get pizza1
public Pizza getPizza1()
{
return pizza1;
}
//Method to get pizza2
public Pizza getPizza2()
{
return pizza2;
}
//Method to get pizza3
public Pizza getPizza3()
{
return pizza3;
}
//Method that calculates the total for pizza order
public double calcTotal()
{
double total = 0;
if (numPizzas >= 1)
total += pizza1.calcCost();
if (numPizzas >= 2)
total += pizza2.calcCost();
if (numPizzas >= 3)
total += pizza3.calcCost();
return total;
}
// Sample main
public static void main(String[] args)
{
Pizza pizza1 = new Pizza("Large",1,1,0);
Pizza pizza2 = new Pizza("Medium",2,0,2);
System.out.println(pizza1.getDescription());
System.out.println(pizza2.getDescription());
PizzaOrder order1 = new PizzaOrder();
order1.setNumPizzas(2); // 2 pizzas in the order
order1.setPizza1(pizza1); // Set first pizza
order1.setPizza2(pizza2); // Set second pizza
double total = order1.calcTotal(); // Should be 18+20 = 38
System.out.println("The order1 total is " + total);
//create an instance of PizzaOrder with order1 object
PizzaOrder order2 = new PizzaOrder(order1);
// Use copy constructor
order2.getPizza1().setNumCheeseToppings(3);
// Change toppings
total = order2.calcTotal();
System.out.println("The order2 total is " + total);
// Should be 22 + 20 = 42
double origTotal = order1.calcTotal();
System.out.println("The order1 total is " + origTotal);
// Should still be 38
}
}
------------------------------------------------------------------------------------------------
Sample output:
Size: Large, Cheese Toppings: 1 Pepperoni Toppings: 0 Ham Toppings: 1. Cost: 18.0
Size: Medium, Cheese Toppings: 2 Pepperoni Toppings: 2 Ham Toppings: 0. Cost: 20.0
The order1 total is 38.0
The order2 total is 42.0
The order1 total is 38.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.