KaiquanMah/TurkuBasicOOPinJava
0
1The program has again defined the class FairyTaleCharacter and its derived classes Wizard, Nephew, and Superhero.2 3Write a method:4public static int countWizards(ArrayList<FairyTaleCharacter> characters)5that takes a list of different fairy tale characters as a parameter 6and returns the number of wizards in the list as an integer.7 8 9 10 11 12import java.util.ArrayList;13import java.util.Collections;14import java.util.Random;15 16public class Test {17 public static void main(String[] args){18 final Random r = new Random();19 20 for (int test=1; test<=3; test++) {21 22 System.out.println("Test " + test);23 24 int age = r.nextInt(5) + 5;25 ArrayList<Integer> ages = new ArrayList<>();26 for (int i=0; i<20; i++) {27 ages.add(age);28 age += r.nextInt(10) + 1;29 }30 31 ArrayList<FairyTaleCharacter> characters = new ArrayList<>();32 String[] colors = "blue yellow red black brown white green".split(" ");33 34 for (String name : new String[] {"Benny", "Rupert", "Milo", "Charlie"}) {35 String color = colors[r.nextInt(colors.length)];36 characters.add(new Nephew(name, ages.remove(r.nextInt(ages.size())), color));37 }38 39 String[] superpowers = "super vision flight super strength reading ability super sneeze".split(" ");40 for (String name : new String[] {"Manbat", "Mouseman", "Bulk", "Copperman", "Steelcat"}) {41 String power = superpowers[r.nextInt(superpowers.length)];42 characters.add(new Superhero(name, ages.remove(r.nextInt(ages.size())), power));43 }44 45 ArrayList<FairyTaleCharacter> wizards = new ArrayList<>();46 String[] wands = "oak licorice birch alder walnut plastic".split(" ");47 for (String name : new String[] {"Harry", "Hermione", "Ron", "Voldemort", "Dumbledore", "Snape"}) {48 String wand = wands[r.nextInt(wands.length)];49 wizards.add(new Wizard(name, ages.remove(r.nextInt(ages.size())), wand));50 }51 52 int ch = r.nextInt(characters.size());53 int wz = r.nextInt(wizards.size());54 55 Collections.shuffle(characters, r);56 Collections.shuffle(wizards, r);57 58 ArrayList<FairyTaleCharacter> testList = new ArrayList<>();59 60 for (int i=0; i<ch; i++) {61 testList.add(characters.get(i));62 }63 64 for (int i=0; i<wz; i++) {65 testList.add(wizards.get(i));66 }67 68 Collections.shuffle(testList, r);69 70 System.out.println("Fairy Tale Characters:");71 testList.stream().forEach(c -> System.out.println("" + c));72 73 System.out.println("Wizards: " + countWizards(testList));74 75 System.out.println("");76 }77 }78 79 80 //ADD81 public static int countWizards(ArrayList<FairyTaleCharacter> characters) {82 int countWizards = 0;83 for (FairyTaleCharacter character : characters) {84 if (character instanceof Wizard) {85 countWizards++;86 }87 }88 return countWizards;89 }90 91 92 93 94 95 96 97 98 99}100 101 102 103 104class FairyTaleCharacter {105 private String name;106 private int age;107 108 public FairyTaleCharacter(String name, int age) {109 this.name = name;110 this.age = age;111 }112 113 public String getName() {114 return name;115 }116 117 public int getAge() {118 return age;119 }120 121 @Override122 public String toString() {123 return name + ", " + age + " years";124 }125}126 127 128 129 130class Nephew extends FairyTaleCharacter {131 private String capColor;132 133 public Nephew(String name, int age, String capColor) {134 super(name, age);135 this.capColor = capColor;136 }137 138 @Override139 public String toString() {140 return super.toString() + ", " + capColor + " cap (nephew)";141 }142}143 144 145 146 147class Wizard extends FairyTaleCharacter {148 private String wand;149 150 public Wizard(String name, int age, String wand) {151 super(name, age);152 this.wand = wand;153 }154 155 @Override156 public String toString() {157 return super.toString() + ", wand: " + wand + " (wizard)";158 }159}160 161 162 163 164class Superhero extends FairyTaleCharacter {165 private String superpower;166 167 public Superhero(String name, int age, String superpower) {168 super(name, age);169 this.superpower = superpower;170 }171 172 @Override173 public String toString() {174 return super.toString() + ", superpower: " + superpower + " (superhero)";175 }176}177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196Test 1197Fairy Tale Characters:198Rupert, 111 years, white cap (nephew)199Milo, 69 years, blue cap (nephew)200Hermione, 105 years, wand: alder (wizard)201Steelcat, 42 years, superpower: flight (superhero)202Manbat, 98 years, superpower: super (superhero)203Snape, 50 years, wand: alder (wizard)204Wizards: 2205 206Test 2207Fairy Tale Characters:208Steelcat, 54 years, superpower: vision (superhero)209Manbat, 32 years, superpower: flight (superhero)210Hermione, 88 years, wand: birch (wizard)211Mouseman, 33 years, superpower: super (superhero)212Rupert, 53 years, green cap (nephew)213Wizards: 1214 215Test 3216Fairy Tale Characters:217Benny, 27 years, blue cap (nephew)218Copperman, 43 years, superpower: super (superhero)219Rupert, 140 years, green cap (nephew)220Steelcat, 108 years, superpower: super (superhero)221Mouseman, 120 years, superpower: flight (superhero)222Wizards: 0223 224 225 226 227 228 