Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need the Java code for the following: Implement a Java class using the followi

ID: 3605064 • Letter: I

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 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<FoodPrice> 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<FoodPrice> 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

Explanation / Answer

ArrayListExample.java

import java.util.ArrayList;

public class ArrayListExample {

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<FoodPrice> priceList = FoodPrice.createList(food, prices);

for (FoodPrice fp : priceList)

System.out.println("Item: "+fp.getItem() +", Price: $"+fp.getPrice());

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("Item: "+fp.getItem() +", Price: $"+fp.getPrice());

}

}

class FoodPrice{

private String item;

private double price;

public FoodPrice(String item, double price) {

super();

this.item = item;

this.price = price;

}

public static ArrayList<FoodPrice> createList(String[] food, double[] prices) {

ArrayList<FoodPrice> fpList=new ArrayList<FoodPrice>();

FoodPrice fp;

for (int i = 0; i < prices.length; i++) {

fp=new FoodPrice(food[i], prices[i]);

fpList.add(fp);

}

return fpList;

}

public String getItem() {

return item;

}

public void setItem(String item) {

this.item = item;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

}

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