Spaces:
Running
Running
File size: 9,419 Bytes
dc38936 |
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 |
Class 'SuperAnimal' is defined in the program.
Class members include 'name' and 'species', which is written in lower case (for example "cat" or "cow").
There is also a getter for species.
Write a class 'SuperGroup' which models 'a group consisting of super animals'.
The class should have the following members:
1
Constructor which receives the name of the group as an argument
2
Method addMember(SuperAnimal animal) which adds a new member to the group.
However, if after adding a new member, the class would have both, a cat and a dog as member,
the method throws a new SuperException type exception instead of adding new member.
3
Method outputGroup() which output the name of the group and its members in the following format
(notice that class SuperAnimal already has a toString method):
Group Woof
Mad Dog (dog), super strength: Super bite
Captain Moo (cow), super strength: Can fly
================
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Test{
public static void main(String[] args){
final Random r = new Random();
System.out.println("Testing class SuperGroup...");
String[] names = "Woof Oink Miow Moo Barf Grr Sniff".split(" ");
System.out.println("Test 1");
SuperGroup group = new SuperGroup("Group " + names[r.nextInt(names.length)]);
System.out.println("Group created!");
ArrayList<SuperAnimal> al = new ArrayList<>();
al.add(new SuperAnimal("pig", "Mr. Piggy", "Superbreath"));
al.add(new SuperAnimal("cow", "Captain Moo", "Supermilk"));
al.add(new SuperAnimal("cat", "Felixx", "Super vision"));
al.add(new SuperAnimal("cat", "Car-Field", "Supercar"));
Collections.shuffle(al, r);
System.out.println("Adding premitted...");
for (SuperAnimal animal : al) {
try {
group.addMember(animal);
}
catch (SuperException e) {
System.out.println("An exception was thrown when trying to add animal");
System.out.println(animal);
}
System.out.println("Group now:");
group.outputGroup();
}
al.clear();
al.add(new SuperAnimal("dog", "Super Goofy", "Flying skill"));
al.add(new SuperAnimal("dog", "Snoopy", "Super imagination"));
Collections.shuffle(al, r);
System.out.println("Adding forbidden...");
for (SuperAnimal animal : al) {
try {
group.addMember(animal);
}
catch (SuperException e) {
System.out.println("An exception was thrown when trying to add animal");
System.out.println(animal);
}
System.out.println("Group now:");
group.outputGroup();
}
System.out.println("");
System.out.println("Test 2");
group = new SuperGroup("Group " + names[r.nextInt(names.length)]);
System.out.println("Group created!");
al.clear();
al.add(new SuperAnimal("pig", "Mr. Piggy", "Superbreath"));
al.add(new SuperAnimal("cow", "Captain Moo", "Super eating"));
al.add(new SuperAnimal("dog", "Super-Spot", "Super barking"));
al.add(new SuperAnimal("dog", "Mad Dog", "Super bite"));
Collections.shuffle(al, r);
System.out.println("Adding premitted...");
for (SuperAnimal animal : al) {
try {
group.addMember(animal);
} catch (SuperException e) {
System.out.println("An exception was thrown when trying to add animal");
System.out.println(animal);
}
System.out.println("Group now:");
group.outputGroup();
}
al.clear();
al.add(new SuperAnimal("cat", "Ms. Garfield", "Super appetite"));
al.add(new SuperAnimal("cat", "Cat Man", "Super purring"));
Collections.shuffle(al, r);
System.out.println("Adding forbidden...");
for (SuperAnimal animal : al) {
try {
group.addMember(animal);
} catch (SuperException e) {
System.out.println("An exception was thrown when trying to add animal");
System.out.println(animal);
}
System.out.println("Group now:");
group.outputGroup();
}
}
}
class SuperException extends Exception {
public SuperException(String message) {
super(message);
}
}
class SuperAnimal {
private String species;
private String name;
private String superStrength;
public SuperAnimal(String species, String name, String superStrength) {
this.species = species;
this.name = name;
this.superStrength = superStrength;
}
public String getspecies() {
return species;
}
public String getname() {
return name;
}
public String getsuperStrength() {
return superStrength;
}
@Override
public String toString() {
return name + " (" + species + "), super strength: " + superStrength;
}
}
//ADD
class SuperGroup {
//INITIALISE ATTRIBUTES
private String name;
private ArrayList<SuperAnimal> listAnimals = new ArrayList<SuperAnimal>();
// CONSTRUCTOR
public SuperGroup(String name) {
this.name = name;
}
// remember to add ' throws SuperException'
// so compiler knows that this method can throw a custom exception
public void addMember(SuperAnimal animal) throws SuperException {
boolean containsCat = false;
boolean containsDog = false;
// check existing animals
for (SuperAnimal animalInList : this.listAnimals) {
if (animalInList.getspecies().equals("cat")) {
containsCat = true;
}
else if (animalInList.getspecies().equals("dog")) {
containsDog = true;
}
}
// check new animal
if (animal.getspecies().equals("cat")) {
containsCat = true;
}
else if (animal.getspecies().equals("dog")) {
containsDog = true;
}
// if we have both cats and dogs in the list, then throw an exception
if (containsCat && containsDog) {
throw new SuperException("Group already contains a cat and a dog");
}
// if we dont have both cats and dogs in the list, then add SuperAnimal to the list
this.listAnimals.add(animal);
}
public void outputGroup() {
System.out.println(this.name);
for (SuperAnimal animal : this.listAnimals) {
System.out.println(animal);
}
}
}
Testing class SuperGroup...
Test 1
Group created!
Adding premitted...
Group now:
Group Miow
Felixx (cat), super strength: Super vision
Group now:
Group Miow
Felixx (cat), super strength: Super vision
Car-Field (cat), super strength: Supercar
Group now:
Group Miow
Felixx (cat), super strength: Super vision
Car-Field (cat), super strength: Supercar
Captain Moo (cow), super strength: Supermilk
Group now:
Group Miow
Felixx (cat), super strength: Super vision
Car-Field (cat), super strength: Supercar
Captain Moo (cow), super strength: Supermilk
Mr. Piggy (pig), super strength: Superbreath
Adding forbidden...
An exception was thrown when trying to add animal
Super Goofy (dog), super strength: Flying skill
Group now:
Group Miow
Felixx (cat), super strength: Super vision
Car-Field (cat), super strength: Supercar
Captain Moo (cow), super strength: Supermilk
Mr. Piggy (pig), super strength: Superbreath
An exception was thrown when trying to add animal
Snoopy (dog), super strength: Super imagination
Group now:
Group Miow
Felixx (cat), super strength: Super vision
Car-Field (cat), super strength: Supercar
Captain Moo (cow), super strength: Supermilk
Mr. Piggy (pig), super strength: Superbreath
Test 2
Group created!
Adding premitted...
Group now:
Group Grr
Super-Spot (dog), super strength: Super barking
Group now:
Group Grr
Super-Spot (dog), super strength: Super barking
Captain Moo (cow), super strength: Super eating
Group now:
Group Grr
Super-Spot (dog), super strength: Super barking
Captain Moo (cow), super strength: Super eating
Mad Dog (dog), super strength: Super bite
Group now:
Group Grr
Super-Spot (dog), super strength: Super barking
Captain Moo (cow), super strength: Super eating
Mad Dog (dog), super strength: Super bite
Mr. Piggy (pig), super strength: Superbreath
Adding forbidden...
An exception was thrown when trying to add animal
Cat Man (cat), super strength: Super purring
Group now:
Group Grr
Super-Spot (dog), super strength: Super barking
Captain Moo (cow), super strength: Super eating
Mad Dog (dog), super strength: Super bite
Mr. Piggy (pig), super strength: Superbreath
An exception was thrown when trying to add animal
Ms. Garfield (cat), super strength: Super appetite
Group now:
Group Grr
Super-Spot (dog), super strength: Super barking
Captain Moo (cow), super strength: Super eating
Mad Dog (dog), super strength: Super bite
Mr. Piggy (pig), super strength: Superbreath
|