Create a class names Pizza that stores information about a single pizza. It shou
ID: 3751768 • Letter: C
Question
Create a class names Pizza that stores information about a single pizza. It should contain the following:
Private instance variables to store the size of the pizza (either small, medium, or large), the number of cheese toppings, the number of pepperoni toppings, and 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 ( )
Write test code to create several pizzas and output their descriptions. For example, a large pizza with one cheese, one pepperoni and two ham toppings should cost a total of $22.
Please make it a new program i want to see the different ways to make the program to the problem.
Explanation / Answer
package chegg;
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()+"");
}
}
===================================================
Test Case -1
===================================================
Choose your pizza size
1.Small
2.Medium
3.Large
1
Enter no of cheese toppings
2
Enter no of pepperoni toppings
0
Enter no of ham toppings
0
Cost : 14
=============================================
Test case -2
=============================================
Choose your pizza size
1.Small
2.Medium
3.Large
3
Enter no of cheese toppings
1
Enter no of pepperoni toppings
1
Enter no of ham toppings
2
Cost : 22
==================================================
Test case -3
=================================================
Choose your pizza size
1.Small
2.Medium
3.Large
2
Enter no of cheese toppings
0
Enter no of pepperoni toppings
0
Enter no of ham toppings
0
Cost : 12
================================================
Test case -4
==================================================
Choose your pizza size
1.Small
2.Medium
3.Large
2
Enter no of cheese toppings
1
Enter no of pepperoni toppings
1
Enter no of ham toppings
1
Cost : 18
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.