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
public static void main (String[] args) { Foo foobar = new Foo(10, 20);
Foo baz = new Foo(30, 40); switcheroo(foobar, baz); fliperoo(foobar, baz); swaperoo(foobar, baz);
CS 61B Scope, Pass-by-Value, Static Spring 2021 Exam Prep Discussion 2: January 25, 2021
1 Give em the ¡¯Ol Switcheroo
For each function call in the main method, write out the x and y values of both foobar and baz after executing that line. (Spring ¡¯15, MT1)
public class Foo { public int x, y;
public Foo (int x, int y) { this.x = x;
this.y = y;
public static void switcheroo (Foo a, Foo b) { Foo temp = a;
a = b;
b = temp; }
public static void fliperoo (Foo a, Foo b) { Foo temp = new Foo(a.x, a.y);
a.x = b.x;
a.y = b.y;
b.x = temp.x;
b.y = temp.y;
}
public static void swaperoo (Foo a, Foo b) { Foo temp = a;
a.x = b.x;
a.y = b.y;
b.x = temp.x;
b.y = temp.y;
}
1
2
3
4
5
6 7}
} }
foobar.x: ___ foobar.y: ___ baz.x: ___ baz.y: ___
foobar.x: ___ foobar.y: ___ baz.x: ___ baz.y: ___
foobar.x: ___ foobar.y: ___ baz.x: ___ baz.y: ___
2 Scope, Pass-by-Value, Static Solution:
line 34: foobar.x: 10 foobar.y: 20 baz.x: 30 baz.y: 40
line 35: foobar.x: 30 foobar.y: 40 baz.x: 10 baz.y: 20
line 36: foobar.x: 10 foobar.y: 20 baz.x: 10 baz.y: 20
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 38
public static void int[] B = A;
2 Quik Maths
What would the contents of the array be after being run through these functions in the main method? (Fall ¡¯16, MT1)
public class QuikMaths public static void for (int x: A) x=x*3;
{
mulitplyBy3(int[] A) { {
multiplyBy2(int[] A) {
1
2
3
4 5} 6}
} }
for (int i = 0; i < B.length; i+= 1) { B[i] *= 2;
} }
public static void int temp = B;
B = A;
A = temp; }
public static void int[] arr;
swap(int A, int B ) {
main(String[] args) {
Solution:
arr = new int[]{2, 3, 3, 4}; multiplyBy3(arr);
/* Value of arr: {________________________} */
arr = new int[]{2, 3, 3, 4}; multiplyBy2(arr);
/* Value of
inta=6; intb=7; swap(a, b);
/* Value of
arr: {________________________} */
a: _______ Value of b: _______ */
line 26: /* Value of arr: {2, 3, 3, 4} */ line 31: /* Value of arr: {4, 6, 6, 8} */ line37:/*Valueofa:6 Valueofb:7*/
Scope, Pass-by-Value, Static 3
4 Scope, Pass-by-Value, Static
3 Static Books
Suppose we have the following Book and Library classes.
class Book {
public String title;
public Library library;
public static Book last = null;
class Library {
public Book[] books;
public int index;
public static int totalBooks = 0;
public Library(int size) { books = new Book[size]; index = 0;
}
public void addBook(Book book) { books[index] = book; index++;
totalBooks++;
book.library = this; }
public Book(String name) { title = name;
last = this; library = null;
}
public static String lastBookTitle() { return last.title;
}
public String getTitle() {
return title; }}
}
(a) For each modification below, determine whether the code of the Library and Book classes will compile or error if we only made that modification, i.e. treat each modification independently.
1. Change the totalBooks variable to non static
2. Change the lastBookTitle method to non static 3. Change the addBook method to static
4. Change the last variable to non static
5. Change the library variable to static
Solution:
1. Compile 2. Compile 3. Error
4. Error
5. Compile
(b) Using the Book and Library classes from before, write the output of the main method below. If a line errors, put the precise reason it errors and continue execution.
1 public class Main {
2 public static void main(String[] args) {
3 System.out.println(Library.totalBooks);
4 System.out.println(Book.lastBookTitle());
5 System.out.println(Book.getTitle());
6
7 Book goneGirl = new Book("Gone Girl");
8 Book fightClub = new Book("Fight Club");
9
10 System.out.println(goneGirl.title);
11 System.out.println(Book.lastBookTitle());
12 System.out.println(fightClub.lastBookTitle());
13 System.out.println(goneGirl.last.title);
14
15 Library libraryA = new Library(1);
16 Library libraryB = new Library(2);
17 libraryA.addBook(goneGirl);
18
19 System.out.println(libraryA.index);
20 System.out.println(libraryA.totalBooks);
21
22 libraryA.totalBooks = 0;
23 libraryB.addBook(fightClub);
24 libraryB.addBook(goneGirl);
25
26 System.out.println(libraryB.index);
27 System.out.println(Library.totalBooks);
28 System.out.println(goneGirl.library.books[0].title);
29 }
30 }
Solution:
1 public class Main {
2 public static void main(String[] args) {
3 System.out.println(Library.totalBooks);
4 System.out.println(Book.lastBookTitle());
5 System.out.println(Book.getTitle());
6
7 Book goneGirl = new Book("Gone Girl");
8 Book fightClub = new Book("Fight Club");
9
10 System.out.println(goneGirl.title);
11 System.out.println(Book.lastBookTitle());
_____________________
_____________________
_____________________
_____________________
_____________________
_____________________
_____________________
_____________________
_____________________
_____________________
_____________________
_____________________
0
Error, NullPointerException
Error, does not compile
Gone Girl
Fight Club
Scope, Pass-by-Value, Static 5
6 Scope, Pass-by-Value, Static
12 System.out.println(fightClub.lastBookTitle());
13 System.out.println(goneGirl.last.title);
14
15 Library libraryA = new Library(1);
16 Library libraryB = new Library(2);
17 libraryA.addBook(goneGirl);
18
19 System.out.println(libraryA.index);
20 System.out.println(libraryA.totalBooks);
21
22 libraryA.totalBooks = 0;
23 libraryB.addBook(fightClub);
24 libraryB.addBook(goneGirl);
25
26 System.out.println(libraryB.index);
27 System.out.println(Library.totalBooks);
28 System.out.println(goneGirl.library.books[0].title);
29 }
30 }
Fight Club
Fight Club
1 1
2
2
Fight Club