Spaces:
Running
Running
catch (CustomException e)
Browse files
Week 5: Class hierarchies/16. Own Exceptions 3: Catching Exceptions
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write a method
|
| 2 |
+
public static void addToGroup(SuperGroup group, ArrayList<SuperAnimal> animals)
|
| 3 |
+
|
| 4 |
+
which receives a 'super group' and 'a list of super animals' as arguments.
|
| 5 |
+
|
| 6 |
+
Method tries to add the members to group one at a time.
|
| 7 |
+
For each animal, the method outputs if the adding was successful or not (see the example below).
|
| 8 |
+
After iterating through all animals, the group is output using the 'outputGroup method' in class SuperGroup.
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
Example of calling the method:
|
| 13 |
+
public static void main(String[] args) {
|
| 14 |
+
//LIST OF ANIMALS
|
| 15 |
+
ArrayList<SuperAnimal> al = new ArrayList<>();
|
| 16 |
+
al.add(new SuperAnimal("pig", "Fifer", "Super musician"));
|
| 17 |
+
al.add(new SuperAnimal("cat", "Crazy Kat", "Super strength"));
|
| 18 |
+
al.add(new SuperAnimal("dog", "Super Snoops", "Super barking"));
|
| 19 |
+
|
| 20 |
+
//GROUP
|
| 21 |
+
SuperGroup group = new Supergroup(The Barkvengers");
|
| 22 |
+
|
| 23 |
+
// FOR SPECIFIED GROUP, ADD LIST OF ANIMALS
|
| 24 |
+
addToGroup(group, al);
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
Program outputs:
|
| 29 |
+
Success: Fifer (pig), super strength: Supermusisointi
|
| 30 |
+
Success: Crazy Kat (cat), super strength: Super strength
|
| 31 |
+
Fail: Super Snoops(dog), super strength: Super barking
|
| 32 |
+
The Barkvengers
|
| 33 |
+
Fifer (pig), super strength: Super musician
|
| 34 |
+
Crazy Kat (cat), super strength: super strength
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
======================================
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
import java.util.Random;
|
| 42 |
+
import java.util.ArrayList;
|
| 43 |
+
import java.util.Collections;
|
| 44 |
+
|
| 45 |
+
public class Test{
|
| 46 |
+
public static void main(String[] args){
|
| 47 |
+
final Random r = new Random();
|
| 48 |
+
|
| 49 |
+
ArrayList<SuperAnimal> al = new ArrayList<>();
|
| 50 |
+
|
| 51 |
+
al.add(new SuperAnimal("pig", "Mr. Piggy", "Superbreath"));
|
| 52 |
+
al.add(new SuperAnimal("cow", "Captain Moo", "Supermilk"));
|
| 53 |
+
al.add(new SuperAnimal("cat", "Felixx", "Super vision"));
|
| 54 |
+
al.add(new SuperAnimal("cat", "Car-Field", "Supercar"));
|
| 55 |
+
|
| 56 |
+
Collections.shuffle(al, r);
|
| 57 |
+
SuperGroup group = new SuperGroup("Ryhmä Äks");
|
| 58 |
+
addToGroup(group, al);
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
//ADD
|
| 67 |
+
public static void addToGroup(SuperGroup group, ArrayList<SuperAnimal> animals) {
|
| 68 |
+
for (SuperAnimal animal : animals) {
|
| 69 |
+
try {
|
| 70 |
+
group.addMember(animal);
|
| 71 |
+
System.out.println("Success: " + animal);
|
| 72 |
+
}
|
| 73 |
+
catch (SuperException e) {
|
| 74 |
+
System.out.println("Fail: " + animal);
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
group.outputGroup();
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
Success: Felixx (cat), superStrength: Super vision
|
| 89 |
+
Success: Car-Field (cat), superStrength: Supercar
|
| 90 |
+
Success: Captain Moo (cow), superStrength: Supermilk
|
| 91 |
+
Success: Mr. Piggy (pig), superStrength: Superbreath
|
| 92 |
+
Ryhmä Äks
|
| 93 |
+
Felixx (cat), superStrength: Super vision
|
| 94 |
+
Car-Field (cat), superStrength: Supercar
|
| 95 |
+
Captain Moo (cow), superStrength: Supermilk
|
| 96 |
+
Mr. Piggy (pig), superStrength: Superbreath
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|