Skip to content

Commit 466e267

Browse files
committed
more challenges
1 parent caf3b12 commit 466e267

2 files changed

Lines changed: 80 additions & 4 deletions

File tree

src/collections/challenges.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,79 @@ Remember the rules for this are
66
- Try not to give up until you've given it a solid attempt
77

88
## Challenge 1.
9+
10+
Replace uses of the "concrete" collection types (`ArrayList`, `HashMap`, `HashSet`)
11+
in the following code with the corresponding collection interfaces.
12+
13+
You want to keep the calls like `new ArrayList` - only change how variables are typed.
14+
15+
```java,editable
16+
import java.util.ArrayList;
17+
import java.util.HashMap;
18+
import java.util.HashSet;
19+
import java.util.List;
20+
import java.util.Map;
21+
import java.util.Set;
22+
23+
class Main {
24+
void main() {
25+
ArrayList<String> jedi = new ArrayList<>();
26+
jedi.add("Luke");
27+
jedi.add("Anakin");
28+
jedi.add("Qui-Gon");
29+
jedi.add("Obi-Wan");
30+
31+
HashSet<String> sith = new HashSet<>();
32+
sith.add("Palpatine");
33+
34+
HashMap<String, String> winningMatchups
35+
= new HashMap<>();
36+
winningMatchups.put("Anakin", "Palpatine");
37+
winningMatchups.put("Obi-Wan", "Jar-Jar");
38+
39+
for (var j : jedi) {
40+
IO.println(j + " is a jedi");
41+
42+
var matchup = winningMatchups.get(j);
43+
if (matchup != null) {
44+
if (sith.contains(matchup)) {
45+
IO.println(j + " would win against " + matchup);
46+
}
47+
else {
48+
IO.println(j + " would win against " + matchup + " (but they aren't sith)");
49+
}
50+
}
51+
52+
}
53+
}
54+
}
55+
```
56+
57+
## Challenge 2.
58+
59+
Call `methodB` using the array returned from `methodA`.
60+
61+
```java
62+
class Main {
63+
String[] methodA() {
64+
return new String[] {
65+
"Chewbacca",
66+
"Attichitcuk",
67+
"Mallatobuck",
68+
"Lumpawaroo"
69+
}
70+
}
71+
72+
void methodB(List<String> character) {
73+
IO.println("Characters in the Star Wars Christmas Special:");
74+
IO.println("----------------");
75+
for (item : character) {
76+
IO.println(item);
77+
}
78+
}
79+
80+
void main() {
81+
// CODE HERE
82+
}
83+
}
84+
```

src/multi_dimensional_arrays/challenges.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ void main() {
8888

8989
IO.println(winnerB);
9090

91-
var winnerC = winner(new Character[][] {
92-
new Character[] { "O", "X", "O" },
93-
new Character[] { "O", "O", "X" },
94-
new Character[] { "O", "X", "O" }
91+
var winnerC = winner(new String[][] {
92+
new String[] { "O", "X", "O" },
93+
new String[] { "O", "O", "X" },
94+
new String[] { "O", "X", "O" }
9595
});
9696

9797
IO.println(winnerC);

0 commit comments

Comments
 (0)