-
Notifications
You must be signed in to change notification settings - Fork 50
/
Main.java
312 lines (248 loc) · 11.2 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
/**********************************************************
*
* Homework # 6 (Programming Assignment). This assignment has several parts.
*
* The first part requires the implementation of the methods add() and contains()
* within the provided PriorityObject class. This first portion requires a basic
* understanding on an implementation of a min-heap to code these two methods.
*
* The second part requires is the Priority Game problem. This part provides
* more practice with teh Java Collection Framework library's PriorityQueue Class.
*
* The third and forth parts are sorting problems. The first requires solving
* removing duplicates from an ArrayList. The second sorting problem requires
* solving various combinations of numbers that add up to some sum.
*
* *** DO NOT MANIPULATE / CHANGE THIS FILE ***
*
*********************************************************/
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int score = 0;
boolean PriorityQueueFail = false;
boolean PQGameFail = false;
boolean Sorting1Fail = false;
boolean Sorting2Fail = false;
/*************************************************************
*
* Test cases for the PriorityQueue Object.
*
* Verify methods add() and contains() work correctly.
*
*************************************************************/
PriorityQueue<String, Integer> pq = new PriorityQueue<>();
PriorityQueue<String, Integer>.Node element;
/*
* Perform an initial queue population, utilizing both add() and offer().
*/
pq.add("element2", 20);
pq.add("element1", 10);
pq.add("element3", 30);
pq.offer("element4", 30);
pq.offer("element5", 5);
/*
* Perform basic tests for peek() and contains().
*/
if ( !pq.isEmpty() && pq.peek().value() != "element5" && pq.peek().priority() != 5 ) {
PriorityQueueFail = true;
System.out.println("Priority Queue Test 1 Failure, value: "
+ pq.peek().value() + " priority: " + pq.peek().priority());
}
if ( ! PriorityQueueFail && !pq.contains("element3")) {
PriorityQueueFail = true;
System.out.println("Priority Queue Test 2 Failure");
}
if ( ! PriorityQueueFail && pq.contains("NOT IN QUEUE") ) {
PriorityQueueFail = true;
System.out.println("Priority Queue Test 3 Failure");
}
/*
* Add a new element. Once added, change it's priority after adding.
* After changing priority, it should be repositioned in the min heap.
*
* Only perform the change priority operation if no prior errors.
*/
element = pq.add("element6", 45); // Add as low priority
if ( !pq.isEmpty() && ! PriorityQueueFail ) {
element.changePriority(2); // Change to highest priority
} else {
PriorityQueueFail = true;
}
if ( ! PriorityQueueFail && pq.peek().value() != "element6" && pq.peek().priority() != 2) {
PriorityQueueFail = true;
System.out.println("Priority Queue Test 4 Failure");
}
/*
* Now change the element's, "element6", priority, which we have a handle to in the
* queue, from the highest priority to a lower priority. It should be repositioned in
* the min heap as a result.
*
* Only perform operation if no prior erros.
*/
if ( ! PriorityQueueFail ) {
element.changePriority(35); // Re-update priority, change lower
}
/*
* We now have 6 elements in the queue/ Based on each elements priority,
* they should dequeue in this order, 5, 1, 2, 3, 4, and 6. Any other
* order is not correct. The following will verify this order.
*/
String[] verifyOrder1 = { "element5", "element1", "element2", "element3",
"element4", "element6" };
for (int i=0; !PriorityQueueFail && !pq.isEmpty() ; i++ ) {
element = pq.remove();
// There are only 6 elements in the queue, if more are returned, an error
if ( i >= verifyOrder1.length ) {
PriorityQueueFail = true;
System.out.println("Priority Queue Test 5 Failure - too many elements");
break;
}
if ( element.value != verifyOrder1[i] ) {
PriorityQueueFail = true;
System.out.println("Priority Queue Test 6 Failure, queue returned: "
+ element.value() + " should be: " + verifyOrder1[i]);
break;
}
}
/*
* Queue is empty, now add and remove more elements. After the following
* operations, the queue should contain [ (ghi,1), (abc,5), ], in that priority
* order. We will verify this after the enqueue and dequeue operations.
*/
String[] verifyOrder2 = { "ghi", "abc" };
pq.add("abc", 5);
pq.add("def", 2);
pq.poll();
pq.offer("ghi", 1);
for (int i=0; !PriorityQueueFail && !pq.isEmpty() ; i++ ) {
element = pq.remove();
// There are only 2 elements in the queue, if more are returned, an error
if ( i >= verifyOrder2.length ) {
PriorityQueueFail = true;
System.out.println("Priority Queue Test 7 Failure - too many elements");
break;
}
if ( element.value != verifyOrder2[i] ) {
PriorityQueueFail = true;
System.out.println("Priority Queue Test 8 Failure, queue returned: "
+ element.value() + " should be: " + verifyOrder2[i]);
break;
}
}
/*************************************************************
*
* Test cases for the PQ Game.
*
*************************************************************/
ProblemSolutions ps = new ProblemSolutions();
int list1[] = {2,7,4,1,8,1}; // Expected answer is '1'
int list2[] = {2,7,4,1,8,1,15,0,19,-2,-100,100,8,2,7}; // Expected answer is '128'
int list3[] = {7,4,7,4,7,2,6,8,6,7}; // Expected answer is '0'
int list4[] = {14,7,24,1,8,1}; // Expected answer is '3'
int list5[] = {14,7,-24,-1,8,-1}; // Expected answer is '27'
if ( ps.lastBoulder(list1) != 1 ) {
PQGameFail = true;
System.out.println("PQ Game Test 1 Failure, returned was " + ps.lastBoulder(list1));
}
if ( ! PQGameFail && ps.lastBoulder(list2) != 128 ) {
PQGameFail = true;
System.out.println("PQ Game Test 2 Failure, returned was " + ps.lastBoulder(list2));
}
if ( ! PQGameFail && ps.lastBoulder(list3) != 0 ) {
PQGameFail = true;
System.out.println("PQ Game Test 3 Failure, returned was " + ps.lastBoulder(list3));
}
if ( ! PQGameFail && ps.lastBoulder(list4) != 3 ) {
PQGameFail = true;
System.out.println("PQ Game Test 4 Failure, returned was " + ps.lastBoulder(list4));
}
if ( ! PQGameFail && ps.lastBoulder(list5) != 27 ) {
PQGameFail = true;
System.out.println("PQ Game Test 5 Failure, returned was " + ps.lastBoulder(list5));
}
/*************************************************************
*
* Test cases for the Sorting 1 and 2 problems
*
*************************************************************/
/*
* Testing for sorting problem 1
*/
ArrayList<String> sort1InputList1 = new ArrayList<>(Arrays.asList("apple", "apple",
"banana", "banana", "banana",
"cherry", "cherry", "cherry",
"cherry"));
ArrayList<String> sort1AnswerList1 = new ArrayList<>(Arrays.asList("apple",
"banana", "cherry"));
if ( ! ps.showDuplicates(sort1InputList1).equals(sort1AnswerList1) ) {
Sorting1Fail = true;
System.out.println("Sorting 1 Test 1 Failure, returned String: "
+ ps.showDuplicates(sort1InputList1));
}
ArrayList<String> sort1InputList2 = new ArrayList<>(Arrays.asList("close", "part",
"PART", "learn", "close",
"start", "learn", "start",
"concur"));
ArrayList<String> sort1AnswerList2 = new ArrayList<>(Arrays.asList("close",
"learn", "start"));
if ( !Sorting1Fail && ! ps.showDuplicates(sort1InputList2).equals(sort1AnswerList2) ) {
Sorting1Fail = true;
System.out.println("Sorting 1 Test 2 Failure, returned String: "
+ ps.showDuplicates(sort1InputList2));
}
/*
* Testing for sorting problem 2
*/
int[] sorting2Input1 = new int[]{2, 3, 3, 4, 5, 6, 7};
int k = 9;
ArrayList<String> sort2AnswerList1 = new ArrayList<>(Arrays.asList("(2, 7)", "(3, 6)",
"(4, 5)"));
// With k==9, expected: [(2, 7), (3, 6), (4, 5)]
if ( ! ps.pair(sorting2Input1, k).equals(sort2AnswerList1) ) {
Sorting2Fail = true;
System.out.println("Sorting 2 Test 1 Failure, returned String: "
+ ps.pair(sorting2Input1, k));
}
int[] sorting2Input2 = new int[]{1, 4, 4, 4, 6, 6, 7, 7, 8, 10};
k = 8;
ArrayList<String> sort2AnswerList2 = new ArrayList<>(Arrays.asList("(1, 7)", "(4, 4)"));
// With k==8, expected: [(1, 7), (4, 4)]
if ( !Sorting2Fail && ! ps.pair(sorting2Input2, k).equals(sort2AnswerList2) ) {
Sorting2Fail = true;
System.out.println("Sorting 2 Test 2 Failure, returned String: "
+ ps.pair(sorting2Input2, k));
}
/*********************************************************
*
* Display Pass / Fail results along with final score.
*
*********************************************************/
if ( ! PriorityQueueFail ) {
score += 19;
System.out.println("Priority Queue - PASSED");
} else {
System.out.println("Priority Queue - *** FAILED ***");
}
if ( ! PQGameFail ) {
score += 27;
System.out.println("PQ Game - PASSED");
} else {
System.out.println("PQ Game - *** FAILED ***");
}
if ( ! Sorting1Fail ) {
score += 27;
System.out.println("Sort 1 - PASSED");
} else {
System.out.println("Sort 1 - *** FAILED ***");
}
if ( ! Sorting2Fail ) {
score += 27;
System.out.println("Sort 2 - PASSED");
} else {
System.out.println("Sort 2 - *** FAILED ***");
}
System.out.println("\nTotal Score is: " + score);
}
}