Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Pizza { private String size; private int numCheeseToppings; private int numHamToppings; private int numPepperoniToppings; public Pizza() { size = Large; numCheeseToppings = 1;

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management With Website Development Applications

Authors: Greg Riccardi

1st Edition

0201743876, 978-0201743876

More Books

Students also viewed these Databases questions