package rebelsky.exam2; /** * Something that helps us count coints. * * @author Samuel A. Rebelsky * @author YOUR NAME HERE */ public class CoinCounter { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** * The valid coin values. */ int[] denominations; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** * Build a new coin counter for a particular set of denominations. * * @pre * The values in _denominations must be stored in * decreasing order. * @pre * All values in _denominations must be positive. */ public CoinCounter(int[] _denominations) { this.denominations = _denominations; } // CoinCounter(int[]) // +----------------+------------------------------------------ // | Public Methods | // +----------------+ /** * Determine the fewest number of coins necessary to * make a particular price. * * @exception Exception * If it is not possible to make that price. */ public int fewestCoins(int price) throws Exception { // Simple solution: No coins needed for no price. if (price == 0) return 0; // Fewest represents the fewest coins we've done so far int fewest = Integer.MAX_VALUE; for (int i = 0; i < this.denominations.length; i++) { int coin = this.denominations[i]; if (coin <= price) { try { int sub = this.fewestCoins(price-coin); if (sub+1 < fewest) fewest = sub+1; } // try catch (Exception e) { // It's okay we failed, we'll just // try a different denomination. } // catch } // if (coin <= price) } // for // Make sure that we found a solution if (fewest == Integer.MAX_VALUE) throw new Exception("Can't make change for " + price); return fewest; } // fewestCoins(int) } // class CoinCounter