Lab Exercise 4 - Arrays of Objects
Lab Exercise 4 - Arrays of Objects
1. Create a class named Pizza. Data fields include a String for toppings (such as
“pepperoni”), an integer for diameter in inches (such as 12), and a double for
price (such as 26.90). Include methods to get and set values for each of these
fields.
2. Create an application named TestPizza that instantiates two Pizza objects and
demonstrates the use of the Pizza set and get methods. Save this application as
TestPizza.java.
3. Modify the TestPizza program in Question 2 so that it can ask the user on the
number of pizzas he/she wants to buy. Your program will then create Pizza
objects based on the number entered by the user. Next, for each pizza object, ask
the user for the chosen toppings and size. The Pizza object data values will then
be set according to these user input. For the price of pizza, declare three prices in
the Pizza class which are small = RM15.90, medium = RM26.90 and large =
RM32.90. Set the price data of the Pizza object according to the size entered by
the user. Lastly, calculate and display the total price for all of the pizzas bought.
Answer :
Number 1:
public Pizza() {
toppings = "";
diameter = 0;
price = 0.0;
}
Number 2 :
import java.util.Scanner;
import java.util.Scanner;