Create a class named Pizza that stores information about a single pizza. It shou
ID: 3529762 • Letter: C
Question
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 contain- ing the pizza size, quantity of each topping, and the pizza cost as calculated bycalcCost( ).
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.
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( );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.