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

How do I change this line of code to make it fit with an ArrayList instead of a

ID: 3727381 • Letter: H

Question

How do I change this line of code to make it fit with an ArrayList instead of a regular array?

Here's my code:

import java.util.ArrayList;

public class Checkout
    {
        private int size;
        private int amount;
        private int sum;
        private double taxRate;
        private ArrayList <DessertItem> dessertItems;
       
        public Checkout()
        {
            size = 100;
            amount = 0;
            sum = 0;
            dessertItems = new ArrayList <DessertItem>();
            taxRate = DessertShoppe.TAX_RATE;
        }
       
        public void enterItem(DessertItem item)
        {
            dessertItems.add (item);
            amount++;
        }
       
        public int numberOfItems()
        {
            return amount;
        }
       
        public int totalCost()
        {
            sum = 0;
            for (int i = 0 ; i < amount ; i++)
                {
                    sum += dessertItems[i].getCost ();
                }
            return sum;
        }
       
        public int totalTax()
        {
            return (int) (Math.round(this.totalCost() * taxRate / 100));
        }
       
        public void clear()
        {
            for (DessertItem item : dessertItems)
                {
                    item = null;
                }
             amount = 0;
             sum = 0;
        }
       
        public String toString()
        {
            String receipt = "Thank You!";
            receipt += DessertShoppe.STORE_NAME + " ";
            receipt += "Purchased: ";
            String totalPay = DessertShoppe.cents2dollarsAndCents(totalCost() + totalTax());
            if (totalPay.length() > DessertShoppe.COST_WIDTH)
                {
                    totalPay = totalPay.substring(0, DessertShoppe.COST_WIDTH);
                }
            receipt += "$" + totalPay;
            return receipt;
         }
       
       
    }

This is the error code I get for the bolded line of code:

The type of the expression must be an array type but it resolved to ArrayList<DessertItem>

Explanation / Answer

If you have

ArrayList<DessertItem> dessertItems = new ArrayList <DessertItem>();

(1) To add a new element to this ArrayList dessertItems you must use method add

ex- dessertItems.add(new_Item)

(2) To read item from an ArrayList at particular index , you must use get(index) method.

ex- dessertItems.get(2) => this will return object stored at index 2

*******************************************************************************************

So in your code , you are trying to access element at index like an array which is not supported in ArrayList

so please use get method at that bolded line

repalce that line with below line

************************Your modified class*******************************

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote