Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 " + + " cents. NOT enough money in the piggy bank." If the balance of the piggy bank is larger than the withdrawal amount, two cases should be considered. 1. If the exact change can be made, your withdraw() method must remove from the given piggy bank the least number of coins needed to fulfill the request. 2. If the exact change cannot be made, your withdraw() method have to remove more money than the requested amount based on which coins are stored in the piggy bank. In this case, rather than minimizing the number of coins to be removed to fulfill the request, your algorithm must minimize the amount over. For example, if there are 3 pennies, 1 nickel, 1 dime, and 2 quarters in the piggy bank. If the withdraw() method is called to remove 39 cents, when minimizing the amount over, 1 quarter, 1 dime, and 1 nickel should be removed (a total of 40 cents).

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

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Brad Bulger, Jay Greenspan, David Wall

2nd Edition

ISBN: 0764549634, 9780764549632

More Books

Students also viewed these Databases questions

Question

Establish Eqs. (8.12) and (8.13).

Answered: 1 week ago