Question
Please help me to correct a coin dispenser java program. import java.io.BufferedReader; import java.io.InputStreamReader; /** * Coin Dispenser * Allows a user to set a
Please help me to correct a coin dispenser java program.
import java.io.BufferedReader; import java.io.InputStreamReader;
/** * Coin Dispenser * Allows a user to set a starting value and dispense that value into the * amount of quarters, dimes, nickels and pennies. The starting value should * be positive and in the value of pennies. I.e. $1.52 should be 152. */ public class CoinDispenser { // Details the value remaining to dispense private int _remainingValue = 0; /** * Sets the starting value to be dispensed * @param startValue */ public void setStartValue(int startValue) { _remainingValue = startValue > 0 ? startValue : 0; } /** * @return the remaining value to be dispensed */ public int getRemainingValue() { return _remainingValue; } /** * Dispenses the remaining value in terms of quarters, leaving the * remaining balance as the remaining value. * * @return amount of quarters that can be dispensed */ public int dispenseQuarters() { return dispenseCoin(25); } /** * Dispenses the remaining value in terms of dimes, leaving the * remaining balance as the remaining value. * * @return amount of quarters that can be dispensed */ public int dispenseDimes() { return dispenseCoin(10); } /** * Dispenses the remaining value in terms of nickels, leaving the * remaining balance as the remaining value. * * @return amount of quarters that can be dispensed */ public int dispenseNickels() { return dispenseCoin(5); } /** * Dispenses the remaining value in terms of pennies, leaving the * remaining balance as the remaining value. * * @return amount of quarters that can be dispensed */ public int dispensePennies() { // TODO: Remove line below return dispenseCoin(1); } /** * Dispenses the remaining value in terms of a given coin value. * Leaving the remaining balance as the new remaining value, and * returning the amount of coins of the particular given value that * can be dispensed. * * @param coinValue, value of the coin to be dispensed * @return amount of the given coin value that can be dispensed */ private int dispenseCoin(int coinValue) { int count = 0; // check if the remaining value can disburse at least one coin, // if not do not perform any calculations if (_remainingValue > coinValue) { count = _remainingValue / coinValue; _remainingValue %= coinValue; } return count; } /** * @param args */ public static void main(String[] args) { CoinDispenser dispenser = new CoinDispenser(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int value = 0; do { System.out.println("Enter the value to dispense in pennies (0 to quit):"); try { value = Integer.parseInt(br.readLine()); } catch (Exception e) { e.printStackTrace(); return; } dispenser.setStartValue(value); System.out.printf("Quarters: %d%nDimes: %d%nNickels: %d%nPennies: %d%n",dispenser.dispenseQuarters(),dispenser.dispenseDimes(), dispenser.dispenseNickels(),dispenser.dispensePennies()); } while (value != 0); }
}
There is an error at dispenseCoin. I need help
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started