KaiquanMah commited on
Commit
69d533f
·
verified ·
1 Parent(s): 5ecac4f

if (obj1 instanceof Class1) { countClass1++; }

Browse files
Week 6: Methods of OO Programming/12B. Count the Wizards [instanceof] ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The program has again defined the class FairyTaleCharacter and its derived classes Wizard, Nephew, and Superhero.
2
+
3
+ Write a method:
4
+ public static int countWizards(ArrayList<FairyTaleCharacter> characters)
5
+ that takes a list of different fairy tale characters as a parameter
6
+ and returns the number of wizards in the list as an integer.
7
+
8
+
9
+
10
+
11
+
12
+ import java.util.ArrayList;
13
+ import java.util.Collections;
14
+ import java.util.Random;
15
+
16
+ public 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
+ //ADD
81
+ 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
+
104
+ class 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
+ @Override
122
+ public String toString() {
123
+ return name + ", " + age + " years";
124
+ }
125
+ }
126
+
127
+
128
+
129
+
130
+ class 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
+ @Override
139
+ public String toString() {
140
+ return super.toString() + ", " + capColor + " cap (nephew)";
141
+ }
142
+ }
143
+
144
+
145
+
146
+
147
+ class 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
+ @Override
156
+ public String toString() {
157
+ return super.toString() + ", wand: " + wand + " (wizard)";
158
+ }
159
+ }
160
+
161
+
162
+
163
+
164
+ class 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
+ @Override
173
+ 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
+
196
+ Test 1
197
+ Fairy Tale Characters:
198
+ Rupert, 111 years, white cap (nephew)
199
+ Milo, 69 years, blue cap (nephew)
200
+ Hermione, 105 years, wand: alder (wizard)
201
+ Steelcat, 42 years, superpower: flight (superhero)
202
+ Manbat, 98 years, superpower: super (superhero)
203
+ Snape, 50 years, wand: alder (wizard)
204
+ Wizards: 2
205
+
206
+ Test 2
207
+ Fairy Tale Characters:
208
+ Steelcat, 54 years, superpower: vision (superhero)
209
+ Manbat, 32 years, superpower: flight (superhero)
210
+ Hermione, 88 years, wand: birch (wizard)
211
+ Mouseman, 33 years, superpower: super (superhero)
212
+ Rupert, 53 years, green cap (nephew)
213
+ Wizards: 1
214
+
215
+ Test 3
216
+ Fairy Tale Characters:
217
+ Benny, 27 years, blue cap (nephew)
218
+ Copperman, 43 years, superpower: super (superhero)
219
+ Rupert, 140 years, green cap (nephew)
220
+ Steelcat, 108 years, superpower: super (superhero)
221
+ Mouseman, 120 years, superpower: flight (superhero)
222
+ Wizards: 0
223
+
224
+
225
+
226
+
227
+