/* Change.java -- define a "Change" class to determine how many pennies, * nickels, dimes and quarters equal a certain amount of change. * There's more than one way to do this, but I think the easiest way * is to make an attribute out of all these numbers (int's). * The purpose of this example program is to practice with the / and % * operations. */ public class Change { private int cents; private int pennies; private int nickels; private int dimes; private int quarters; // Constructor -- let's assume we start out knowing # cents public Change(int money) { cents = money; // compute the number of quarters, and then subtract them // from the total. But we don't want to change the value of // "cents". So we'll keep track of how much change is left // in a separate variable changeLeft. quarters = cents / 25; int changeLeft = cents % 25; // compute dimes in a similar way dimes = changeLeft / 10; changeLeft = changeLeft % 10; // and do the same for nickels -- but then the change left over // will simply be the number of pennies nickels = changeLeft / 5; pennies = changeLeft % 5; } // Output function. Note that the '\t' character is a tab for indenting. public void printCoins() { System.out.println("\nTo give " + cents + " cents change, you need:"); System.out.println("\t" + quarters + " quarters"); System.out.println("\t" + dimes + " dimes"); System.out.println("\t" + nickels + " nickels"); System.out.println("\tand " + pennies + " pennies"); } }