Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What leadership style would best characterize Adam Neumann?

Answered: 1 week ago