Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS61B: Data Structures

Midterm #1, Spring 2019

1. Static Swap.

a) (12 points) Consider the class shown below. On each line with a swap method, fill in the boxes for every variable that is changed by that swap method call. If a swap method causes no        change, fill in none” for that line. No syntax errors or runtime errors occur.

public class StaticSwap {

public static int staticX = 5;

public static int staticY = 10;

public static void swap(int x, int y) {

int temp = x; x = y; y = temp;

}

public static void staticSwap(int x, int y) {

int temp = staticX; staticX = staticY; staticY = temp;

}

public static void main(String[] args) {

int a = 5;

int b = 10;

swap(a, b);

a

b

staticX

staticY

none

swap(staticX, staticY);

a

b

staticX

staticY

none

staticSwap(a, b);

a

b

staticX

staticY

none

staticSwap(staticX,staticY);

a

b

staticX

staticY

none

}

}

b) (12 points) Now imagine that every variable was of type String instead of type int. No syntax errors or runtime errors occur. Assume swap and staticSwap have been redefined to take Strings.

public static String staticX = “goose”;

public static String staticY = “hare”;

public static void main(String[] args) {

String a = “moose”;

String b = “bear”;

swap(a, b);

a

b

staticX

staticY

none

swap(staticX, staticY);

a

b

staticX

staticY

none

staticSwap(a, b);

a

b

staticX

staticY

none

staticSwap(staticX,staticY);

a

b

staticX

staticY

none


2. JUnit (24 points). The Object class in Java defines the equals method as shown below. public boolean equals(Object obj) { return (this == obj); }

The org.junit.Assert.assertEquals method looks like:

public static void assertEquals(Object expected, Object actual) {

if (!expected.equals(actual)) { recordFailure(expected, actual); } }

Suppose we define a class called Dog as follows:

1: public class Dog {

2:    private int size; private List<String> favoriteFoods;

3:    public Dog(int s, List<String> f) { size = s; favoriteFoods = f; }

4:    public boolean equals(Dog o) {

5:        if (this.size != o.size) {

6:            return false;

7:        }

8:        if (this.favoriteFoods != o.favoriteFoods) {

9:            return false;

10:        }

11:        return true;

12:    }

13:    public String toString() { ... };

14: }

Suppose we write a test as follows:

@Test

public void testBananaTofu() {

Dog D1 = new Dog(5, List.of("banana", "tofu")); // List.of(...) creates a Dog D2 = new Dog(5, List.of("banana", "tofu")); // java.util.List assertEquals(D1, D2);

}

Due to at least one error in the Dog class, this test fails with: “expected:Dog<5, [banana, tofu]> but was:Dog<5, [banana, tofu]>”. Explain what you’d need to change so that the Dog class is   correct and also passes testBananaTofu. Refer to line numbers where possible. You may not need all lines. Your changes should be written in English, but can include code.

Change 1: _____________________________________________________

Change 2: _____________________________________________________

Change 3: _____________________________________________________

Change 4: _____________________________________________________

Change 5: _______________ ______________________________________


3. PNH (0 points) What famous work begins with this famous line? “In those days, in those far remote days, in those nights, in those faraway nights, in those years, in those far remote years, at that time the  wise one who knew how to speak in elaborate words lived in the land.”

4. a) More JUnit (10 points). Suppose we add a method equalLists to our AList class with the    signature below. This method returns true if the given List61B has all the same items as the current AList in the same order. Your midterm 1 reference sheet might be useful for this problem.

public boolean equalLists(List61B<T> otherList)

Write a JUnit test that verifies that this method correctly returns true when called with an SLList as an argument. Your lists should be of length 3. Assume all JUnit classes needed have been imported.

public void testAListEqualToSLListOFLength3() {

SLList<Integer> sll = new SLList<>();

______________________________________

______________________________________

______________________________________

AList<Integer> all = new AList<>();

______________________________________

______________________________________

______________________________________

______________________________________

}

b) (25 points) Write the method equalLists. It should work for all possible inputs, not just your JUnit test above. Your method must be non-destructive. You may not need all lines.

public class AList { ...

public boolean equalLists(List61B<T> otherList) {

}

5. SLList (40 points). Suppose we have an SLList as defined in lecture, with a single sentinel node at the front. See your midterm 1 reference sheet for the names of the fields.

Fill in the recursive insert method below. You may not need all lines. Do not write more than one statement on each line. You may not use any for or while loops!

Assume this insert method is part of the SLList class.

/** Inserts item into given index. For example, if the list is currently

* [0, 10, 20, 30], and we call insert(25, 3), the list will become

* [0, 10, 20, 25, 30]. If we then call insert(-5, 0), the list will become

* [-5, 0, 10, 20, 25, 30]. */

public void insert(T item, int index) {

if (index < 0 || index > size()) {

throw new IllegalArgumentException();

}

_____________________________________;

_____________________________________;

}

private void insert(T item, int index, _____________) {

if (________________) {

_____________________________________;

_____________________________________;

} else {

_____________________________________;

_____________________________________;

}

}

6. XList. Let’s define a new type of list known as an IntXList that only has an addLast operation.     An IntXList is a hybrid of the SLList and AList ideas. In an XList, the data is stored as an array of IntSLLists, where each IntSLList must have size <= 4. When addLast is called, we always use the first list with available space. IntXLists store integers, i.e. are not generic.

For example, suppose we have created an IntXList and then call addLast(0), addLast(1),   addLast(2), addLast(3), and addLast(4). This would result in the box-and-pointer diagram below:

a) (20 points) Fill in the addLast method for the XList class so that it is correct and has reasonable performance. Assume that the resize method correctly resizes the IntSLList array. If you’re stuck, consider doing part b first.

public class IntXList {

private IntSLList[] items;

private int size;

public IntXList() {

items = new IntSLList[1];

items[0] = new IntSLList();

}

private void resize(int

}

public void addLast(int

numSLLists)

x) {

// after calling resize,

{ // IntSLList[] items will be of

// length numSLLists

if (__________________________________) {

resize(_____________________);

}

items[__________________].____________________

size += 1;

}

}

b) (35 points) Fill in the resize method in the IntXList class. You may not need both loops or all lines.

private void resize(int numSLLists) {

IntSLList[] temp = ______________________;                for (________________;________________;________________) {

__________________________________;

}

for (________________;________________;________________) {

_____________________________________;

}

_________________________________________;

_________________________________________;

}

7) Yeah One of These Problems (28 points). Suppose we define the following two classes:

public class Deity {

public void smite(Object o) { System.out.println("DO"); }

public void smite(Deity o)  { System.out.println("DD"); }

public void smite(Titan o)  { System.out.println("DT"); }

}

public class Titan extends Deity {

public void smite(Object o) { System.out.println("TO"); }

public void smite(Deity o)  { System.out.println("TD"); }

public void smite(Titan o)  { System.out.println("TT"); }

}

For each of the lines below, fill in the bubble for the string that is printed. Or if the line has a compile or runtime error, fill in the runtime error” (RE) or “compile error” (CE) bubble instead.

Titan T = new Titan();

Deity D = new Deity();

Deity Colette = new Titan();

T.smite(Colette);

DO

DD

DT

TO

TD

TT

RE

CE

T.smite((Deity) Colette);

DO

DD

DT

TO

TD

TT

RE

CE

T.smite(D);

DO

DD

DT

TO

TD

TT

RE

CE

T.smite((Object) D);

DO

DD

DT

TO

TD

TT

RE

CE

((Deity) T).smite(D);

DO

DD

DT

TO

TD

TT

RE

CE

T.smite((Titan) D);

DO

DD

DT

TO

TD

TT

RE

CE

((Object