java program Create a class named Pizza that stores information about a single p
ID: 3701458 • Letter: J
Question
java program
Create a class named 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 calc Cost() 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 $22Explanation / Answer
PizzaApp.java
import java.util.Scanner;
public class PizzaApp {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter Pizza Size(small or medium or large): ");
String sizeOfPizza = scan.next();
System.out.print("Enter Number of Cheese Toppings: ");
int numOfCheeseToppings = scan.nextInt();
System.out.print("Enter Number of Pepperoni Toppings: ");
int numOfPepperoniToppings = scan.nextInt();
System.out.print("Enter Number of Ham Toppings: ");
int numOfHamToppings = scan.nextInt();
Pizza p = new Pizza(sizeOfPizza, numOfCheeseToppings, numOfPepperoniToppings, numOfHamToppings);
System.out.println(p.getDescription());
}
}
Pizza.java
public class Pizza {
private String sizeOfPizza;
private int numOfCheeseToppings;
private int numOfPepperoniToppings;
private int numOfHamToppings;
public Pizza(String sizeOfPizza, int numOfCheeseToppings, int numOfPepperoniToppings, int numOfHamToppings){
this.sizeOfPizza = sizeOfPizza;
this.numOfCheeseToppings = numOfCheeseToppings;
this.numOfHamToppings = numOfHamToppings;
this.numOfPepperoniToppings = numOfPepperoniToppings;
}
public String getSizeOfPizza() {
return sizeOfPizza;
}
public void setSizeOfPizza(String sizeOfPizza) {
this.sizeOfPizza = sizeOfPizza;
}
public int getNumOfCheeseToppings() {
return numOfCheeseToppings;
}
public void setNumOfCheeseToppings(int numOfCheeseToppings) {
this.numOfCheeseToppings = numOfCheeseToppings;
}
public int getNumOfPepperoniToppings() {
return numOfPepperoniToppings;
}
public void setNumOfPepperoniToppings(int numOfPepperoniToppings) {
this.numOfPepperoniToppings = numOfPepperoniToppings;
}
public int getNumOfHamToppings() {
return numOfHamToppings;
}
public void setNumOfHamToppings(int numOfHamToppings) {
this.numOfHamToppings = numOfHamToppings;
}
public double calcCost(){
double cost = 0;
int perTopping = 2;
int sizeCost = 0;
if(sizeOfPizza.equalsIgnoreCase("small")){
sizeCost = 10;
}
else if(sizeOfPizza.equalsIgnoreCase("medium")){
sizeCost = 12;
}
else if(sizeOfPizza.equalsIgnoreCase("large")){
sizeCost = 14;
}
cost = sizeCost + perTopping * (numOfCheeseToppings + numOfHamToppings + numOfPepperoniToppings);
return cost;
}
public String getDescription(){
return "Pizza Size: "+getSizeOfPizza()+" Number of Cheese Toppings:"+getNumOfCheeseToppings()+ " Number Of Pepperoni Toppings: "
+getNumOfPepperoniToppings()+" Number of Ham Toppings: "+getNumOfHamToppings()+" Pizza Cost: "+calcCost();
}
}
Output:
Enter Pizza Size(small or medium or large): small
Enter Number of Cheese Toppings: 4
Enter Number of Pepperoni Toppings: 5
Enter Number of Ham Toppings: 6
Pizza Size: small
Number of Cheese Toppings:4
Number Of Pepperoni Toppings: 5
Number of Ham Toppings: 6
Pizza Cost: 40.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.