-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangemakerTester.java
More file actions
344 lines (335 loc) · 13.8 KB
/
ChangemakerTester.java
File metadata and controls
344 lines (335 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//////////////// FILE HEADER (INCLUDE IN EVERY FILE) //////////////////////////
//
// Title: Tester class for the Changemaker class
// Course: CS 300 Spring 2023
//
// Author: Shourjo Aditya Chaudhuri
// Email: sachaudhuri@wisc.edu
// Lecturer: Hobbes LeGault
//
//////////////////// PAIR PROGRAMMERS COMPLETE THIS SECTION ///////////////////
//
// Partner Name: Swasti Singh
// Partner Email: sssingh4@wisc.edu
// Partner Lecturer's Name: Hobbes LeGault
//
// VERIFY THE FOLLOWING BY PLACING AN X NEXT TO EACH TRUE STATEMENT:
// _x_ Write-up states that pair programming is allowed for this assignment.
// _x_ We have both read and understand the course Pair Programming Policy.
// _x_ We have registered our team prior to the team registration deadline.
//
///////////////////////// ALWAYS CREDIT OUTSIDE HELP //////////////////////////
//
// Persons: None
// Online Sources: None
//
///////////////////////////////////////////////////////////////////////////////
import java.util.Arrays;
/**
* This class has six methods which tests whether each method present in the Changemaker class functions properly.
*/
public class ChangemakerTester {
/**
* This method checks the correctness of the base case in count method in the Changemaker class
* <p>
* Each test case checks a specific scenario:
* Test case (1) when the value passed to "count" method is negative.
* Test case (2) when there is no way to make change for the value passed to "count"
* method.
* Test case (3) when the value passed to "count" method is 0.
* For each test case, the expected result is defined and compared to the actual result
* returned by the "count" method. If the 2 values don't match then it returns false.
*
* @return true only if all scenarios pass, false otherwise
*/
public static boolean testCountBase() {
//(1) when the value is negative
{
int[] denominations = {1, 5, 10, 25};
int[] coinsRemaining = {1, 2, 3, 4};
int expected = 0;
int actual = Changemaker.count(denominations, coinsRemaining, -3);
if (expected != actual) {
return false;
}
}
//(2) when the value is positive but there is no way to make change
{
int[] denominations = {1, 5, 10, 25};
int[] coinsRemaining = {0, 0, 0, 1};
int expected = 0;
int actual = Changemaker.count(denominations, coinsRemaining, 58);
if (expected != actual) {
return false;
}
}
//(3) when the value is 0
{
int[] denominations = {1, 5, 10, 25};
int[] coinsRemaining = {0, 0, 0, 2};
int expected = 1;
int actual = Changemaker.count(denominations, coinsRemaining, 0);
if (expected != actual) {
return false;
}
}
return true;
}
/**
* This method checks how correct the recursive case in count method of the Changemaker
* class is.
* <p>
* Each test case checks a specific scenario:
* <p>
* Test case (1) when there are at least three different coins used to make change.
* Test case (2) when there are at least two different optimal ways to make change.
* Test case (3) when greedily choosing the largest coin first does not produce a
* result with the minimum number of coins.
* For each test case, the expected result is defined and compared to the actual result
* returned by the "count" method. If the two values are not equal, the method returns false
*
* @return true only if all scenarios pass, false otherwise
*/
public static boolean testCountRecursive() {
//(1) The scenario where there are at-least 3 different coins used to make change
{
int[] denomination = {1, 5, 10, 25};
int[] coinsRemaining = {1, 1, 1, 1};
int expected = 6;
int actual = Changemaker.count(denomination, coinsRemaining, 36);
if (expected != actual) {
return false;
}
}
//(2) The scenario where there are at-least 2 different optimal ways to make change
{
int[] denomination = {2, 5, 7, 10};
int[] coinsRemaining = {1, 1, 1, 1};
int expected = 4;
int actual = Changemaker.count(denomination, coinsRemaining, 12);
if (expected != actual) {
return false;
}
}
//(3) The scenario where one greedily chooses the largest coin first and does not produce a result with
// the minimum number of coins
{
{
int[] denomination = {1, 5, 6, 9};
int[] coinsRemaining = {2, 1, 1, 1};
int expected = 5;
int actual = Changemaker.count(denomination, coinsRemaining, 11);
if (expected != actual) {
return false;
}
}
}
return true;
}
/**
* This method checks the correctness of the base case in minCoins method in the Changemaker
* class
* <p>
* Each test case checks a specific scenario:
* Test case (1) when the value passed to minCoins method is negative.
* Test case (2) when there is no way to make change for the value passed to "minCoins"
* method.
* Test case (3) when the value passed to "minCoins" method is 0.
* For each test case, the expected result is defined and compared to the actual result
* returned by the "minCoins" method. If the 2 values don't match then it returns false.
*
* @return true only if all scenarios pass, false otherwise
*/
public static boolean testMinCoinsBase() {
//(1) when the value is negative
{
int[] denomination = {1, 5, 10, 25};
int[] coinsRemaining = {1, 2, 3, 4};
int expected = -1;
int actual = Changemaker.minCoins(denomination, coinsRemaining, -1);
if (expected != actual) {
return false;
}
}
//(2) when the value is positive but there is no way to make change
{
int[] denomination = {1, 5, 10, 25};
int[] coinsRemaining = {0, 0, 0, 1};
int expected = -1;
int actual = Changemaker.minCoins(denomination, coinsRemaining, 50);
if (expected != actual) {
return false;
}
}
//(3) when the value is 0
{
int[] denomination = {1, 5, 10, 25};
int[] coinsRemaining = {0, 0, 0, 2};
int expected = 0;
int actual = Changemaker.minCoins(denomination, coinsRemaining, 0);
if (expected != actual) {
return false;
}
}
return true;
}
/**
* This method checks the correctness of the recursive case in minCoins method in the Changemaker
* class.
* <p>
* Each test case checks a specific scenario:
* <p>
* Test case (1) when there are at least three different coins used to make change.
* Test case (2) when there are at least two different ways to make change using the same
* optimal number of coins.
* Test case (3) when greedily choosing the largest coin first does not produce a
* result with the minimum number of coins.
* For each test case, the expected result is defined and compared to the actual result
* returned by the "minCoins" method. If the two values are not equal, the method returns false
*
* @return true only if all scenarios pass, false otherwise
*/
public static boolean testMinCoinsRecursive() {
//(1) The scenario where there are at-least 3 different coins used to make change
{
int[] denomination = {1, 5, 10, 25};
int[] coinsRemaining = {1, 1, 1, 1};
int expected = 3;
int actual = Changemaker.minCoins(denomination, coinsRemaining, 36);
if (expected != actual) {
return false;
}
}
//(2) The scenario where there are at-least 2 different ways to make change using the same optimal number
// of coins
{
int[] denomination = {2, 5, 7, 10};
int[] coinsRemaining = {1, 1, 1, 1};
int expected = 2;
int actual = Changemaker.minCoins(denomination, coinsRemaining, 12);
if (expected != actual) {
return false;
}
}
//(3) The scenario where one greedily chooses the largest coin first and does not produce a result with
//the minimum number of coins
{
{
int[] denomination = {1, 5, 6, 9};
int[] coinsRemaining = {2, 1, 1, 1};
int expected = 2;
int actual = Changemaker.minCoins(denomination, coinsRemaining, 11);
if (expected != actual) {
return false;
}
}
}
return true;
}
/**
* This method checks the correctness of the base case in makeChange method in the Changemaker
* class
* <p>
* Each test case checks a specific scenario:
* Test case (1) when the value passed to makeChange method is negative.
* Test case (2) nwhen there is no way to make change for the value passed to "makeChange"
* method.
* Test case (3) when the value passed to "makeChange" method is 0.
* For each test case, the expected result is defined and compared to the actual result
* returned by the "makeChange" method. If the 2 values don't match then it returns false.
*
* @return true only if all scenarios pass, false otherwise
*/
public static boolean testMakeChangeBase() {
//(1) when the value is negative
{
int[] denomination = {1, 5, 10, 25};
int[] coinsRemaining = {1, 2, 3, 4};
int[] expected = null;
int[] actual = Changemaker.makeChange(denomination, coinsRemaining, -1);
if (!Arrays.equals(actual, expected)) {
return false;
}
}
//(2) when the value is positive but there is no way to make change
{
int[] denomination = {1, 5, 10, 25};
int[] coinsRemaining = {0, 0, 0, 1};
int[] expected = null;
int[] actual = Changemaker.makeChange(denomination, coinsRemaining, 50);
if (!Arrays.equals(actual, expected)) {
return false;
}
}
//(3) when the value is 0
{
int[] denomination = {1, 5, 10, 25};
int[] coinsRemaining = {0, 0, 0, 2};
int[] expected = {0, 0, 0, 0};
int[] actual = Changemaker.makeChange(denomination, coinsRemaining, 0);
if (!Arrays.equals(actual, expected)) {
return false;
}
return true;
}
}
/**
* This method checks the correctness of the recursive case in makeChange method in
* the Changemaker class.
* Each test case checks a specific scenario:
* <p>
* Test case (1) when there are at least three different coins used to make change.
* Test case (2) when there are at least two different ways to make change using the same
* optimal number of coins.
* Test case (3) when always choosing the largest coin first does not produce a result
* with the minimum number of coins used.
* For each test case, the expected array is compared to the actual array
* returned by the "makeChange" method. If the two arrays are not equal, the method returns false
*
* @return true only if all scenarios pass, false otherwise
*/
public static boolean testMakeChangeRecursive() {
//(1) The Scenario where there are at-least 3 different coins used to make change
{
int[] denomination = {1, 5, 10, 25};
int[] coinsRemaining = {1, 1, 1, 1};
int[] expected = {1, 1, 1, 0};
int[] actual = Changemaker.makeChange(denomination, coinsRemaining, 16);
if (!Arrays.equals(actual, expected)) {
return false;
}
}
//(2) The scenario where there are at-least 2 different ways to make change using the
//same optimal number of coins
{
int[] denomination = {2, 5, 7, 10};
int[] coinsRemaining = {1, 1, 1, 1};
int[] expected = {0, 1, 1, 0};
int[] expected2 = {1, 0, 0, 1};
int[] actual = Changemaker.makeChange(denomination, coinsRemaining, 12);
if (!(Arrays.equals(actual, expected) || Arrays.equals(actual, expected2))) {
return false;
}
}
//(3) The scenario in which always choosing the largest coin first does not produce a result
// with the minimum number of coins used.
{
int[] denomination = {1, 5, 6, 9};
int[] coinsRemaining = {2, 1, 1, 1};
int[] expected = {0, 1, 1, 0};
int[] actual = Changemaker.makeChange(denomination, coinsRemaining, 11);
if (!Arrays.equals(actual, expected)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println("Count Base: " + (testCountBase() ? "pass" : "X"));
System.out.println("Count Recursive: " + (testCountRecursive() ? "pass" : "X"));
System.out.println("Minimum Coins Base: " + (testMinCoinsBase() ? "pass" : "X"));
System.out.println("Minimum Coins Recursive: " + (testMinCoinsRecursive() ? "pass" : "X"));
System.out.println("Make Change Base: " + (testMakeChangeBase() ? "pass" : "X"));
System.out.println("Make Change Recursive: " + (testMakeChangeRecursive() ? "pass" : "X"));
}
}