// This program prompts shows a user all of the ways to make change for // a specific amount. import java.util.*; public class Change { public static void main(String[] args) { System.out.println(" P N D Q"); System.out.println("------------"); showChange(28); } // Shows all ways to make change for the given amount public static void showChange(int amount) { List coinValues = new LinkedList(); coinValues.add(1); coinValues.add(5); coinValues.add(10); coinValues.add(25); // ... your code goes here ... Stack chosen = new Stack(); explore(amount, coinValues, chosen); } // Shows all ways to make change for the given amount and coin values, // given the stack of existing number of coins already chosen. private static void explore(int amount, List coinValues, Stack chosen) { if (coinValues.isEmpty()) { // base case: no coins left to choose. if (amount == 0) { System.out.println(chosen); } } else { // recursive case: grab coin value (penny, nickel, etc.) from list int coinValue = coinValues.remove(0); for (int i = 0; i <= (amount / coinValue); i++) { // try each value i from 0 to the max # possible of this coin chosen.push(i); // explore the other coin values recursively explore(amount - (i * coinValue), coinValues, chosen); chosen.pop(); // unmake the choice (1st half) } coinValues.add(0, coinValue); // unmake the choice (2nd half) } } }