Please help. I have an exam coming up, and cannot get this right. Create a class
ID: 3624073 • Letter: P
Question
Please help. I have an exam coming up, and cannot get this right.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 calcCost().
Within the main method create a new scanner. Have user enter information for
- Number of cheese toppings
- Number of pepperoni toppings
- Number of Ham toppings
- Size of pizza
Create 3 new Pizza (size, numCheeseToppings, numHamToppings, numPepperoniToppings)
- Supreme pizza
- Cheese pizza
- Pepperoni pizza
Call the method getDescription(), to print out summary
Explanation / Answer
import java.util.*;
class Main
{
private String size;
private int no_of_cheese_toppings;
private int no_of_pepperoni_toppings;
private int no_of_ham_toppings;
public Main()
{
size = "Small";
no_of_cheese_toppings = 0;
no_of_pepperoni_toppings = 0;
no_of_ham_toppings = 0;
}
public void set_size(String str)
{
size = str;
}
public void set_no_of_cheese_toppings(int k)
{
no_of_cheese_toppings = k;
}
public void set_no_of_pepperoni_toppings(int k)
{
no_of_pepperoni_toppings = k;
}
public void set_no_of_ham_toppings(int k)
{
no_of_ham_toppings =k;
}
public String get_size()
{
return size;
}
public int get_no_of_cheese_toppings()
{
return no_of_cheese_toppings;
}
public int get_no_of_pepperoni_toppings()
{
return no_of_pepperoni_toppings;
}
public int get_no_of_ham_toppings()
{
return no_of_ham_toppings;
}
public double calcCost()
{
double cost = 0;
int k = get_no_of_cheese_toppings() + get_no_of_pepperoni_toppings() + get_no_of_ham_toppings();
if(size.equalsIgnoreCase("small"))
{
cost = 10 + 2*k;
}
else if(size.equalsIgnoreCase("medium"))
{
cost = 12 + 2*k;
}
if(size.equalsIgnoreCase("large"))
{
cost = 14 + 2*k;
}
return cost;
}
public void getDescription( )
{
System.out.println(" Main size is " + size + " number of cheese toppings " + get_no_of_cheese_toppings() +
" number of pepperoni toppings " + get_no_of_pepperoni_toppings() + " number of ham toppings " + get_no_of_ham_toppings());
System.out.println(" cost of this pizza is " + calcCost());
}
public static void main(String[] args)
{
Main a = new Main();
a.set_size("large");
a.set_no_of_cheese_toppings(1);
a.set_no_of_pepperoni_toppings(1);
a.set_no_of_ham_toppings(2);
a.getDescription( );
System.out.println(" ");
Main b = new Main();
b.set_size("small");
b.set_no_of_cheese_toppings(1);
b.getDescription( );
}
}