Question
When I try to run the PizzaOrder.java file I get the error: Exception in thread main java.lang.Error: Unresolved compilation problem: The method PizzaOrder(int, Pizza, Pizza,
When I try to run the PizzaOrder.java file I get the error: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method PizzaOrder(int, Pizza, Pizza, null) is undefined for the type PizzaOrder
Here is the problem the program is for:
This programming project extends Programming Project 4.11. 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 Programming Project 4.11. 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.
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.
Write a main method to test the 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 first three lines are incomplete. You must complete them as part of the Programming Project.
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 order = // Code to create an order
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
Here is the Pizza.java file:
import java.util.Scanner; public class Pizza { private String size=""; private int noOfCheeseToppings; private int noOfPepperoniToppings; private int noOfHamToppings; public Pizza(String size,int noOfCheeseToppings,int noOfPepperoniToppings,int noOfHamToppings) { // TODO Auto-generated constructor stub this.size=size; this.noOfCheeseToppings=noOfCheeseToppings; this.noOfPepperoniToppings=noOfPepperoniToppings; this.noOfHamToppings=noOfHamToppings; } public String getSize() { return size; } public void setSize(String size) { this.size = size; } public int getNoOfCheeseToppings() { return noOfCheeseToppings; } public void setNoOfCheeseToppings(int noOfCheeseToppings) { this.noOfCheeseToppings = noOfCheeseToppings; } public int getNoOfPepperoniToppings() { return noOfPepperoniToppings; } public void setNoOfPepperoniToppings(int noOfPepperoniToppings) { this.noOfPepperoniToppings = noOfPepperoniToppings; } public int getNoOfHamToppings() { return noOfHamToppings; } public void setNoOfHamToppings(int noOfHamToppings) { this.noOfHamToppings = noOfHamToppings; } public int calculateCost(){ int cost=0; if(getSize()=="Small") cost=10; else if (getSize()=="Medium") cost=12; else cost=14; int noOfToppings=getNoOfCheeseToppings()+getNoOfPepperoniToppings()+getNoOfHamToppings(); cost+=noOfToppings*2; return cost; } public static void main(String args[]){ Pizza P=new Pizza("",0,0,0); int ch; Scanner sc=new Scanner(System.in); System.out.println("Choose your pizza size"); System.out.println("1.Small"); System.out.println("2.Medium"); System.out.println("3.Large"); ch=sc.nextInt(); switch(ch){ case 1: P.setSize("Small"); break; case 2: P.setSize("Medium"); break; case 3: P.setSize("Large"); break; } System.out.println("Enter no. of cheese toppings"); P.setNoOfCheeseToppings(sc.nextInt()); System.out.println("Enter no. of pepperoni toppings"); P.setNoOfPepperoniToppings(sc.nextInt()); System.out.println("Enter no. of ham toppings"); P.setNoOfHamToppings(sc.nextInt()); System.out.println("Cost : "+P.calculateCost()+""); } } Here is my PizzaOrder.java file:
import java.util.Scanner; public class PizzaOrder { private Pizza pizza1; private Pizza pizza2; private Pizza pizza3; private int numPizzas; public PizzaOrder(int numPizza, Pizza pizza1, Pizza pizza2, Pizza pizza3) { setNumPizzas(numPizza); setPizza1(pizza1); setPizza1(pizza2); setPizza1(pizza3); }
public void setNumPizzas(int numPizzas) { this.numPizzas = numPizzas; } public void setPizza1(Pizza pizza1) { this.pizza1 = pizza1; }
public void setPizza2(Pizza pizza2) { this.pizza2 = pizza2; }
public void setPizza3(Pizza pizza3) { this.pizza3 = pizza3; }
public double calcTotal() { double total = 0; if (pizza1 != null) total += pizza1.calculateCost(); if (pizza2 != null) total += pizza2.calculateCost(); if (pizza3 != null) total += pizza3.calculateCost(); return total; } public static void main(String args[]) { Pizza p1=new Pizza("Small",3,2,1); Pizza p2=new Pizza("Large",2,1,0); PizzaOrder(2,p1,p2,null); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started