CS计算机代考程序代写 CS 61B Spring 2021

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.
(b) On line 28, we set level equal to 50. What level do we mean? An instance variable of the Pokemon class? The local variable containing the parameter to the change method? The local variable in the main method? Something else?
(c) If we were to call Pokemon.printStats() at the end of our main method, what would happen?

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;
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 index of the first Node with item n, or -1 if there is no such node containing that item.
8
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
private static class Node { int item;
Node next; }
public int findFirst(int n) {
return ________________________________________;
}
private int findFirstHelper(int n, int index, Node curr) { if (____________________) {
return -1; }
if (____________________) { return index;
}else{
return ________________________________________;
} }
}
public class SLList { Node sentinel;
1
2
3
4
5 6} 7
public SLList() { this.sentinel = new Node();
(b) Why do we use a helper method here? Why can¡¯t we just have the signature for findFirst also have a pointer to the curr node, such that the user of the function passes in the sentinel each time?