CS 61B Spring 2021
1 Static Electricity
Scope, Static, and Linked Lists Discussion 3: February 01, 2021
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
public class Pokemon {
public String name;
public int level;
public static String trainer = “Ash”; public static int partySize = 0;
public Pokemon(String name, int level) { this.name = name;
this.level = level;
this.partySize += 1;
}
public static void main(String[] args) {
Pokemon p = new Pokemon(“Pikachu”, 17);
Pokemon j = new Pokemon(“Jolteon”, 99); System.out.println(“Party size: ” + Pokemon.partySize); p.printStats()
int level = 18; Pokemon.change(p, level); p.printStats() Pokemon.trainer = “Ash”; j.trainer = “Brock”; p.printStats();
}
public static void change(Pokemon poke, int level) { poke.level = level;
level = 50;
poke = new Pokemon(“Voltorb”, 1);
poke.trainer = “Team Rocket”;
}
public void printStats() {
System.out.print(name + ” ” + level + ” ” + trainer);
} }
2 Scope, Static, and Linked Lists
(a) Write what would be printed after the main method is executed.
Party Size: 2
Pikachu 17 Ash
Pikachu 18 Team Rocket
Pikachu 18 Brock
(b) On line 28, we set level equal to 50. What level do we mean? An instance variable of the Pokemon object? The local variable containing the parameter to the change method? The local variable in the main method? Something else?
It is the local variable in the change method and does not have any effect on the other variables of the same name in the Pokemon class or the main method.
(c) If we were to call Pokemon.printStats() at the end of our main method, what would happen?
If we were to add this line to our main method, it would error. In the class, printStats() is an instance method. What would it mean to print the name and level of the class Pokemon, as opposed to a specific Pokemon¡¯s name? It doesn¡¯t really make sense. So when we try to run this method on our class, it errors.
One more thing to note is the method change is declared static itself. Static methods can be called using the name of the class, as in line 19, whereas non-static methods cannot. The golden rule for static methods to know is that static methods can only modify static variables.
2 To Do List
Draw the box-and-pointer diagram that results from running the following code. A StringList is similar to an IntList. It has two instance variables, first and rest.
1 StringList L = new StringList(“eat”, null);
2 L = new StringList(“should”, L);
3 L = new StringList(“you”, L);
4 L = new StringList(“sometimes”, L);
5 StringList M = L.rest;
6 StringList R = new StringList(“many”, null);
7 R = new StringList(“potatoes”, R);
8 R.rest.rest = R;
9 M.rest.rest.rest = R.rest;
10 L.rest.rest = L.rest.rest.rest;
11 L = M.rest;
For a step by step walkthrough of this box and pointer diagram, see https://tinyurl.com/y38jkzpj
Scope, Static, and Linked Lists 3
4 Scope, Static, and Linked Lists 3 Helping Hand Extra
(a) Fill in blanks in the methods findFirst and findFirstHelper below such that they return the first IntNode with item n, or null if there is no such node containing that item.
public class SLList { Node sentinel;
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
(b)
private static class Node { int item;
Node next; }
…
public int findFirst(int n) {
return findFirstHelper(n, 0, sentinel.next);
}
private int findFirstHelper(int n, int index, Node curr) { if (curr == null) {
return -1; }
if (curr.item == n) { return index;
} else {
return findFirstHelper(n, index + 1, curr.next);
} }
}
Why do we use a helper method here? Why can¡¯t we just have the signature for findFirst take in the start index and curr, such that the user of the function passes in 0 and sentinel.next each time?
It¡¯s not intuitive for the user to have to pass in 0 and sentinel.next every single time they¡¯re calling findFirst(), as it is unrelated to what they¡¯re actually requesting. Additionally, it is breaking the abstraction barrier, as it requires our user to understand how this method works under the hood. Finally, if the user didn¡¯t understand what to pass in (because again, it¡¯s not quite intuitive), they could pass in some random values that will result in an incorrect answer.
Thus, it is bad programming practice to make the user pass in those extra arguments every time. However, we do need a way of keeping track of which node and index we¡¯re on as we recurse, so we must make a helper method that can keep track of all that information.
public SLList() { this.sentinel = new Node();