VillainTracker.java
3 bugs hidden
1
import java.util.ArrayList;
2
import java.util.HashMap;
3
4
public class VillainTracker {
5
6
public static void main(String[] args) {
7
8
9
10
villains.add("Shadow Mox");
11
villains.add("Plague Vesper");
12
villains.add("Doctor Nullius");
13
14
HashMap<String, String> threatLevels = new HashMap<>();
15
16
threatLevels.put("Shadow Mox", "HIGH");
17
threatLevels.put("Plague Vesper", "CRITICAL");
18
threatLevels.put("Doctor Nullius", "EXTREME");
19
20
System.out.println("--- Villain Threat Report ---");
21
22
23
24
25
String villain = villains.get(i);
26
27
System.out.println(villain + ": " + threat);
28
}
29
}
30
}
Section 1: Declarations
Anchored to: ArrayList declaration (line 8)
Look at how this ArrayList is declared. What is missing, and why does it matter?
Anchored to: ArrayList add calls (lines 10-12)
Three items are added to the list. What is the index of the last item? What happens if you try to access index 3?
Section 2: HashMap
Anchored to: HashMap declaration (line 14)
This HashMap maps one type to another. What are the key and value types, and what does each represent?
Anchored to: HashMap .get() call (line 24)
Look at what is being passed into .get() on this line. What type does the HashMap expect as a key? What is actually being passed in, and why is that a problem?
Section 3: The Loop
Anchored to: for loop (line 22)
Trace through this loop. How many times does it execute? How many items are in the list? What happens on the final iteration?
Anchored to: full program
You have identified all three bugs and written your fixes above. Now write the exact output this program should produce once all three are corrected.