Question: I need the Java code for the following: Implement a Java class using the following UML diagram. Be sure to use the correct access specifiers

I need the Java code for the following:

Implement a Java class using the following UML diagram. Be sure to use the correct access specifiers for the class, instance variables and methods. Mutators and accessors must be implemented in your class as specified in the UML diagram. The createList method is a static method, as indicated by the underlined signature. This method takes two parallel arrays; one array contains the food name and the other the food price. It declares an ArrayList containing FoodPrice Objects, creates FoodPrice objects out of each parallel array element, then inserts each FoodPrice object into an ArrayList. The ArrayList is returned to the caller.

To declare an ArrayList named "fp" which stores FoodPrice objects, use the following:

ArrayList fp = new ArrayList();

To add FoodPrice objects the the ArrayList fp using the 2-argument constructor shown in the UML diagram, use the following:

fp.add(new FoodPrice(items[i], prices[i]));

Use the following main method in your class to test it (don't modify this code)

// Main method for unit test public static void main(String[] args) { final String[] food = { "Tomatoes", "Ground Beef", "Pasta", "Bread", "Cheese" }; final double[] prices = { 1.99, 5.49, 0.99, 2.57, 3.59 }; System.out.println("Original list: "); ArrayList priceList = FoodPrice.createList(food, prices); for (FoodPrice fp : priceList) System.out.println(fp); System.out.println(); System.out.println("New List: "); FoodPrice cheese = priceList.get(4); // should be Cheese cheese.setItem("American Cheese"); // new name cheese.setPrice(4.59); // new price priceList.set(4, cheese); // change the cheese in the list for (FoodPrice fp : priceList) System.out.println(fp); }

Expected output:

Original list: Item: Tomatoes, Price: $1.99 Item: Ground Beef, Price: $5.49 Item: Pasta, Price: $0.99 Item: Bread, Price: $2.57 Item: Cheese, Price: $3.59 New List: Item: Tomatoes, Price: $1.99 Item: Ground Beef, Price: $5.49 Item: Pasta, Price: $0.99 Item: Bread, Price: $2.57 Item: American Cheese, Price: $4.59

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!