# Lab: Coin Change

## The Problem

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.

## Setup

Follow these steps to set up a project for this lab.

1. Create a new Eclipse project by copying ProjectTemplate (if needed, see Creating a New Project from a Project Template for details). Name the new project CoinChange.
2. Open the src folder of this project and then open (default package). As a starting point you should use ProgramWithIO.java. Rename it CoinChange1 and delete the other files from the project (if needed, see Creating a Program from a Skeleton (also Renaming a Java Program) for details).

## Method

1. 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.
2. 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.
3. 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?

2. Copy CoinChange2.java to create CoinChange3.java. Introduce a private static method called `coinCountsToMakeChange` that has two formal parameters: the number of cents for which change is needed, and the array of coin denominations to be used to make change; and that returns an array of coin counts for the respective denominations. As a model of approximately what this might look like, see `myMethod` in the sample Java code `ProgramWithIOAndStaticMethod.java` in the Project Template.