I give u more information now, this the file I have to used to write this progra
ID: 3572796 • Letter: I
Question
I give u more information now, this the file I have to used to write this program.
I hope u understand now.
// Purpose: Pizza Example
// Notes:
// A Class provides one or more methods (Methods represents tasks in a program)
// Classes contain one or more attributes (Specified by instance variables)
// Think of this Pizza class as being a "template" for a Pizza
// This class does not contain a main method - this cannot be run!
public class Pizza
{
// Pizza Data (Class Attributes)
// size - 1 small; 2 medium; 3 large
private int size;
// number of toppings 0-9
private int numToppings;
private String toppingDescription;
// the proper way to handle the toppings is to
// use an array of Strings...that's chapter 7
// Constructor (when you do issue a "new Pizza" this is called
public Pizza(int newSize, int newNumToppings, String newToppingDescription)
{
setSize(newSize);
setNumToppings(newNumToppings);
setToppingDescription(newToppingDescription);
}
// method to set size
public void setSize( int newSize )
{
if ((newSize >= 1) && (newSize <=3))
size = newSize;
else
size = 1; // assume small
}
// method to set numToppings
public void setNumToppings( int newNumToppings )
{
if ((newNumToppings >=0) && (newNumToppings <= 9))
numToppings = newNumToppings;
else
numToppings = 0; // assume no toppings
}
// method to set toppingDescription
public void setToppingDescription( String newToppingDescription )
{
toppingDescription = newToppingDescription;
}
// method to get size
public int getSize( )
{
return size;
}
// method to get numToppings
public int getNumToppings( )
{
return numToppings;
}
// method to get toppingDescription
public String getToppingDescription( )
{
return toppingDescription;
}
// method to display the pizza
public void displayPizza( )
{
String displayPizzaSize;
if (size == 1)
displayPizzaSize = "Small Pizza";
else if (size == 2)
displayPizzaSize = "Medium Pizza";
else if (size == 3)
displayPizzaSize = "Large Pizza";
else
displayPizzaSize = "Unknown Pizza Size";
if (numToppings > 0)
System.out.printf(" %s with %d toppings(%s).",
displayPizzaSize,numToppings,toppingDescription);
else
System.out.printf(" %s with no toppings.",displayPizzaSize);
System.out.printf(" Cost %.2f ",cost());
}
// method to return the cost of the pizza
public double cost( )
{
// it would be best if these values were read
// from a database or file
final double SMALL_BASE_COST = 7.5;
final double MEDIUM_BASE_COST = 9.25;
final double LARGE_BASE_COST = 12.5;
final double SMALL_TOPPING_COST = .6;
final double MEDIUM_TOPPING_COST = .75;
final double LARGE_TOPPING_COST = .9;
double pizzaCost = 0;
if (size == 1)
pizzaCost = SMALL_BASE_COST + numToppings * SMALL_TOPPING_COST;
else if (size == 2)
pizzaCost = MEDIUM_BASE_COST + numToppings * MEDIUM_TOPPING_COST;
else if (size == 3)
pizzaCost = LARGE_BASE_COST + numToppings * LARGE_TOPPING_COST;
return pizzaCost;
}
}
Explanation / Answer
Image displayed is not complete but what i got from your explanation is that you want to make a Pizza_Order Class that will have few pizza orders to demonstrate the use of pizza class.
Just put this file named Pizza_Order.java in same package as Pizza.java, compile and run
******Here Is the Solution******
public class Pizza_Order
{
public static void main(String args[])
{
Pizza p1=new Pizza(2,4,”Double Cheese”);
Pizza p2=new Pizza(3,5,”Extra Olive”);
Pizza p3=new Pizza(1,8,”Corn”);
p1.displayPizza();
p1.cost();
p2.displayPizza();
p2.cost();
p3.displayPizza();
p3.cost();
System.out.println(“Press any key to continue….”);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.