-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblemSet2A.java
348 lines (297 loc) · 8.1 KB
/
ProblemSet2A.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
package ProblemSets;
import android.content.Context;
import android.content.Intent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ProblemSet2A {
public static void main(String[] args) {
ArrayList<Octagon1> l = new ArrayList<Octagon1>();
l.add(new Octagon1(2));
l.add(new Octagon1(3));
l.add(new Octagon1(1));
Collections.sort(l, new OctagonComparator());
for (Octagon1 o : l){
System.out.println(o.getSide());
}
AirPoulltionAlert singaporeAlert = new AirPoulltionAlert();
Subscriber man = new Subscriber("man", singaporeAlert);
Subscriber simon = new Subscriber("simon", singaporeAlert);
singaporeAlert.setAirPollutionIndex(200);
singaporeAlert.setAirPollutionIndex(50);
singaporeAlert.setAirPollutionIndex(120);
singaporeAlert.unregister(man);
singaporeAlert.setAirPollutionIndex(300);
}
}
// Question 1: Comparable Interface
// todo: modify octagon class to implement Comparable<Octagon> interface to allow sorting of
// Octagon objects based on their parameters.
class Octagon implements Comparable<Octagon>{
private double side;
public Octagon(double side){
this.side = side;
}
public double getSide(){
return side;
}
@Override
public int compareTo(Octagon octagon){
if (side == octagon.side){
return 0;
} else if (side > octagon.side) {
return 1;
}
else {
return -1;
}
}
public static void main(String[] args) {
ArrayList<Octagon> l = new ArrayList<Octagon>();
l.add(new Octagon(2));
l.add(new Octagon(3));
l.add(new Octagon(1));
Collections.sort(l);
for (Octagon o : l){
System.out.println(o.getSide());
}
}
}
// Question 2: Comparator Interface
// todo: given same code & assumptions from previous question
// implement a OctagonComparator class to implement a Comparable<Octagon> interface to allow
// sorting of Octagon object based on their parameters.
class Octagon1 {
private double side;
public Octagon1 (double side){
this.side = side;
}
public double getSide(){
return side;
}
}
class OctagonComparator implements Comparator<Octagon1> {
@Override
public int compare(Octagon1 o1, Octagon1 o2){
Octagon1 obj1 = (Octagon1) o1;
Octagon1 obj2 = (Octagon1) o2;
if (obj1.getSide() == obj2.getSide()){
return 0;
} else if (obj1.getSide() > obj2.getSide()) {
return 1;
}
else {
return -1;
}
}
}
// Question 3: Observer
interface Observer {
void update(double airPollutionIndex);
}
interface Subject{
void register(Observer o);
void unregister(Observer o);
void notifyObservers();
}
class Subscriber implements Observer {
private Subject subject;
private String observerId;
public static String outputMsg = "";
// constructor.
public Subscriber(String observerId, Subject subject){
this.subject = subject;
this.observerId = observerId;
this.subject.register(this); // register itself
}
@Override
public void update(double airPollutionIndex){
String s = this.observerId + " received notification: " + airPollutionIndex;
System.out.println(s);
outputMsg += (s + " ");
}
}
// todo: modify AirPollutionAlert to implement interface Subject, under Observer design pattern
class AirPoulltionAlert implements Subject{
private double airPollutionIndex;
private List<Observer> observers;
public AirPoulltionAlert(){
this.observers = new ArrayList<>();
}
public void setAirPollutionIndex(double airPollutionIndex){
this.airPollutionIndex = airPollutionIndex;
if (airPollutionIndex > 100){
notifyObservers();
}
}
@Override
public void register(Observer o){
observers.add(o);
}
@Override
public void unregister(Observer o){
observers.remove(o);
}
@Override
public void notifyObservers(){
for (Observer observer : observers){
// notify the observers
observer.update(this.airPollutionIndex);
}
}
}
// Question 4
interface I1 {
int p1();
}
interface I2 {
int p2();
}
interface I3 {
int p3();
}
interface I4 extends I1, I2, I3 {
int p4();
}
interface I5 extends I3 {
int p5();
}
abstract class C1 implements I4 {
abstract int q1();
}
class C2 extends C1 implements I5 {
@Override
public int p4(){
return 0;
}
@Override
public int p1(){
return 0;
}
@Override
public int p2(){
return 0;
}
@Override
public int p3(){
return 0;
}
@Override
int q1(){
return 0;
}
@Override
public int p5(){
return 0;
}
}
class C3 implements I5 {
@Override
public int p5(){
return 0;
}
@Override
public int p3(){
return 0;
}
}
// Question 5: Palindrome
class Palindrome{
static boolean checkPalindrome(String str, int i, int j){
if (i == j){
return true;
}
if ((str.charAt(i)) != (str.charAt(j))){
return false;
}
if (i < j + 1){
return checkPalindrome(str, i + 1, j -1);
}
return true;
}
static boolean isPalindrome(String input){
int n = input.length();
if(n == 0){
return true;
}
return checkPalindrome(input, 0, n - 1);
}
public static void main(String[] args) {
System.out.println(isPalindrome("abba")); // true
System.out.println(isPalindrome("adbcba")); // false
System.out.println(isPalindrome("ZZaZZ")); // true
System.out.println(isPalindrome("123421")); // false
}
}
// Question 6: Permutation
class Permutation{
private final String in;
private ArrayList<String> a = new ArrayList<String>();
Permutation(final String str){
in = str;
}
public void permute(){
permuteHelper("", in);
}
private void permuteHelper(String prefix, String postfix){
// todo: produce the permuted sequences of 'in' and store in array a, recursively
int n = postfix.length();
// base case
if (n == 0){
a.add(prefix);
}
else {
for (int i = 0; i < n; i++){
permuteHelper(prefix + postfix.charAt(i),
postfix.substring(0, i) + postfix.substring(i + 1, n));
}
}
}
public ArrayList<String> getA() {
return a;
}
public static void main(String[] args) {
ArrayList<String> v;
Permutation p = new Permutation("ABC");
p.permute();
v = p.getA();
System.out.println(v);
}
}
// Question 8: ArrayIndexOutOfBoundsException
class Exception{
public static String tstException(int idx, String[] y){
String result;
// todo: try catch to get ArrayIndexOutOfBounds
try {
result = y[idx];
}
catch (ArrayIndexOutOfBoundsException e){
result = "Out of Bounds";
}
return result;
}
public static void main(String[] args) {
String[] in = {"hello", "good night", "good morning"};
String ret = tstException(2, in);
System.out.println(ret);
ret = tstException(-1, in);
System.out.println(ret);
}
}
// SINGLETON IMPLEMENTATION EXAMPLE
class Connection {
// make your constructor private to prevent instantiation by constructor
// prevent inheritance, because super() would not work
private Connection(){}
// static factory method (factory -> make objects)
private static Connection connection;
// either return connection, or instantiate and return
public static Connection getInstance(){
if (connection == null){
connection = new Connection();
}
return connection;
}
}