Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.miracosta.cs113.change;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- /**
- * ChangeCalculator : Class containing the recursive method calculateChange, which determines and prints all
- * possible coin combinations representing a given monetary value in cents.
- * <p>
- * Problem derived from Koffman & Wolfgang's Data Structures: Abstraction and Design Using Java (2nd ed.):
- * Ch. 5, Programming Project #7, pg. 291.
- * <p>
- * NOTE: An additional method, printCombinationsToFile(int), has been added for the equivalent tester file to
- * verify that all given coin combinations are unique.
- */
- public class ChangeCalculator {
- //coin value
- static int[] coins = {1, 5, 10, 25};
- //combination of array.
- /*
- Example. "15"
- 1 1 1 1 1 1 1... (15 times) pennnies
- 2 2 2 2 2 ( nickel and 10 pennies ) 3 3 3 3 3 (2nickel 5 pennies) 4 4 4 4 4(3nickel 0 penny)
- 5 5 5 5 5 (dime and 5penny) 6 6 6 6 6 (dime nickel)
- */
- static int[] combinations;
- /*
- ArrayList to track the combinations in string
- */
- private static ArrayList<String> combos = new ArrayList<>();
- public static void main(String[] args) {
- System.out.println(calculateChange(15));
- }
- /**
- * Wrapper method for determining all possible unique combinations of quarters, dimes, nickels, and pennies that
- * equal the given monetary value in cents.
- * <p>
- * In addition to returning the number of unique combinations, this method will print out each combination to the
- * console. The format of naming each combination is up to the user, as long as they adhere to the expectation
- * that the coins are listed in descending order of their value (quarters, dimes, nickels, then pennies). Examples
- * include "1Q 2D 3N 4P", and "[1, 2, 3, 4]".
- *
- * @param cents a monetary value in cents
- * @return the total number of unique combinations of coins of which the given value is comprised
- */
- public static int calculateChange(int cents) {
- //Create a static variable that holds the amount of cents. Include one more for empty value.
- combinations = new int[cents + 1];
- //Set the first element to zero because there's only one way to make 0cents. i.e 0 coints.
- combinations[0] = 1;
- // Iterate with the number of coins we have
- for (int i = 0; i < coins.length; i++) {
- // Once its higher than its value we can store it and make a counter.
- for (int j = 0; j < combinations.length; j++) {
- if (coins[i] <= j) //once its value is bigger than the loop we can keep expanding our count. recursively using an array.
- // Update the array by grabbing its old value and adding to it with the new number
- combinations[j] += combinations[j - coins[i]];
- }
- }
- // return the value at the position of cents (last index)
- makeChange(cents, 0, 0, 0, cents);
- printCombinationsToFile(cents);
- return combinations[cents];
- }
- public static void makeChange(int total, int q, int d, int n, int p) {
- final int QUARTER = coins[3], DIME = coins[2], NICKEL = coins[1], PENNY = coins[0];
- if (q * QUARTER + d * DIME + n * NICKEL + p * PENNY > total) {
- return;
- }
- //Combination in string
- String s = "[" + q + ", " + d + ", " + n + ", "
- + p + "]";
- if (!combos.contains(s))
- combos.add(s);
- // Recursive Cases
- if (p >= 5)
- makeChange(total, q, d, n + 1, p - 5);
- if (p >= 10)
- makeChange(total, q, d + 1, n, p - 10);
- if (p >= 25)
- makeChange(total, q + 1, d, n, p - 25);
- }
- /**
- * Calls upon calculateChange(int) to calculate and print all possible unique combinations of quarters, dimes,
- * nickels, and pennies that equal the given value in cents.
- * <p>
- * Similar to calculateChange's function in printing each combination to the console, this method will also
- * produce a text file named "CoinCombinations.txt", writing each combination to separate lines.
- *
- * @param cents a monetary value in cents
- */
- public static void printCombinationsToFile(int cents) {
- // TODO:
- // This when calculateChange is complete. Note that the text file must be created within this directory.
- try {
- PrintWriter pw = new PrintWriter(new FileWriter(new File(System.getProperty("user.dir") + "\\src\\edu.miracosta.cs113\\change\\CoinCombinations.txt")));
- for (String s : combos) {
- pw.println(s);
- }
- pw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- } // End of class ChangeCalculator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement