TurkuBasicOOPinJava / Week 6: Methods of OO Programming /12B. Count the Wizards [instanceof]
KaiquanMah's picture
if (obj1 instanceof Class1) { countClass1++; }
69d533f verified
The program has again defined the class FairyTaleCharacter and its derived classes Wizard, Nephew, and Superhero.
Write a method:
public static int countWizards(ArrayList<FairyTaleCharacter> characters)
that takes a list of different fairy tale characters as a parameter
and returns the number of wizards in the list as an integer.
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();
for (int test=1; test<=3; test++) {
System.out.println("Test " + test);
int age = r.nextInt(5) + 5;
ArrayList<Integer> ages = new ArrayList<>();
for (int i=0; i<20; i++) {
ages.add(age);
age += r.nextInt(10) + 1;
}
ArrayList<FairyTaleCharacter> characters = new ArrayList<>();
String[] colors = "blue yellow red black brown white green".split(" ");
for (String name : new String[] {"Benny", "Rupert", "Milo", "Charlie"}) {
String color = colors[r.nextInt(colors.length)];
characters.add(new Nephew(name, ages.remove(r.nextInt(ages.size())), color));
}
String[] superpowers = "super vision flight super strength reading ability super sneeze".split(" ");
for (String name : new String[] {"Manbat", "Mouseman", "Bulk", "Copperman", "Steelcat"}) {
String power = superpowers[r.nextInt(superpowers.length)];
characters.add(new Superhero(name, ages.remove(r.nextInt(ages.size())), power));
}
ArrayList<FairyTaleCharacter> wizards = new ArrayList<>();
String[] wands = "oak licorice birch alder walnut plastic".split(" ");
for (String name : new String[] {"Harry", "Hermione", "Ron", "Voldemort", "Dumbledore", "Snape"}) {
String wand = wands[r.nextInt(wands.length)];
wizards.add(new Wizard(name, ages.remove(r.nextInt(ages.size())), wand));
}
int ch = r.nextInt(characters.size());
int wz = r.nextInt(wizards.size());
Collections.shuffle(characters, r);
Collections.shuffle(wizards, r);
ArrayList<FairyTaleCharacter> testList = new ArrayList<>();
for (int i=0; i<ch; i++) {
testList.add(characters.get(i));
}
for (int i=0; i<wz; i++) {
testList.add(wizards.get(i));
}
Collections.shuffle(testList, r);
System.out.println("Fairy Tale Characters:");
testList.stream().forEach(c -> System.out.println("" + c));
System.out.println("Wizards: " + countWizards(testList));
System.out.println("");
}
}
//ADD
public static int countWizards(ArrayList<FairyTaleCharacter> characters) {
int countWizards = 0;
for (FairyTaleCharacter character : characters) {
if (character instanceof Wizard) {
countWizards++;
}
}
return countWizards;
}
}
class FairyTaleCharacter {
private String name;
private int age;
public FairyTaleCharacter(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return name + ", " + age + " years";
}
}
class Nephew extends FairyTaleCharacter {
private String capColor;
public Nephew(String name, int age, String capColor) {
super(name, age);
this.capColor = capColor;
}
@Override
public String toString() {
return super.toString() + ", " + capColor + " cap (nephew)";
}
}
class Wizard extends FairyTaleCharacter {
private String wand;
public Wizard(String name, int age, String wand) {
super(name, age);
this.wand = wand;
}
@Override
public String toString() {
return super.toString() + ", wand: " + wand + " (wizard)";
}
}
class Superhero extends FairyTaleCharacter {
private String superpower;
public Superhero(String name, int age, String superpower) {
super(name, age);
this.superpower = superpower;
}
@Override
public String toString() {
return super.toString() + ", superpower: " + superpower + " (superhero)";
}
}
Test 1
Fairy Tale Characters:
Rupert, 111 years, white cap (nephew)
Milo, 69 years, blue cap (nephew)
Hermione, 105 years, wand: alder (wizard)
Steelcat, 42 years, superpower: flight (superhero)
Manbat, 98 years, superpower: super (superhero)
Snape, 50 years, wand: alder (wizard)
Wizards: 2
Test 2
Fairy Tale Characters:
Steelcat, 54 years, superpower: vision (superhero)
Manbat, 32 years, superpower: flight (superhero)
Hermione, 88 years, wand: birch (wizard)
Mouseman, 33 years, superpower: super (superhero)
Rupert, 53 years, green cap (nephew)
Wizards: 1
Test 3
Fairy Tale Characters:
Benny, 27 years, blue cap (nephew)
Copperman, 43 years, superpower: super (superhero)
Rupert, 140 years, green cap (nephew)
Steelcat, 108 years, superpower: super (superhero)
Mouseman, 120 years, superpower: flight (superhero)
Wizards: 0