Question
Complete in Java Given this code for a coin bank program, import java.util.Random; class PiggyBank { public final static int[] COINS_VALUES = { 1, 5,
Complete in Java
Given this code for a coin bank program,
import java.util.Random;
class PiggyBank {
public final static int[] COINS_VALUES = { 1, 5, 10, 25 }; // coins values in cents
// coins names
public final static String[] COINS_NAMES = { "PENNY", "NICKEL", "DIME", "QUARTER" };
public final static String NO_SUCH_COIN = "N/A"; // N/A string
private final static Random RAND_GEN = new Random(100); // generator of random integers
/**
* Returns the name of a specified coin value
* @param coin represents a coin value in cents.
* @return the String name of a specified coin value if it is valid and N/A if
* the coin value is not valid
*/
public static String getCoinName(int coin) {
for (int i = 0; i < COINS_VALUES.length; i++) {
// if coin found
if (COINS_VALUES[i] == coin) {
return COINS_NAMES[i];
}
}
// if not found return N/A
return "N/A";
}
/**
* Returns the balance of a piggy bank in cents
* @param coins an oversize array which contains all the coins held in a piggy bank
* @param size the total number of coins stored in coins array
* @return the total value of the coins held in a piggy bank in cents
*/
public static int getBalance(int[] coins, int size) {
int total_balance = 0;
for (int i = 0; i < size; i++) {
total_balance += coins[i];
}
return total_balance;
}
/**
* Returns the total number of coins of a specific coin value held in a piggy bank
* @param coinValue the value of a specific coin
* @param coins an oversize array which contains all the coins held in a piggy bank
* @param size the total number of coins stored in coins array
* @return the number of coins of value coinValue stored in the array coins
*/
public static int getSpecificCoinCount(int coinValue, int[] coins, int size) {
int count = 0;
for (int i = 0; i < size; i++) {
if (coins[i] == coinValue)
count++;
}
return count;
}
/**
* Displays information about the content of a piggy bank
* @param coins an oversize array storing the coins held in a piggy bank
* @param size number of coin held array coins
*/
public static void printPiggyBank(int[] coins, int size) {
for (int i = 0; i < size; i++)
System.out.print(coins[i] + " ");
System.out.println();
}
/**
* Adds a given coin to a piggy bank. This operation can terminate successfully
* or unsuccessfully. For either cases, this method displays a descriptive
* message for either cases.
* @param coin the coin value in cents to be added to the array coins
* @param coins an oversize array storing the coins held in a piggy bank
* @param size the total number of coins contained in the array coins before
* this addCoin method is called.
* @return the new size of the coins array after trying to add coin.
*/
public static int addCoin(int coin, int[] coins, int size) {
if (coins.length > size) {
coins[size] = coin;
System.out.println("Coin inserted succesfully");
size++;
return size;
} else {
System.out.println("Can't insert coin!");
return size;
}
}
/**
* Removes an arbitrary coin from a piggy bank. This method may terminate
* successfully or unsuccessfully. In either cases, a descriptive message is
* displayed.
* @param coins an oversize array which contains the coins held in a piggy bank
* @param size the number of coins held in the coins array before this method is called
* @return the size of coins array after this method returns successfully
*/
public static int removeCoin(int[] coins, int size) {
int to_remove = RAND_GEN.nextInt(size);
// remove coin at this location
size--;
// move whole coins to 1 left
for (int i = to_remove; i < size; i++) {
coins[i] = coins[i + 1];
}
return size;
}
/**
* Removes all the coins in a piggy bank. It also displays the total value of the removed coins
* @param coins an oversize array storing the coins held in a piggy bank
* @param size number of coins held in coins array before this method is called
* @return the new size of the piggy bank after removing all its coins.
*/
public static int emptyPiggyBank(int[] coins, int size) {
// print total balance
System.out.println("Total value: " + getBalance(coins, size));
// remove all coins
size = 0;
return size;
}
Write the withdraw method given these constraints:
/**
* Removes the least number of coins needed to fulfill a withdrawal request
* @param amount amount to withdraw given in cents
* @param coins an oversize array storing the coins held in a piggy bank
* @param size number of coins stored in coins array before this method is called
* @return perfect size array of 5 elements, index 0 stores the new size of the
* piggy bank after this method returns. Indexes 1, 2, 3, and 4 store
* respectively the number of removed quarters, dimes, nickels, and
* pennies.
*/
public static int[] withdraw(int amount, int[] coins, int size) {
If the piggy bank does not contain the requested amount, then the withdraw method should display the following error message. "Unable to withdraw " +
You must edit the original coins array, do not set a new array.
You must to move all of the zeroes to the end of that coins array.
Some more examples:
Withdraw 40 cents from a piggy bank which stores 2 pennies, 3 nickels, 2 dimes, and 2 quarters. Your method should return the following array of ints [6, 1, 1, 1, 0].
Withdraw 44 cents from a piggy bank which stores 3 pennies, 2 nickels, 1 dime, and 3 quarters. Your method should return the following array of ints [5, 1, 1, 2, 0].
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