Question: What is wrong with my JAVA Code? When I run the Compiler in Eclipse, it does not run. Please propose a fix. I was trying

What is wrong with my JAVA Code? When I run the Compiler in Eclipse, it does not run. Please propose a fix. I was trying to answer the following question.

Reimplement the CashRegister class so that it keeps track of the price of each added item in an ArrayList. Remove the itemCount and totalPrice instance variables. Reimplement the clear, addItem, getTotal, and getCount methods. Add a method displayAll that displays the prices of all items in the current sale.

--------------------

/** * Re-implement the CashRegister class so that it keeps track of the total price as an integer: the total cents of the price. **/

import java.util.ArrayList;

public class CashRegister02 { private ArrayList prices; public CashRegister02() { prices = new ArrayList<>(); } // Adds an item to this cash register. public void addItem(double price) { prices.add((int) (price * 100)); }

// Gets the price of all items in the current sale.

public double getTotal() { double total = 0; for (double el : prices) { total += el; } return total / 100; } // Gets the number of items in the current sale.

public int getCount() { return prices.size(); }

// Clears the item count and the total using clear() method for ArrayList.

public void clear() { prices.clear(); }

// Displays all prices in the array.

public void displayAll() { for (int el : prices) { System.out.print(el + " "); } } }

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!