TurkuBasicOOPinJava / Week 5: Class hierarchies /15. Own Exceptions 2: SuperGroup+++
KaiquanMah's picture
public void method1(Class1 obj1) throws CustomException, throw new CustomException("txt...")
dc38936 verified
raw
history blame
9.42 kB
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