Starting from:
$35

$29

PA5: Coin Changer Solution

Goal:
Use i) the state pattern; AND ii) the chain of responsibility pattern to implement a solution for the Make Change problem.

Make Change Problem:
Determine the minimal size set of coins needed to represent a given quantity of money and a given set of coin denominations. For example, if the amount is 58, then the change based on the following denomination table will be Quarters: 2, Dimes: 0, Nickels: 1, Pennies: 3.

For this assignment the following denominations will be used.

Denomination
Value
Quarter
25
Dime
10
Nickel
5
Penny
1
Requirements for application of the State Pattern:
For each denomination, write a class with the denomination name that implements the IDenomination interface.
Write a class named "Calculation_Change" that implements the ICalculation_Change interface.
No "if" statements are allowed. Loop statements (while, for, do-while) are permitted.
The program main line should be the test class given below.
Interfaces:
public interface IDenomination {

/**

* Getter method for the initial amount.

* @return initial amount from which the number of coins is to be calculated.

*/

int get_initial_amount();

 

/**

* Setter method for the initial amount.

* @param amount Initial amount of money from which the number of coins is

* to be calculated.

*/

void set_initial_amount(int amount);

 

 

/**

* Calculates the number of coins of this denomination are needed.

* @return The next denomination to be considered.

*/

IDenomination Calculate_num_changes();

 

/**

* Prints the name of the denomination and number of coins needed for it.

* Format: "name: number", where "name" is the name of the denomination

* and "number" is the number of coins.

* @return The next denomination to be printed.

*/

IDenomination print_num_changes();

}




public interface ICalculation_Change {

/**

* Sets the first denomination to be considered.

* @param d Instance of first denomination to be considered.

*/

void set_init_denomination(IDenomination d);

 

/**

* Gets the first denomination to be considered.

* @return Instance of first denomination to be considered.

*/

IDenomination get_init_denomination();

 

/**

* Using the state pattern, calculates the number of coins needed given

* amount of money.

* @param amount Initial amount of money.

*/

void calculate_all_changes(int amount);

 

/**

* Using the state pattern, prints the number of coins for each denomination.

* This method should only be called after the calculation has been performed

* by the calculate_all_changes method

*/

void print_all_changes();

}




Test Class:
public class Test {

public static void main(String[] args) {

ICalculation_Change chg = new Calculation_Change();

// Test 1: 74 cents

IDenomination strt = new Quarter();

chg.set_init_denomination(strt);

chg.calculate_all_changes(74);

chg.print_all_changes();

// Test 2: 107 cents

strt = new Quarter();

chg.set_init_denomination(strt);

chg.calculate_all_changes(107);

chg.print_all_changes();

}

/* Expected output:

Quarters: 2, Dimes: 2, Nickels: 0, Pennies: 4

Quarters: 4, Dimes: 0, Nickels: 1, Pennies: 2

*/

}




Requirements for application of the Chain of Responsibility Pattern:
You need to design your implementation using a UML class diagram. Specifically, you first define a set of interfaces where the required methods should be defined. Association must be provided and so the corresponding methods should be skipped. Next, your UML class diagram should include the corresponding classes implementing the interface you define as well as the main class to test it. Remember, your class diagram should completely match your implementation. The UML class diagram should be edited in a word document. The implementation should be in a different package from the program using the State pattern.





Requirements for PA5,

1. You must use the Eclipse IDE to create a Java project.

2. You must follow the instruction to submit your Java project (http://www.cs.wmich.edu/~wwshen/cs1120_Fall2012/documents/HowtoSubmitPAinEclipse.pdf).

3. Don’t forget to submit your class diagram using a word document.

More products