QuestionQuestion

Make Change Calculator
in this program (closely related to the change calculator done as the prior assignment) you will make “change for a dollar” using the most efficient set of coins possible.In Part A you will give the fewest quarters, dimes, nickels, and pennies possible (i.e., without regard to any ‘limits’ on coin counts), but in Part B you will re-do the program to give change from a specific number of available coins.
Part A: Basic Change for a dollar:
In this version of the program you will give change for a dollar in the most efficient manner (i.e., fewest number of coins of each type).
A sample run of the program might look as follows:
Welcome to the Make Change calculator!
What value would you like change for? (1-100 cents, or 0=quit): 23
For 23 cent(s) I give: 0 quarters, 2 dimes, 0 nickels, 3 pennies
What value would you like change for? (1-100 cents, or 0=quit): 1
For: 1 cent(s) I give: 0 quarters, 0 dime, 0 nickels, 1 pennies
What value would you like change for? (1-100 cents, or 0=quit): 100
For: 100 cent(s) I give: 4 quarters, 0 dime, 0 nickels, 0 pennies
What value would you like change for? (1-100 cents, or 0=quit): 0
Thanks for using the Make Change calculator!

Part A Required Elements: the following features must be present for full credit:
• Declare as global only those items that must be used across methods
• Minimize the code inside main, but include a loop so the user can enter change values multiple times (entry of a zero terminates the loop)
• Code a separate method to obtain and validate the input value as being from 0 to 100
Validation should include a try/catch structure to avoid crashing the program on non-integer input
• Code a separate method to determine and print the actual change returned

Part B: Perfect change with specific coin counts:
The claim has been made that when receiving change for any amount up to and including $1 you can always make that change with only the following coins: 3 quarters, 1 dime, 2 nickels, and 5 pennies. You will write a program to test that assumption. To do this, you will add a method to MakeChange that runs at the beginning of the program to ask if a specific set of coins is being used (but note that the program may still run in the old way also). A sample run might be as follows:
Welcome to the Make Change calculator!
Do you have a specific set of coins for making change? (Y/N): Y
No. of Quarters: 3
No. of Dimes: 1
No. of Nickels: 2
No. of Pennies: 5
What value would you like change for? (1-100 cents, 101=New Coin set, or 0=quit): 23
For 23 cent(s) I give:
0 quarters, 1 dime, 2 nickels, 3 pennies leaving
3 quarters, 0 dime, 0 nickels, 2 pennies
What value would you like change for? (1-100 cents, 101=New Coin set, or 0=quit): 100
For: 100 cent(s) I give:
3 quarters, 1 dime, 2 nickels, 5 pennies leaving:
0 quarters, 0 dime, 0 nickels, 0 pennies
What value would you like change for? (1-100 cents, 101=New Coin set, or 0=quit): 101
Do you have a specific set of coins for making change? (Y/N): Y
No. of Quarters: 3
No. of Dimes: 1
No. of Nickels: 2
No. of Pennies: 2
What value would you like change for? (1-100 cents, 101=New Coin set, or 0=quit): 4
I could not make change for: 4 cents (I am short: 2 cents)
What value would you like change for? (1-100 cents, 101=New Coin set, or 0=quit): 0
Thanks for using the Make Change calculator!
In this version the ‘how much change’ question now has a new option: 101. If the user selects this option, the program should run the method that asks if a coin set is being used and, if the operator answers yes, then asks for the specific coin counts (i.e., the same method that now runs at the beginning of the program).
Now during operation, the program must know if a specific set of coins has been entered: if so, then a new method called ‘PerfectChange’ will be used to determine the coins to be given in change. But, if a specific set of coins was not used, the program should operate in exactly the same way is it did in Part A (i.e., giving the fewest possible coins as change for $1).   
Finally, note that the user may go back and forth between using a coin set and not. By using option 101 but answering ‘N’ to the ‘Do you have a specific set of coins’ the user effectively turns off the PerfectChange feature and returns to making change in the most efficient way possible.
Also note that in the ‘specific coins’ scenario, it is possible that perfect change cannot be given (see sample run above).
Call the program MakeChange.java and zip the finished netbeans project from the top-level folder. Submit it through blackboard as a separate zip file, as discussed in class.

Extra Credit
Add another option to the ‘how much change’ question (i.e., add an item 102) that produces an ‘all’ output result. Thus, if the user enters 102, the program should loop to produce results for all values 1 through 100 (using the coin counts if entered or the original ‘most efficient’ method – whichever is currently selected in the program). This option should not fundamentally modify any of the existing methods in the program, but merely add a new method to loop through all possible values from 1 – 100 (making use of the other program methods, as needed). New parameter passing may be needed for this operation, depending on program structure

Make Change Program (Part A)
Imports:
java.util.Scanner
Global Variables:
Scanner sc – scanner object to read System.in
Methods:
static void main(String[] args) – process control with loop for multiple runs
static int getCents() – obtain and validate input of change being requested
static void makeChange(int cents) – calculate and display coins given in change
Process:
main(String[] args):
local variables: int cents – variable for change amount being requested
1. Display welcome message
2. getCents  cents
3. while loop on cents != 0
a. makeChange(cents)
b. getCents  cents
4. Display end message
getCents():
local variables: int c – cents input value to return
1. do - loop to control input/validation process
a. Try
i. How much change?  c
ii. c < 0?  error message
iii. c > 100?  error message
b. Catch
i. Illegal input message
ii. Clear input buffer
iii. Set c to sentinel value (to force loop to repeat)
         while (c < 0 or c > 100)
2. return c;
makeChange(int cents):
local variables: int q,d,n,p – for quarters, dimes, nickels, pennies to give
int r – working variable for ‘remaining cents’ to give
1. set r = cents
2. Determine Quarters to give:
a. q = r / 25 (integer division)
b. adjust r to new remaining value: r = r – (q * 25)
3. Repeat step 2 for dimes, nickels (in that order)
4. Remaining r after step 3 is given as pennies
5. Print results

MakeChange Program Changes for: Part B
Global Variables:
    Add: int qoh, doh, noh, and poh for Quarters-on-Hand, Dimes-on-Hand, Nickels-on-Hand, and Pennies-on-Hand
Methods:
Add: int getCoins(String cointype) – to validate and return coins on hand for each ‘cointype’
Specific method changes:
main():
Add logic to request each coin type using getCoins() and place in proper global variable (qoh, doh, etc.)
Int getCoins(String cointype):
Code is virtually the same as getCoins method from prior assignment (calcChange)
makeChange(int cents):
Changes to algorithm: basic process is the same, except after determining each coin count, value must be checked against coins on hand, and adjusted accordingly. So, for prior step 2 you would now have:
Determine Quarters to give:
a. q = r /25 (integer division)
b. if q > qoh, adjust q to equal qoh
c. adjust r as: r = r – (q * 25)
The final step (print results) must also check that r has been worked down to zero: if not, perfect change could not be given from the available set of coins.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

package makechange;


import java.util.Scanner;

public class MakeChange {

static Scanner sc = new Scanner(System.in);
static int qoh, doh, noh, poh;

public static void main(String[] args) {
    int cents;
    boolean usingset;
    System.out.println("Welcome to the Make Change Calculator!");
   
    usingset = getCoinSet();//two steps:Y/N on using a coinset,
    //if yes: fill qoh, doh, noh, poh

    cents = getCents();
    while (cents != 0){
      if (cents == 101) {
       usingset= getCoinSet();
      } else {
       if (usingset == false) {
          makeChange(cents);
       } else {
          perfectChange(cents);
       }
      } //end of else
      cents = getCents();
    } //end of while   
   
   
   
    System.out.println("Thanks for using the Make Change calculator!");
}//end of main

public static int getCents() {
    int c;
    do {
      try{
       System.out.print("What value would you like change for? (1-100 cents, 101=New Coin set, or 0=quit): " );
       c = Integer.parseInt(sc.nextLine());
       if (c < 0 || c>101){
          System.out.println("Input out of range: 0-101 only");
       }
      
      }catch (Exception e) {
       System.out.println("Illegal input: 0-101 only");
       sc.nextLine();
       c = -1;
      }
    } while (c<0 || c>101);
    return c;
   
}//end of getCents
public static void makeChange(int cents) {
    //System.out.println("Cents required = " + cents);
   
    int q, d, n, p, r;
    r = cents;
   
    q = r/ 25; //integer division
   
    r = r - (q * 25);
   
    d = r /10;
    r = r - (d * 10);
   
    n = r/ 5;
    r = r - (n * 5);
   
    p=r;
    System.out.println("For " + cents + " cent(s), I give: " +
                      q + " quarters, " +
                      d + " dimes, " +
                      n + " nickels, " +
                      p + " pennies");
   
   
} //end of makeChange
public static boolean getCoinSet...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

50% discount

Hours
Minutes
Seconds
$12.00 $6.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats