Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java (Bluej) Create a class named Pizza that stores information about a single p

ID: 669663 • Letter: J

Question

Java (Bluej)

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:
o Small: $10 + $2 per topping
o Medium: $12 + $2 per topping
o Large: $14 + $2 per topping
A public method toString() that returns a String containing the pizza size, quantity of each topping, and the pizza cost as calculated by calcCost().
Write main method that asks for pizza size and number of toppings for each category. It should then initialize Pizza class and Print out the Pizza information without explicitly using toString();
The main method will then ask for another pizza, information including pizza size and number of toppings for each category. Define equals() method to compare if the two pizzas are the same.
Sample run:
First pizza:
Enter the size of Pizza: Large
Enter the number of Cheese toppings: 2
Enter the number of Pepperoni toppings: 0
Enter the number of Ham toppings: 1
Your order:
Large Pizza with 2 Cheese toppings,0 Pepperoni toppings, and 1 Ham toppings.
Total Price: 20.0
Second pizza:
Enter the size of Pizza: Medium
Enter the number of Cheese toppings: 2
Enter the number of Pepperoni toppings: 0
Enter the number of Ham toppings: 1
Your order:
Large Pizza with 2 Cheese toppings, 0 Pepperoni toppings, and 1 Ham toppings.
Total Price: 18.0
Two different pizzas

Explanation / Answer

package mani;

import java.util.Scanner;


public class Pizza{
   private String size;
   private int nCheese;
   private int pepper;
   private int ham;
   public Pizza(String s,int c,int p,int h){
       this.size=s.toLowerCase();
       this.nCheese=c;
       this.pepper=p;
       this.ham=h;
   }
   public void setSize(String s){
       size=s.toLowerCase();
   }
public void setCheese(int n){
       this.nCheese=n;
   }
public void setpepper(int n){
   this.pepper=n;
}
public void setHam(int n){
   this.ham=n;
}
public static boolean equals(Pizza p1,Pizza p2){
   if(p1.size.equals(p2.size)&&p1.nCheese==p2.nCheese&&p1.pepper==p2.pepper&&p1.ham==p2.ham){
       return true;
   }else{
   return false;
   }
  
}
public double calCost(){
   double tCost=0;
   if(size.equals("small")){
       tCost=10;
       tCost=tCost+2*(nCheese+pepper+ham);
   }else if(size.equals("large")){
       tCost=12;
       tCost=tCost+2*(nCheese+pepper+ham);
   }else if(size.equals("large")){
       tCost=14;
       tCost=tCost+2*(nCheese+pepper+ham);
   }
   return tCost;
}
     

   public String toString(){
       return "Your order: "+size+" with "+nCheese+" toppings,"+pepper+" toppings and "+ham+" Ham toppings ";
   }
   public static void main(String[] args){
       System.out.println("First Pizza:");
       Scanner s=new Scanner(System.in);
       System.out.print("Enter size of pizza: ");
       String size=s.next();
       System.out.print("Enter the number of cheese toppings: ");
       int n=s.nextInt();
       System.out.print("Enter the number of Pepperoni toppings: ");
       int p=s.nextInt();
       System.out.print("Enter the number of ham toppings: ");
       int h=s.nextInt();
       Pizza p1=new Pizza(size, n, p, h);
       double cost=p1.calCost();
       System.out.println(p1.toString());
       System.out.println("Total Price: "+cost);
       System.out.println("Second Pizza:");
      
       System.out.print("Enter size of pizza: ");
       size=s.next();
       System.out.print("Enter the number of cheese toppings: ");
       n=s.nextInt();
       System.out.print("Enter the number of Pepperoni toppings: ");
       p=s.nextInt();
       System.out.print("Enter the number of ham toppings: ");
       h=s.nextInt();
       Pizza p2=new Pizza(size, n, p, h);
       cost=p2.calCost();
       System.out.println(p2.toString());
       System.out.println("Total Price: "+cost);
       if(equals(p1, p2)){
           System.out.println("Pizzas are same.");
       }else{
           System.out.println("Two different pizzas");
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote