Question
Calculating change is something that we have been trained to do from a young age. For example, without thinking about it, we all know how
Calculating change is something that we have been trained to do from a young age. For example, without thinking about it, we all know how many quarters, dimes, nickels and pennies we should receive if the change from our purchase is 47 cents. In this lab, you will write a program to convert a given amount of money (in cents) into change. Notice, however, that there are many ways to calculate change. That is, if our change is 47 cents, we could use 47 pennies. Or we could use 3 dimes and 17 pennies. Or we could use 4 dimes, a nickel, and 2 pennies. You get the idea. What we would really like to do is use a minimal number of coins. For 47 cents, one way to use a minimal number of coins is 1 quarter, 2 dimes, and 2 pennies. For this lab, you will implement a greedy solution for the problem of calculating change. What is a greedy solution? It is quite simple. Assume you want to convert an arbitrary number of cents into coins of the following denominations: dollar, half-dollar, quarter, dime, nickel, and penny. Use as many dollar coins as possible; then, on what's left over, use as many half-dollar coins as possible; then again as many quarters as possible; then as many dimes as possible; then as many nickels as possible; finally the remainder is in pennies. Edit CoinChange1.java (including updating comments appropriately) to ask the user for an amount (number of cents) to make change for and then use the greedy solution to compute and output the numbers of coins of each kind required to make that amount of change. As mentioned before, you should use coins of the following denominations: dollar, half-dollar, quarter, dime, nickel, and penny. Hint: if you have any loops in your code, try again. Copy CoinChange1.java to create CoinChange2.java (right-click on CoinChange1.java to get the contextual pop-up menu and choose Copy, then right-click on (default package) and choose Paste, providing the new name CoinChange2). Modify the program so that it solves the same problem, with the same greedy solution, but uses arrays (one for the coin denominations and one for the coin counts) and one loop with a simple body to replace the longer straight-line code in CoinChange1.java. Note that this change also generalizes the solution so it can be easily modified, by changing a single line of code, to work with other coin denominations. Can you convince yourself and your partner that the greedy solution is optimal, i.e., it solves the problem with a minimum number of coins?
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