SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Defining Classes I
Exercise:
Programming Projects
Question:12 | ISBN:9780132830317 | Edition: 5

Question

This programming project extends Programming Project 4.11. Create a PizzaOrder class that allows up to three pizzas to be saved in an order. Each pizza saved should be a Pizza object as described in Programming Project 4.11. In addition to appropriate instance variables and constructors, add the following methods:

  • public void setNumPizzas(int numPizzas) —sets the number of pizzas

  • in the order. numPizzas must be between 1 and 3.

  • public void setPizza1(Pizza pizza1) —sets the first pizza in the order.

  • public void setPizza2(Pizza pizza2) —sets the second pizza in the order.

  • public void setPizza3(Pizza pizza3) —sets the third pizza in the order.

  • public double calcTotal() —returns the total cost of the order.

Write a main method to test the class. The setPizza2 and setPizza3 methods

will be used only if there are two or three pizzas in the order, respectively. Sample code illustrating the methods is shown below. Note that first three lines are incomplete. You must complete them as part of the Programming Project.

Pizza pizza1 = // Code to create a large pizza, 1 cheese, 1 ham

Pizza pizza2 = // Code to create a medium pizza, 2 cheese, 2 pepperoni

PizzaOrder order = // Code to create an order

order.setNumPizzas(2); // 2 pizzas in the order

order.setPizza1(pizza1); // Set first pizza

order.setPizza2(pizza2); // Set second pizza

double total = order.calcTotal(); // Should be 18+20 = 38


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

// Pizza.java
public class Pizza
{
	private String pizzaSize;
	private int cheeseCount;
	private int pepperoniCount;
	private int hamCount;

	public Pizza()
	{
		this.pizzaSize = "";
		this.cheeseCount = 0;
		this.pepperoniCount = 0;
		this.hamCount = 0;
	}
	
	public Pizza(String pizzaSize, int cheeseCount, 
					int pepperoniCount, int hamCount)
	{
		this.pizzaSize = pizzaSize;
		this.cheeseCount = cheeseCount;
		this.pepperoniCount = pepperoniCount;
		this.hamCount = hamCount;
	}
	
	public String getPizzaSize()
	{
		return pizzaSize;
	}

	public void setPizzaSize(String pizzaSize)
	{
		this.pizzaSize = pizzaSize;
	}

	public int getNumCheeseToppings()
	{
		return cheeseCount;
	}

	public void setNumCheeseToppings(int cheeseCount)
	{
		this.cheeseCount = cheeseCount;
	}

	public int getNumPepperoniToppings()
	{
		return pepperoniCount;
	}

	public void setNumPepperoniToppings(int pepperoniCount)
	{
		this.pepperoniCount = pepperoniCount;
	}

	public int getNumHmaToppings()
	{
		return hamCount;
	}

	public void setNumHmaToppings(int hamCount)
	{
		this.hamCount = hamCount;
	}

	public double calcCost()
	{		
		if(pizzaSize.equalsIgnoreCase("small"))
		{
			return 10 + (cheeseCount + pepperoniCount + hamCount) * 2;
		}
		else if(pizzaSize.equalsIgnoreCase("medium"))
		{
			return 12 + (cheeseCount + pepperoniCount + hamCount) * 2;
		}
		else if(pizzaSize.equalsIgnoreCase("large"))
		{
			return 14 + (cheeseCount + pepperoniCount + hamCount) * 2;
		}
		else
		{
			return 0.0;
		}
	}

	public String getDescription()
	{
		return "Pizza size: " + pizzaSize + "\n Cheese toppings: " 
				+ cheeseCount + "\n Pepperoni toppings: "
				+ pepperoniCount + "\n Ham toppings: " + hamCount
				+ "\n Pizza cost: $" + calcCost() + "\n";
	}
}

 

// PizzaOrder.java
public class PizzaOrder
{
	private int numPizzas;
	private Pizza pizza1;
	private Pizza pizza2;
	private Pizza pizza3;
	
	public PizzaOrder()
	{
		this.numPizzas = 0;
		this.pizza1 = null;
		this.pizza2 = null;
		this.pizza3 = null;
	}
	
	public PizzaOrder(int numPizzas, Pizza pizza1, 
							Pizza pizza2, Pizza pizza3)
	{
		setNumPizzas(numPizzas);
		setPizza1(pizza1);
		setPizza2(pizza2);
		setPizza3(pizza3);
	}

	public void setNumPizzas(int numPizzas)
	{		
		if(numPizzas < 1)
			this.numPizzas = 1;
		else if(numPizzas > 3)
			this.numPizzas = 3;
		else
			this.numPizzas = numPizzas;
	}

	public void setPizza1(Pizza pizza1)
	{
		if(numPizzas >= 1)
			this.pizza1 = pizza1;
		else
			this.pizza1 = null;
	}

	public void setPizza2(Pizza pizza2)
	{
		if(numPizzas >= 2)
			this.pizza2 = pizza2;
		else
			this.pizza2 = null;
	}

	public void setPizza3(Pizza pizza3)
	{
		if(numPizzas >= 3)
			this.pizza3 = pizza3;
		else
			this.pizza3 = null;
	}
	
	public double calcTotal()
	{
		double total = 0.0;
		
		if(pizza1 != null)
			total += pizza1.calcCost();
		
		if(pizza2 != null)
			total += pizza2.calcCost();
		
		if(pizza3 != null)
			total += pizza3.calcCost();
		
		return total;
	}	
}

 

// PizzaOrderTest.java
public class PizzaOrderTest
{
	public static void main(String[] args)
	{
		Pizza pizza1 = new Pizza("large", 1, 0, 1);
		Pizza pizza2 = new Pizza("medium", 2, 2, 0);
		PizzaOrder order = new PizzaOrder();
		order.setNumPizzas(2);
		order.setPizza1(pizza1);
		order.setPizza2(pizza2);
		double total = order.calcTotal();
		
		System.out.println(pizza1.getDescription());
		System.out.println(pizza2.getDescription());
		System.out.println("Total cost: $" + total);
	}
}

 

Output:

Pizza size: large
 Cheese toppings: 1
 Pepperoni toppings: 0
 Ham toppings: 1
 Pizza cost: $18.0

Pizza size: medium
 Cheese toppings: 2
 Pepperoni toppings: 2
 Ham toppings: 0
 Pizza cost: $20.0

Total cost: $38.0

 

0 0

Discussions

Post the discussion to improve the above solution.