-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathMain.java
381 lines (317 loc) · 14.3 KB
/
Main.java
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**********************************************************
*
* Homework #4 (Programming Assignment). This assignment has two parts:
* (1) The first being that you must complete the implementation of select
* methods within the Hash Map (Hash Table) object provided in file
* myHashMap.java. This object is emulating the Hashmap object within
* Java's Collection Framework Library.
* (2) Second is solving simple collection problems contained within
* the file HashProblems.java, which are using the HashMap object contained
* within the Java Collection Framework library. Note that HashMap's internal
* implementation is using ArrayList (for buckets) and LinkedList (for the
* collision chain), which is the same approach being used by our implementation
* referred to above.
*
* This main file is a driver for testing these methods that you write within
* the files 'myHashMap.java' and 'HashProblems.java'. Your work will need to
* pass all the tests for 100%
*
* *** DO NOT MANIPULATE / CHANGE THIS FILE ***
*
*********************************************************/
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
myHashMap<String, Integer> hashmap = new myHashMap<>();
boolean removeErrorFlag = false;
boolean replaceErrorFlag = false;
boolean getAverageErrorFlag = false;
boolean oddErrorFlag = false;
boolean twoSumsErrorFlag = false;
Integer oldValue;
int assignmentScore = 0;
/*
* Populate sample data into the hash map
*/
hashmap.put("Key_1", 1);
hashmap.put("Key_2", 2);
hashmap.put("Key_3", 3);
hashmap.put("Key_4", 4);
hashmap.put("Key_5", 5);
hashmap.put("Key_6", 6);
hashmap.put("Key_7", 7);
hashmap.put("Key_8", 8);
hashmap.put("Key_9", 9);
hashmap.put("Key_10", 10);
hashmap.put("Key_11", 11);
hashmap.put("Key_12", 12);
hashmap.put("Key_13", 13);
hashmap.put("Key_14", 14);
hashmap.put("Key_15", 15);
hashmap.put("Key_16", 16);
hashmap.put("Key_17", 17);
hashmap.put("Key_18", 18);
hashmap.put("Key_19", 19);
hashmap.put("Key_20", 20);
hashmap.put("Key_21", 21);
hashmap.put("Key_22", 22);
hashmap.put("Key_23", 23);
hashmap.put("Key_24", 24);
hashmap.put("Key_25", 25);
hashmap.put("Key_26", 26);
hashmap.put("Key_27", 27);
hashmap.put("Key_28", 28);
hashmap.put("Key_29", 29);
hashmap.put("Key_30", 30);
hashmap.put("Key_31", 31);
hashmap.put("Key_32", 32);
hashmap.put("Key_33", 33);
hashmap.put("Key_34", 34);
hashmap.put("Key_35", 35);
hashmap.put("Key_36", 36);
hashmap.put("Key_37", 37);
hashmap.put("Key_38", 38);
hashmap.put("Key_39", 39);
hashmap.put("Key_40", 40);
hashmap.put("Key_41", 41);
hashmap.put("Key_42", 42);
hashmap.put("Key_43", 43);
hashmap.put("Key_44", 44);
hashmap.put("Key_45", 45);
hashmap.put("Key_46", 46);
hashmap.put("Key_47", 47);
hashmap.put("Key_48", 48);
hashmap.put("Key_49", 49);
hashmap.put("Key_50", 50);
/*******************************************************************
*
* Tests for the myHashMap remove(K) method
*
******************************************************************/
// remove() should return null, as key does not exist in hashmap
if ( hashmap.remove("NOT FOUND") != null ) {
System.out.println("Error 1: remove failed");
removeErrorFlag = true;
}
// remove() returns the node object deleted, key should exist and been deleted
if ( ! removeErrorFlag && hashmap.remove("Key_5") == null ) {
System.out.println("Error 2: remove failed");
removeErrorFlag = true;
}
// The K/V pair <Key_4,4> should be in hashmap, verify the value can be found
if ( ! removeErrorFlag && ! hashmap.containsValue(4)) {
System.out.println("Error 3: containsValues failed");
removeErrorFlag = true;
}
// The K/V pair <Key_4,4> should be in hashmap, verify the key can be found
if ( ! removeErrorFlag && ! hashmap.containsKey("Key_4")) {
System.out.println("Error 4: containsKey failed");
removeErrorFlag = true;
}
// The K/V pair <Key_5,5> has already been deleted, verify the value is not found
if ( ! removeErrorFlag && hashmap.containsValue(5)) {
System.out.println("Error 5: containsValues failed");
removeErrorFlag = true;
}
// The K/V pair <Key_5,5> has already deleted, verify key is not found
if ( ! removeErrorFlag && hashmap.containsKey("Key_5")) {
System.out.println("Error 6: containsKey failed");
removeErrorFlag = true;
}
// There should be 49 elements contained within the hashmap, verify.
if ( removeErrorFlag && hashmap.Size() != 49 ) {
System.out.println("Error 7: Size failed");
removeErrorFlag = true;
}
/*******************************************************************
*
* Tests for the myHashMap replace(K,V) method
*
******************************************************************/
oldValue = hashmap.replace("Key_8", 16);
// verify replace returned the previous value, which should be 8
if ( oldValue != 8 ) {
System.out.println("Error 8: replace failed");
replaceErrorFlag = true;
}
// The value 8 should no longer exit in the hashmap
if ( ! replaceErrorFlag && hashmap.containsValue(8)) {
System.out.println("Error 9: containsValues failed");
replaceErrorFlag = true;
}
// The value 8 was replaced with 16, this new value should be found
if ( ! replaceErrorFlag && ! hashmap.containsValue(16)) {
System.out.println("Error 10: containsValues failed");
replaceErrorFlag = true;
}
// 'Key_8' should still be found in the hashmap
if ( ! replaceErrorFlag && ! hashmap.containsKey("Key_8")) {
System.out.println("Error 11: containsKey failed");
replaceErrorFlag = true;
}
/*******************************************************************
*
* Tests for the myHashMap replace(K,V, V) method
*
******************************************************************/
// false should be returned, as Key_9's value is not 0
if ( ! replaceErrorFlag && hashmap.replace("Key_9", 0, 100 ) ) {
System.out.println("Error 12: replace failed");
replaceErrorFlag = true;
}
// true should be returned, as Key_9's value is 9
if ( ! replaceErrorFlag && ! hashmap.replace("Key_9", 9, 100 ) ) {
System.out.println("Error 13: replace failed");
replaceErrorFlag = true;
}
// There is no key String_1, so false should be returned
if ( ! replaceErrorFlag && hashmap.replace("String_1", 9, 100 ) ) {
System.out.println("Error 14: replace failed");
replaceErrorFlag = true;
}
// The value 9 was replaced with 100 above, this new value should be found
if ( ! replaceErrorFlag && ! hashmap.containsValue(100)) {
System.out.println("Error 15: containsValues failed");
replaceErrorFlag = true;
}
// 'Key_9' should be found in the hashmap
if ( ! replaceErrorFlag && ! hashmap.containsKey("Key_9")) {
System.out.println("Error 16: containsKey failed");
replaceErrorFlag = true;
}
/*
* You can temporarily un-comment either the subsequent and/or both statements iff
* you want to visually see what is in the HashMap for debugging purposes. Do recall,
* that unlike trees, hashmaps retain no order, so they will not be displayed in the
* insertion order. But, recommend instead use your IDE debugger to debug instead.
*/
//System.out.println("HashMap Ks only : " + hashmap.keySet());
//System.out.println("\nHashMap K/Vs : " + hashmap.entrySet() + "\n");
/*
*
* Testing now the methods in the object HashingProblems
*
*/
/*******************************************************************
*
* Tests HashingProblems for getAverage
*
******************************************************************/
HashingProblems hp = new HashingProblems();
HashMap<Integer, Integer> hash_map = new HashMap<>();
hash_map.put(1, 10);
hash_map.put(2, 20);
hash_map.put(5, 50);
hash_map.put(12, 100);
hash_map.put(3, 3);
int[] arr1 = {100, 1, 2, 9, 7, 8, 15};
int[] arr2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
// based on the above values, the average should be 15.0
if ( hp.getAverage(hash_map, arr1) != (double) 15.0 ) {
System.out.println("Error 17: getAverage failed");
getAverageErrorFlag = true;
}
// based on the above values, the average should be 36.6
if ( ! getAverageErrorFlag && hp.getAverage(hash_map, arr2) != (double) 36.6 ) {
System.out.println("Error 18: getAverage failed");
getAverageErrorFlag = true;
}
/*******************************************************************
*
* Tests HashingProblems for odd
*
******************************************************************/
ArrayList<String> ArrayList1 = new ArrayList<String>( Arrays.asList("Diana", "Adam", "Kavitha", "Mushjtaba", "Peter"));
ArrayList<String> ArrayList2 = new ArrayList<String>();
HashMap<Integer, String> hash_map2 = new HashMap<>();
hash_map2.put(1, "Diana");
hash_map2.put(2, "Naomi");
hash_map2.put(3, "Adam");
hash_map2.put(4, "Eric");
hash_map2.put(5, "Kavitha");
hash_map2.put(6, "Yu");
hash_map2.put(7, "Mushjtaba");
hash_map2.put(8, "Marisa");
hash_map2.put(9, "Peter");
hash_map2.put(10, "Slanovich");
ArrayList2 = hp.odd(hash_map2);
/*
* You can temporarily un-comment both of the following statements if you need
* to visually debug the method odd(); but recommend instead using your IDE
* debugger to debug instead.
*/
//System.out.println("Result should be the following: <" + ArrayList1 + ">");
//System.out.println("Result is as follows : <" + ArrayList2 + ">");
if (ArrayList1.equals(ArrayList2) == false ) {
System.out.println("Error 19: odd failed");
oddErrorFlag = true;
}
/*******************************************************************
*
* Tests HashingProblems for twoSums
*
******************************************************************/
int [] numberList = new int[] {1,4,5,7,8,9};
int k1 = 4;
int k2 = 5;
// When k=4, answer should be 3
if ( hp.twoSums(numberList, k1) != 3 ) {
System.out.println("Error 20: twoSums failed");
twoSumsErrorFlag = true;
}
// When k=5, answer should be 1
if ( ! twoSumsErrorFlag && hp.twoSums(numberList, k2) != 1 ) {
System.out.println("Error 21: twoSums failed");
twoSumsErrorFlag = true;
}
/*
* You can temporarily un-comment both of the following statements if you need
* to visually debug the method odd(); but recommend instead using your IDE
* debugger to debug instead.
*/
//System.out.println("TwoSums where k=" + k1 + ", result was: " + hp.twoSums(numberList, k1));
//System.out.println("TwoSums where k=" + k2 + ", result was: " + hp.twoSums(numberList, k2));
/*******************************************************************
*
* Calculate the final score
*
********************************************8*********************/
if ( ! removeErrorFlag ) {
assignmentScore += 25;
System.out.println("Automated HashMap remove testing terminated -- tests PASSED");
} else {
System.out.println("Automated Hashmap remove testing terminated -- tests FAILED ***");
}
// Adjust assignment score for TreeMap removeEven testing
if ( ! replaceErrorFlag ) {
assignmentScore += 25;
System.out.println("Automated Hashmap replace testing terminated -- tests PASSED");
} else {
System.out.println("Automated Hashmap replace testing terminated -- tests FAILED ***");
}
// Adjust assignment score for HashingProblems getAverage testing
if ( ! getAverageErrorFlag ) {
assignmentScore += 15;
System.out.println("Automated HashingProblems, getAvg terminated -- tests PASSED");
} else {
System.out.println("Automated HashingProblems, getAvg terminated -- tests FAILED ***");
}
// Adjust assignment score for HashingProblems odd testing
if ( ! oddErrorFlag ) {
assignmentScore += 10;
System.out.println("Automated HashingProblems, odd terminated -- tests PASSED");
} else {
System.out.println("Automated HashingProblems, odd terminated -- tests FAILED ***");
}
// Adjust assignment score for HashingProblems odd testing
if ( ! twoSumsErrorFlag ) {
assignmentScore += 25;
System.out.println("Automated HashingProblems, twoSum terminated -- tests PASSED");
} else {
System.out.println("Automated HashingProblems, twoSum terminated -- tests FAILED ***");
}
System.out.println("\nAssignment score is: " + assignmentScore);
}
}