File size: 5,649 Bytes
69d533f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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