KaiquanMah commited on
Commit
dc38936
·
verified ·
1 Parent(s): 29a26c5

public void method1(Class1 obj1) throws CustomException, throw new CustomException("txt...")

Browse files
Week 5: Class hierarchies/15. Own Exceptions 2: SuperGroup+++ ADDED
@@ -0,0 +1,342 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Class 'SuperAnimal' is defined in the program.
2
+ Class members include 'name' and 'species', which is written in lower case (for example "cat" or "cow").
3
+ There is also a getter for species.
4
+
5
+
6
+
7
+ Write a class 'SuperGroup' which models 'a group consisting of super animals'.
8
+
9
+ The class should have the following members:
10
+ 1
11
+ Constructor which receives the name of the group as an argument
12
+
13
+ 2
14
+ Method addMember(SuperAnimal animal) which adds a new member to the group.
15
+ However, if after adding a new member, the class would have both, a cat and a dog as member,
16
+ the method throws a new SuperException type exception instead of adding new member.
17
+
18
+ 3
19
+ Method outputGroup() which output the name of the group and its members in the following format
20
+ (notice that class SuperAnimal already has a toString method):
21
+
22
+
23
+
24
+
25
+ Group Woof
26
+ Mad Dog (dog), super strength: Super bite
27
+ Captain Moo (cow), super strength: Can fly
28
+
29
+
30
+
31
+ ================
32
+
33
+
34
+
35
+ import java.util.ArrayList;
36
+ import java.util.Collections;
37
+ import java.util.Random;
38
+
39
+ public class Test{
40
+ public static void main(String[] args){
41
+ final Random r = new Random();
42
+
43
+ System.out.println("Testing class SuperGroup...");
44
+
45
+ String[] names = "Woof Oink Miow Moo Barf Grr Sniff".split(" ");
46
+
47
+ System.out.println("Test 1");
48
+ SuperGroup group = new SuperGroup("Group " + names[r.nextInt(names.length)]);
49
+ System.out.println("Group created!");
50
+
51
+ ArrayList<SuperAnimal> al = new ArrayList<>();
52
+
53
+ al.add(new SuperAnimal("pig", "Mr. Piggy", "Superbreath"));
54
+ al.add(new SuperAnimal("cow", "Captain Moo", "Supermilk"));
55
+ al.add(new SuperAnimal("cat", "Felixx", "Super vision"));
56
+ al.add(new SuperAnimal("cat", "Car-Field", "Supercar"));
57
+ Collections.shuffle(al, r);
58
+
59
+ System.out.println("Adding premitted...");
60
+ for (SuperAnimal animal : al) {
61
+ try {
62
+ group.addMember(animal);
63
+ }
64
+ catch (SuperException e) {
65
+ System.out.println("An exception was thrown when trying to add animal");
66
+ System.out.println(animal);
67
+ }
68
+ System.out.println("Group now:");
69
+ group.outputGroup();
70
+ }
71
+
72
+ al.clear();
73
+ al.add(new SuperAnimal("dog", "Super Goofy", "Flying skill"));
74
+ al.add(new SuperAnimal("dog", "Snoopy", "Super imagination"));
75
+ Collections.shuffle(al, r);
76
+
77
+ System.out.println("Adding forbidden...");
78
+ for (SuperAnimal animal : al) {
79
+ try {
80
+ group.addMember(animal);
81
+ }
82
+ catch (SuperException e) {
83
+ System.out.println("An exception was thrown when trying to add animal");
84
+ System.out.println(animal);
85
+ }
86
+ System.out.println("Group now:");
87
+ group.outputGroup();
88
+ }
89
+
90
+ System.out.println("");
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+ System.out.println("Test 2");
100
+ group = new SuperGroup("Group " + names[r.nextInt(names.length)]);
101
+ System.out.println("Group created!");
102
+
103
+ al.clear();
104
+
105
+ al.add(new SuperAnimal("pig", "Mr. Piggy", "Superbreath"));
106
+ al.add(new SuperAnimal("cow", "Captain Moo", "Super eating"));
107
+ al.add(new SuperAnimal("dog", "Super-Spot", "Super barking"));
108
+ al.add(new SuperAnimal("dog", "Mad Dog", "Super bite"));
109
+ Collections.shuffle(al, r);
110
+
111
+ System.out.println("Adding premitted...");
112
+ for (SuperAnimal animal : al) {
113
+ try {
114
+ group.addMember(animal);
115
+ } catch (SuperException e) {
116
+ System.out.println("An exception was thrown when trying to add animal");
117
+ System.out.println(animal);
118
+ }
119
+ System.out.println("Group now:");
120
+ group.outputGroup();
121
+ }
122
+
123
+ al.clear();
124
+ al.add(new SuperAnimal("cat", "Ms. Garfield", "Super appetite"));
125
+ al.add(new SuperAnimal("cat", "Cat Man", "Super purring"));
126
+ Collections.shuffle(al, r);
127
+
128
+ System.out.println("Adding forbidden...");
129
+ for (SuperAnimal animal : al) {
130
+ try {
131
+ group.addMember(animal);
132
+
133
+ } catch (SuperException e) {
134
+ System.out.println("An exception was thrown when trying to add animal");
135
+ System.out.println(animal);
136
+ }
137
+ System.out.println("Group now:");
138
+ group.outputGroup();
139
+ }
140
+
141
+
142
+
143
+ }
144
+ }
145
+
146
+
147
+
148
+
149
+
150
+
151
+ class SuperException extends Exception {
152
+ public SuperException(String message) {
153
+ super(message);
154
+ }
155
+ }
156
+
157
+
158
+
159
+
160
+
161
+
162
+ class SuperAnimal {
163
+ private String species;
164
+ private String name;
165
+ private String superStrength;
166
+
167
+ public SuperAnimal(String species, String name, String superStrength) {
168
+ this.species = species;
169
+ this.name = name;
170
+ this.superStrength = superStrength;
171
+ }
172
+
173
+ public String getspecies() {
174
+ return species;
175
+ }
176
+
177
+ public String getname() {
178
+ return name;
179
+ }
180
+
181
+ public String getsuperStrength() {
182
+ return superStrength;
183
+ }
184
+
185
+ @Override
186
+ public String toString() {
187
+ return name + " (" + species + "), super strength: " + superStrength;
188
+ }
189
+ }
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+ //ADD
198
+ class SuperGroup {
199
+ //INITIALISE ATTRIBUTES
200
+ private String name;
201
+ private ArrayList<SuperAnimal> listAnimals = new ArrayList<SuperAnimal>();
202
+
203
+ // CONSTRUCTOR
204
+ public SuperGroup(String name) {
205
+ this.name = name;
206
+ }
207
+
208
+
209
+ // remember to add ' throws SuperException'
210
+ // so compiler knows that this method can throw a custom exception
211
+ public void addMember(SuperAnimal animal) throws SuperException {
212
+ boolean containsCat = false;
213
+ boolean containsDog = false;
214
+
215
+ // check existing animals
216
+ for (SuperAnimal animalInList : this.listAnimals) {
217
+ if (animalInList.getspecies().equals("cat")) {
218
+ containsCat = true;
219
+ }
220
+ else if (animalInList.getspecies().equals("dog")) {
221
+ containsDog = true;
222
+ }
223
+ }
224
+
225
+ // check new animal
226
+ if (animal.getspecies().equals("cat")) {
227
+ containsCat = true;
228
+ }
229
+ else if (animal.getspecies().equals("dog")) {
230
+ containsDog = true;
231
+ }
232
+
233
+ // if we have both cats and dogs in the list, then throw an exception
234
+ if (containsCat && containsDog) {
235
+ throw new SuperException("Group already contains a cat and a dog");
236
+ }
237
+ // if we dont have both cats and dogs in the list, then add SuperAnimal to the list
238
+ this.listAnimals.add(animal);
239
+ }
240
+
241
+ public void outputGroup() {
242
+ System.out.println(this.name);
243
+ for (SuperAnimal animal : this.listAnimals) {
244
+ System.out.println(animal);
245
+ }
246
+ }
247
+ }
248
+
249
+
250
+
251
+
252
+ Testing class SuperGroup...
253
+ Test 1
254
+ Group created!
255
+ Adding premitted...
256
+ Group now:
257
+ Group Miow
258
+ Felixx (cat), super strength: Super vision
259
+ Group now:
260
+ Group Miow
261
+ Felixx (cat), super strength: Super vision
262
+ Car-Field (cat), super strength: Supercar
263
+ Group now:
264
+ Group Miow
265
+ Felixx (cat), super strength: Super vision
266
+ Car-Field (cat), super strength: Supercar
267
+ Captain Moo (cow), super strength: Supermilk
268
+ Group now:
269
+ Group Miow
270
+ Felixx (cat), super strength: Super vision
271
+ Car-Field (cat), super strength: Supercar
272
+ Captain Moo (cow), super strength: Supermilk
273
+ Mr. Piggy (pig), super strength: Superbreath
274
+
275
+ Adding forbidden...
276
+ An exception was thrown when trying to add animal
277
+ Super Goofy (dog), super strength: Flying skill
278
+ Group now:
279
+ Group Miow
280
+ Felixx (cat), super strength: Super vision
281
+ Car-Field (cat), super strength: Supercar
282
+ Captain Moo (cow), super strength: Supermilk
283
+ Mr. Piggy (pig), super strength: Superbreath
284
+
285
+ An exception was thrown when trying to add animal
286
+ Snoopy (dog), super strength: Super imagination
287
+ Group now:
288
+ Group Miow
289
+ Felixx (cat), super strength: Super vision
290
+ Car-Field (cat), super strength: Supercar
291
+ Captain Moo (cow), super strength: Supermilk
292
+ Mr. Piggy (pig), super strength: Superbreath
293
+
294
+
295
+
296
+
297
+
298
+
299
+
300
+ Test 2
301
+ Group created!
302
+ Adding premitted...
303
+ Group now:
304
+ Group Grr
305
+ Super-Spot (dog), super strength: Super barking
306
+ Group now:
307
+ Group Grr
308
+ Super-Spot (dog), super strength: Super barking
309
+ Captain Moo (cow), super strength: Super eating
310
+ Group now:
311
+ Group Grr
312
+ Super-Spot (dog), super strength: Super barking
313
+ Captain Moo (cow), super strength: Super eating
314
+ Mad Dog (dog), super strength: Super bite
315
+ Group now:
316
+ Group Grr
317
+ Super-Spot (dog), super strength: Super barking
318
+ Captain Moo (cow), super strength: Super eating
319
+ Mad Dog (dog), super strength: Super bite
320
+ Mr. Piggy (pig), super strength: Superbreath
321
+
322
+ Adding forbidden...
323
+ An exception was thrown when trying to add animal
324
+ Cat Man (cat), super strength: Super purring
325
+ Group now:
326
+ Group Grr
327
+ Super-Spot (dog), super strength: Super barking
328
+ Captain Moo (cow), super strength: Super eating
329
+ Mad Dog (dog), super strength: Super bite
330
+ Mr. Piggy (pig), super strength: Superbreath
331
+
332
+ An exception was thrown when trying to add animal
333
+ Ms. Garfield (cat), super strength: Super appetite
334
+ Group now:
335
+ Group Grr
336
+ Super-Spot (dog), super strength: Super barking
337
+ Captain Moo (cow), super strength: Super eating
338
+ Mad Dog (dog), super strength: Super bite
339
+ Mr. Piggy (pig), super strength: Superbreath
340
+
341
+
342
+