blob: db1ae996910c957b7b4d3c97bae548b01d62baa9 (
plain)
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
|
import java.util.Random;
import java.util.Scanner;
public class FrequencyBagTester
{
public static void main(String[] args)
{
int numberOfData = 20;
int maxValueEx = 5;
boolean wrongFrequency = false;
int failed = 0;
Scanner in = new Scanner(System.in);
//CHANGE THIS BACK----------------------------------CHANGE THIS BACK-------
int option = 1;
//CHANGE THIS BACK----------------------------------CHANGE THIS BACK-------
while(option == 0)
{
System.out.println("Please select a test option");
System.out.println(" 1. Stop the test as soon as an error is detected");
System.out.println(" 2. Do not stop the test when an error is detected");
System.out.print("Enter an option (1 or 2): ");
option = in.nextInt();
if(option != 1 && option != 2)
{
System.out.println(option + " is not a valid choice.\n");
option = 0;
}
}
in.close();
FrequencyBag<Integer> fb = new FrequencyBag<Integer>();
// getFrequencyOf() empty bag
System.out.print("Checking the method getFrequencyOf() of an empty frequency bag: ");
for(int i = 0; i < maxValueEx; i++)
{
if(fb.getFrequencyOf(i) != 0)
{
if(!wrongFrequency)
{
System.out.println("FAIL");
failed++;
}
System.out.println("The method getFrequencyOf(" + i + ") of an empty bag should return 0.");
System.out.println("But your method getFrequencyOf(" + i + ") returns " + fb.getFrequencyOf(i) + ".");
wrongFrequency = true;
}
}
if(!wrongFrequency)
{
System.out.println("PASS");
}
else if(option == 1)
{
return;
}
// size() of empty bag.
System.out.print("Checking the method size() of an empty bag: ");
if(fb.size() != 0)
{
failed++;
System.out.println("FAIL");
System.out.println("The size of an empty bag should be 0.");
System.out.println("But your method size() returns " + fb.size() + ".\n");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS");
}
// getMaxFrequency() of empty bag
System.out.print("Checking the method getMaxFrequency() of an empty frequency bag: ");
if(fb.getMaxFrequency() != 0)
{
failed++;
System.out.println("FAIL");
System.out.println("The method getMaxFrequency() of an empty bag should return 0.");
System.out.println("But your method getMaxFrequency() returns " + fb.getMaxFrequency() + ".\n");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS\n");
}
// Adding data into empty bag
int dataSet[] = new int[5];
for(int i = 0; i < maxValueEx; i++)
{
dataSet[i] = 0;
}
Random rand = new Random();
System.out.println("Add the following data into your frequency bag (in that order):");
for(int i = 0; i < numberOfData; i++)
{
int temp = rand.nextInt(maxValueEx);
System.out.print(temp + " ");
fb.add(temp);
dataSet[temp]++;
}
System.out.println();
// getFrequencyOf()
System.out.println("Checking the method getFrequencyOf() of each data in this frequency bag: ");
for(int i = 0; i < maxValueEx; i++)
{
System.out.print("Frequency of " + i + ": ");
if(fb.getFrequencyOf(i) != dataSet[i])
{
failed++;
System.out.println("FAIL");
System.out.println("The frequency of " + i + " is " + dataSet[i] + ".");
System.out.println("But the method getFrequencyOf(" + i + ") returns " + fb.getFrequencyOf(i) + ".\n");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS");
}
}
// getFrequencyOf()
System.out.print("Checking the method getFrequencyOf() of a data that is not in this frequency bag: ");
if(fb.getFrequencyOf(-1) != 0)
{
failed++;
System.out.println("FAIL");
System.out.println("The frequency of -1 is 0.");
System.out.println("But the method getFrequencyOf(-1) returns " + fb.getFrequencyOf(-1) + ".\n");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS");
}
// getProbabilityOf()
System.out.println("Checking the method getProbabilityOf() of each data in this frequency bag: ");
for(int i = 0; i < maxValueEx; i++)
{
System.out.print("Probability of " + i + ": ");
if(Math.abs(fb.getProbabilityOf(i) - ((double) dataSet[i]/numberOfData)) > 0.000001)
{
failed++;
System.out.println("FAIL");
System.out.println("The probability of " + i + " is " + ((double) dataSet[i]/numberOfData) + ".");
System.out.println("But the method getProbabilityOf(" + i + ") returns " + fb.getProbabilityOf(i) + ".\n");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS");
}
}
// getProbabilityOf()
System.out.print("Checking the method getProbabilityOf() of a data that is not in this frequency bag: ");
if(fb.getProbabilityOf(-1) != 0.0)
{
failed++;
System.out.println("FAIL");
System.out.println("The probability of -1 is 0.");
System.out.println("But the method getProbabilityOf(-1) returns " + fb.getProbabilityOf(-1) + ".\n");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS");
}
// size()
System.out.print("Checking the method size() of this non-empty frequency bag: ");
if(fb.size() != numberOfData)
{
failed++;
System.out.println("FAIL");
System.out.println("After adding " + numberOfData + " entries, the method size() should return " + numberOfData + ".");
System.out.println("But your method size() returns " + fb.size() + ".\n");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS");
}
// getMaxFrequency()
System.out.print("Checking the method getMaxFrequency() of this non-empty frequency bag: ");
int tempMax = 0;
for(int i = 0; i < maxValueEx; i++)
{
if(tempMax < dataSet[i])
{
tempMax = dataSet[i];
}
}
if(fb.getMaxFrequency() != tempMax)
{
failed++;
System.out.println("FAIL");
System.out.println("The method getMaxFrequency() of this non-empty frequency bag should return " + tempMax + ".");
System.out.println("But your method getMaxFrequency() of this non-empty frequency bag returns " + fb.getMaxFrequency() + ".\n");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS\n");
}
// clear();
fb.clear();
System.out.println("Checking the method clear()");
System.out.print("Checking the method size() after clear(): ");
if(fb.size() != 0)
{
failed++;
System.out.println("FAIL");
System.out.println("After the bag is cleared, the method size() should return 0.");
System.out.println("But your method size() returns " + fb.size() + ".\n");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS");
}
// getFrequencyOf() after clear()
System.out.print("Checking the method getFrequencyOf(3) after clear(): ");
if(fb.getFrequencyOf(3) != 0)
{
failed++;
System.out.println("FAIL");
System.out.println("After the bag is cleared, the method getFrequencyOf(3) should return 0.");
System.out.println("But your method getFrequencyOf(3) returns " + fb.getFrequencyOf(3) + ".\n");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS");
}
// getMaxFrequency() after clear()
System.out.print("Checking the method getMaxFrequency() after clear(): ");
if(fb.getMaxFrequency() != 0)
{
failed++;
System.out.println("FAIL");
System.out.println("After the bag is cleared, the method getMaxFrequency() should return 0.");
System.out.println("But your method getMaxFrequency() returns " + fb.getMaxFrequency() + ".\n");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS");
}
// Generate large bag
numberOfData = 1000000;
maxValueEx = 1000;
dataSet = new int[maxValueEx];
for(int i = 0; i < maxValueEx; i++)
{
dataSet[i] = 0;
}
System.out.println("\nSo far so good.");
System.out.println("Let's try adding " + numberOfData + " data into your frequency bag.");
for(int i = 0; i < numberOfData; i++)
{
int temp = rand.nextInt(maxValueEx);
fb.add(temp);
dataSet[temp]++;
}
// size() of large bag.
System.out.print("Checking the method size() of this non-empty frequency bag: ");
if(fb.size() != numberOfData)
{
failed++;
System.out.println("FAIL");
System.out.println("After adding " + numberOfData + " entries, the method size() should return " + numberOfData + ".");
System.out.println("But your method size() returns " + fb.size() + ".\n");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS");
}
// getFrequencyOf() of large bag
System.out.print("Checking the method getFrequencyOf() of each data in this frequency bag: ");
wrongFrequency = false;
for(int i = 0; i < maxValueEx; i++)
{
if(fb.getFrequencyOf(i) != dataSet[i])
{
wrongFrequency = true;
}
}
if(wrongFrequency)
{
failed++;
System.out.println("FAIL");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS");
}
// getProbabilityOf() of large bag
System.out.print("Checking the method getProbabilityOf() of each data in this frequency bag: ");
boolean wrongProbability = false;
for(int i = 0; i < maxValueEx; i++)
{
if(Math.abs(fb.getProbabilityOf(i) - ((double) dataSet[i]/numberOfData)) > 0.000001)
{
wrongProbability = true;
}
}
if(wrongProbability)
{
failed++;
System.out.println("FAIL");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS");
}
// getMaxFrequency() of large bag
System.out.print("Checking the method getMaxFrequency() of this non-empty frequency bag: ");
tempMax = 0;
for(int i = 0; i < maxValueEx; i++)
{
if(tempMax < dataSet[i])
{
tempMax = dataSet[i];
}
}
if(fb.getMaxFrequency() != tempMax)
{
failed++;
System.out.println("FAIL");
System.out.println("The method getMaxFrequency() of this non-empty frequency bag should return " + tempMax + ".");
System.out.println("But your method getMaxFrequency() of this non-empty frequency bag returns " + fb.getMaxFrequency() + ".\n");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS");
}
// Frequency Bag of String
System.out.println("\nSo far so good.");
System.out.println("Let's construct a frequency bag of String.");
FrequencyBag<String> sfb = new FrequencyBag<String>();
String s = "hello how are you i am find thank you and you i am fine thank you";
String[] str = s.split(" ");
for(int i = 0; i < str.length; i++)
{
sfb.add(str[i]);
}
System.out.println("Adding the following strings into an empty frequency bag:");
System.out.println(s + "\n");
System.out.print("Checking the method size() of this non-empty frequency bag: ");
if(sfb.size() != 16)
{
failed++;
System.out.println("FAIL");
System.out.println("After adding 16 entries, the method size() should return 16.");
System.out.println("But your method size() returns " + sfb.size() + ".\n");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS");
}
System.out.print("Checking the method getFrequencyOf(\"am\"): ");
if(sfb.getFrequencyOf("am") != 2)
{
failed++;
System.out.println("FAIL");
System.out.println("There are two strings \"am\" added into this bag.");
System.out.println("But your method getFrequencyOf(\"am\") returns " + sfb.getFrequencyOf("am") + ".\n");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS");
}
System.out.print("Checking the method getMaxFrequency(): ");
if(sfb.getMaxFrequency() != 4)
{
failed++;
System.out.println("FAIL");
System.out.println("The maximum frequency should be 4 (the string \"you\").");
System.out.println("But your method getMaxFrequency() returns " + sfb.getMaxFrequency() + ".\n");
if(option == 1)
{
return;
}
}
else
{
System.out.println("PASS");
}
System.out.println();
if(failed != 0)
{
System.out.println("Threre are " + failed + " in this test. Fix your program.");
}
else
{
System.out.println("Congratulation!!! Your FrequencyBag works perfectly (I hope).");
System.out.println("Run the program FrequencyFrame and compare its result.");
}
}
}
|