0% found this document useful (0 votes)
15 views4 pages

03 Disc

The document discusses concepts related to scope, static variables, and linked lists in Java programming, using examples such as a Pokemon class and a StringList. It includes exercises to analyze code output, understand variable scope, and implement methods for linked list operations. Additionally, it explores the use of helper methods for searching within linked lists.

Uploaded by

18124677831li
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

03 Disc

The document discusses concepts related to scope, static variables, and linked lists in Java programming, using examples such as a Pokemon class and a StringList. It includes exercises to analyze code output, understand variable scope, and implement methods for linked list operations. Additionally, it explores the use of helper methods for searching within linked lists.

Uploaded by

18124677831li
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CS 61B Scope, Static, and Linked Lists

Spring 2021 Discussion 3: February 01, 2021

1 Static Electricity
1 public class Pokemon {
2 public String name;
3 public int level;
4 public static String trainer = "Ash";
5 public static int partySize = 0;
6

7 public Pokemon(String name, int level) {


8 this.name = name;
9 this.level = level;
10 this.partySize += 1;
11 }
12

13 public static void main(String[] args) {


14 Pokemon p = new Pokemon("Pikachu", 17);
15 Pokemon j = new Pokemon("Jolteon", 99);
16 System.out.println("Party size: " + Pokemon.partySize);
17 p.printStats()
18 int level = 18;
19 Pokemon.change(p, level);
20 p.printStats()
21 Pokemon.trainer = "Ash";
22 j.trainer = "Brock";
23 p.printStats();
24 }
25

26 public static void change(Pokemon poke, int level) {


27 poke.level = level;
28 level = 50;
29 poke = new Pokemon("Voltorb", 1);
30 poke.trainer = "Team Rocket";
31 }
32

33 public void printStats() {


34 System.out.print(name + " " + level + " " + trainer);
35 }
36

37 }
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?
Scope, Static, and Linked Lists 3

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;
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.

1 public class SLList {


2 Node sentinel;
3

4 public SLList() {
5 this.sentinel = new Node();
6 }
7

8 private static class Node {


9 int item;
10 Node next;
11 }
12

13 public int findFirst(int n) {


14 return ________________________________________;
15 }
16

17 private int findFirstHelper(int n, int index, Node curr) {


18 if (____________________) {
19 return -1;
20 }
21 if (____________________) {
22 return index;
23 } else {
24 return ________________________________________;
25 }
26 }
27

28 }

(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?

You might also like