need help writing this java program 2, create a elass named Pizza that steres in
ID: 3777293 • Letter: N
Question
need help writing this java program
Explanation / Answer
/* Code Output:
Pizza of large size with 1 cheese toppings, 2 ham toppings and 1 pepperoni toppings will cost $22.0
Pizza of small size with 1 cheese toppings, 2 ham toppings and 1 pepperoni toppings will cost $18.0
Pizza of medium size with 1 cheese toppings, 2 ham toppings and 1 pepperoni toppings will cost $20.0
*/
package abc;
public class RE{
public static void main(String[] args) {
Pizza p = new Pizza("large",1,1,2);
System.out.println(p.getDescription());
Pizza p1 = new Pizza("small",1,1,2);
System.out.println(p1.getDescription());
Pizza p2 = new Pizza("medium",1,1,2);
System.out.println(p2.getDescription());
}
}
class Pizza{
private String size;
private int no_of_cheese_toppings;
private int no_of_pepperoni_toppings;
private int no_of_ham_toppings;
Pizza(String size, int no_of_cheese_toppings, int no_of_pepperoni_toppings, int no_of_ham_toppings){
this.size = size;
this.no_of_cheese_toppings = no_of_cheese_toppings;
this.no_of_pepperoni_toppings = no_of_pepperoni_toppings;
this.no_of_ham_toppings = no_of_ham_toppings;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public int getNo_of_cheese_toppings() {
return no_of_cheese_toppings;
}
public void setNo_of_cheese_toppings(int no_of_cheese_toppings) {
this.no_of_cheese_toppings = no_of_cheese_toppings;
}
public int getNo_of_pepperoni_toppings() {
return no_of_pepperoni_toppings;
}
public void setNo_of_pepperoni_toppings(int no_of_pepperoni_toppings) {
this.no_of_pepperoni_toppings = no_of_pepperoni_toppings;
}
public int getNo_of_ham_toppings() {
return no_of_ham_toppings;
}
public void setNo_of_ham_toppings(int no_of_ham_toppings) {
this.no_of_ham_toppings = no_of_ham_toppings;
}
public double calcCost(){
int cost = 0;
if(size.equalsIgnoreCase("Small")){
cost = 10 + (2*no_of_cheese_toppings) + (2*no_of_ham_toppings) + (2*no_of_pepperoni_toppings);
}
else if(size.equalsIgnoreCase("Medium")){
cost = 12 + (2*no_of_cheese_toppings) + (2*no_of_ham_toppings) + (2*no_of_pepperoni_toppings);
}
else if(size.equalsIgnoreCase("Large")){
cost = 14 + (2*no_of_cheese_toppings) + (2*no_of_ham_toppings) + (2*no_of_pepperoni_toppings);
}
return cost;
}
public String getDescription(){
double cost = calcCost();
String str = "Pizza of "+size+" size with "+no_of_cheese_toppings+" cheese toppings, "+no_of_ham_toppings+" ham toppings and "+no_of_pepperoni_toppings+" pepperoni toppings will cost $"+cost;
return str;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.