0% found this document useful (0 votes)
47 views

Lab Exercise 4 - Arrays of Objects

1. Create a Pizza class with fields for toppings, diameter, and price, along with getters and setters. 2. Create a TestPizza class that makes two Pizza objects and demonstrates the get/set methods. 3. Modify TestPizza to prompt the user for the number of pizzas, then use a Pizza array to create that many objects and set each one's fields based on user input for toppings, size, and calculated price based on size. Finally, calculate and display the total price.

Uploaded by

Ku H6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Lab Exercise 4 - Arrays of Objects

1. Create a Pizza class with fields for toppings, diameter, and price, along with getters and setters. 2. Create a TestPizza class that makes two Pizza objects and demonstrates the get/set methods. 3. Modify TestPizza to prompt the user for the number of pizzas, then use a Pizza array to create that many objects and set each one's fields based on user input for toppings, size, and calculated price based on size. Finally, calculate and display the total price.

Uploaded by

Ku H6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

STIA1123 Programming 2

Lab Exercise 4: Arrays of Objects (Ku Hazwan Bin Ku Izham)


289889

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.

(You have to use an array of Pizza objects in this exercise)

Answer :
Number 1:

public class Pizza {


private String toppings;
private int diameter;
private double price;

public Pizza() {
toppings = "";
diameter = 0;
price = 0.0;
}

public String getToppings() {


return toppings;
}

public void setToppings(String toppings) {


this.toppings = toppings;
}

public int getDiameter() {


return diameter;
}

public void setDiameter(int diameter) {


this.diameter = diameter;
}

public double getPrice() {


return price;
}

public void setPrice(double price) {


this.price = price;
}
}

Number 2 :
import java.util.Scanner;

public class TestPizza {


public static void main(String[] args) {
Pizza pizza1 = new Pizza();
pizza1.setToppings("pepperoni");
pizza1.setDiameter(12);
pizza1.setPrice(26.90);

Pizza pizza2 = new Pizza();


pizza2.setToppings("mushroom");
pizza2.setDiameter(10);
pizza2.setPrice(20.50);

System.out.println("Pizza 1 toppings: " + pizza1.getToppings());


System.out.println("Pizza 1 diameter: " + pizza1.getDiameter());
System.out.println("Pizza 1 price: " + pizza1.getPrice());

System.out.println("Pizza 2 toppings: " + pizza2.getToppings());


System.out.println("Pizza 2 diameter: " + pizza2.getDiameter());
System.out.println("Pizza 2 price: " + pizza2.getPrice());
}
}
Number 3 :

import java.util.Scanner;

public class TestPizza {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of pizzas you want to buy: ");
int numPizzas = input.nextInt();

Pizza[] pizzas = new Pizza[numPizzas];

for (int i = 0; i < numPizzas; i++) {


System.out.println("Enter details for Pizza " + (i+1));
pizzas[i] = new Pizza();
System.out.print("Enter toppings: ");
pizzas[i].setToppings(input.next());
System.out.print("Enter diameter in inches: ");
pizzas[i].setDiameter(input.nextInt());
System.out.print("Enter size (small, medium, or large): ");
String size = input.next();
if (size.equals("small")) {
pizzas[i].setPrice(15.90);
} else if (size.equals("medium")) {
pizzas[i].setPrice(26.90);
} else if (size.equals("large")) {
pizzas[i].setPrice(32.90);
} else {
System.out.println("Invalid size entered. Setting price to 0.");
pizzas[i].setPrice(0.0);
}
}

double totalPrice = 0.0;


for (int i = 0; i < numPizzas

You might also like