In: Computer Science
Instructions
1. Create a class named after your favorite object in the whole world. For example, if you love pizza, then create a class called Pizza.java. This class should not have a main method (1 point)
2. Create Instance Variables (attributes) (2 point)
3. Create getter (accessor) and setter (mutator) methods
4. Create a Method to display your data (1 point)
5. Create any other method of your choice that increments or decrements any one of the objects instance fields. Test out the method for at least one of your objects. (2 point)
6. Create 2 Constructors
7. Testing your program (2 points)
8. Put all of your objects in an array, loop through the array, and
call the display method on each object (2
points)
9. Add JavaDoc style comments above all methods and classes (1 point)
/**
* @author pyari
* Defines class Pizza
*
*/
class Pizza
{
// Instance variables to store pizza information
private String topping;
private String size;
private double price;
private int quantity;
/**
* Default constructor to assign "Unknown" to string
instance variables
* 0 to double and integer instance variables
*/
public Pizza()
{
topping = "Unknown";
size = "Unknown";
price = 0.0;
quantity = 0;
}
/**
* Parameterized constructor to assign parameter values
to instance variables
* @param topping - type of topping for pizza
* @param size - size of the pizza
* @param quantity - number of pizza ordered
* @param price - price of each pizza
*/
public Pizza(String topping, String size, int
quantity, double price)
{
this.topping = topping;
this.size = size;
this.price = price;
this.quantity = quantity;
}
/**
* Method to increase the price of the pizza by given
parameter percentage
* @param percent - price percentage to increase
*/
public void increasePriceByPercent(double
percent)
{
price += (price * percent) /
100.0;
}
// Getter methods
public String getTopping()
{
return topping;
}
public String getSize()
{
return size;
}
public double getPrice()
{
return price;
}
public int getQuantity()
{
return quantity;
}
// Setter methods
public void setTopping(String topping)
{
this.topping = topping;
}
public void setSize(String size)
{
this.size = size;
}
public void setPrice(double price)
{
this.price = price;
}
public void setQuantity(int quantity)
{
this.quantity = quantity;
}
/**
* Method to display pizza information
*/
public void showPizza()
{
System.out.println("\n Topping: " +
topping + "\n Size: " + size +
"\n Quantity: " + quantity + "\n Price: $" +
price +
"\n Amount to pay: $" + (quantity *
price));
}
}// End of class Pizza
/**
* @author pyari
* Driver class to test the pizza class methods
*
*/
public class PizzaDriver
{
// main method definition
public static void main(String []s)
{
// Declares an array of object of
class Pizza of size 4
Pizza [] pizza = new
Pizza[4];
// Creates a pizza object using
default constructor
pizza[0] = new Pizza();
// Calls the setter method to set
data
pizza[0].setTopping("Mushrooms");
pizza[0].setSize("Small");
pizza[0].setQuantity(10);
pizza[0].setPrice(50.63);
// Creates a pizza object using
default constructor
pizza[1] = new Pizza();
// Calls the setter method to set
data
pizza[1].setTopping("Cheese");
pizza[1].setSize("Large");
pizza[1].setQuantity(4);
pizza[1].setPrice(90.12);
// Creates pizza object using
parameterized constructor
pizza[2] = new Pizza("Onions",
"Medium", 8, 61.12);
pizza[3] = new Pizza("Pepperoni",
"extraLarge", 6, 78.33);
// Loops till number of pizza
ordered
for(int c = 0; c < pizza.length;
c++)
// Calls the
method to display each pizza information
pizza[c].showPizza();
// Calls the method to increase the
pizza price by 10%
pizza[1].increasePriceByPercent(10.0);
System.out.println("\n After
increasing the price of pizza One by 10%");
// Calls the method to display
pizza information
pizza[1].showPizza();
}// End of main method
}// End of driver class
Sample Output:
Topping: Mushrooms
Size: Small
Quantity: 10
Price: $50.63
Amount to pay: $506.3
Topping: Cheese
Size: Large
Quantity: 4
Price: $90.12
Amount to pay: $360.48
Topping: Onions
Size: Medium
Quantity: 8
Price: $61.12
Amount to pay: $488.96
Topping: Pepperoni
Size: extraLarge
Quantity: 6
Price: $78.33
Amount to pay: $469.98
After increasing the price of pizza One by 10%
Topping: Cheese
Size: Large
Quantity: 4
Price: $99.132
Amount to pay: $396.528