When I was a freshmen in high school, one our earliest programs was to find the correct change for some number of cents. I remember when I wrote that first program, I did it an atypical way and got some strange looks. Instead of using nested modulus like everyone else, I used integer division and then subtracted out the difference from a running total.
Years later, I thought it would be cool to redo that program and it certainly was fun. I used the same methodology in fact, but with a couple of optimizations.
I start the program off by creating a Scanner object for user input and a prompt.
Scanner scanner = new Scanner(System.in);
System.out.println("Enter some amount in cents: ");
int cents = scanner.nextInt();
Then the optimizations kick in. There is a denominations array, which has a list of coin values, from 1 dollar, a quarter, a dime, a nickel and finally a penny.
int[] denominations = { 100, 25, 10, 5, 1 };
One of the original requirements of the program was that the amount of coins needed must be in their proper plurality. I can do this easily by having a two dimensional array of strings that store both forms of each word. You might ask, why not just attach an extra s to the end if it’s plural? Well, penny doesn’t turn into pennys.
String[][] terms = { { "dollar", "dollars" },
{ "quarter", "quarters" }, { "dime", "dimes" },
{ "nickle", "nickles" }, { "penny", "pennies" } };
The program will spit out values for each of the denominations listed, so it will contains dollars, quarters and so on and it’s store in this values array.
int[] values = new int[denominations.length];
I use a rolling total to keep track of how much change is left over still.
int rolling = cents;
The magic happens in the for-loop. It’s completely standard, initialized to 0, counting towards the length of the denominations array and incrementing by 1.
for (int i = 0; i < denominations.length; i++) {
The calculations happen in the next section. Basically, the denomination integer pulls the proper value from the denominations array. Then, the value in the values array is set to the integer division of the rolling total and current denomination. Finally, the rolling value is truncated by the denomination by modulus.
int denomination = denominations[i];
values[i] = (rolling / denomination);
rolling = rolling % denomination;
To pick the proper singular or plural, the value is checked against being 1. Keep in mind that values of fractions are indeed plural. You have .1 dollars, e.g. ten cents, or a dime.
String term = (values[i] == 1 ? terms[i][0] : terms[i][1]);
Finally, there is the printing logic which always gets in the way. I certainly wish there was a better way, but this was the best I found. Basically, by counting up until the last value, I print without a new line the value and then the matching proper term, and when it reaches the last value, I print an and, value, term and finally a period.
if (i < (denominations.length - 1)) {
System.out.print(values[i] + " " + term + " ");
} else {
System.out.println("and " + values[i] + " " + term);
}
Here’s a short sample run with different amounts of cents.
Ryans-MacBook-Air:APCS3 ryanrampersad$ java CorrectChange Enter some amount in cents: 119 Your change is: 1 dollar 0 quarters 1 dime 1 nickle and 4 pennies. Ryans-MacBook-Air:APCS3 ryanrampersad$ java CorrectChange Enter some amount in cents: 169 Your change is: 1 dollar 2 quarters 1 dime 1 nickle and 4 pennies. Ryans-MacBook-Air:APCS3 ryanrampersad$
I remember when I wrote this program four years ago. This program is 35 lives with a bunch of whitespace. My code from youth was probably more like 70 lines. As you code, you mature. As you read others’ code, you mature too. Experimenting is the best way to grow. You can of course download the full source code.
Happy counting.
