Discussion 2: CS 61B Spring 2020
Discussion 2: CS 61B Spring 2020
1 int x = 7;
2 String chorus = "Thank u, next";
3 Singer queen = new Singer("Ariana");
4
5 while (x > 0) {
6 x -= 1;
7 queen.sing(chorus);
8 }
9
10 String[] phrases = {"love", "patience", "pain", "what does the fox say?"};
11
12 for (int i = 0; i < 3; i += 1) {
13 System.out.println("One taught me " + phrases[i]);
14 }
15
16 System.out.println(phrases[phrases.length - 1]);
Write the return value of mystery1 if inputArray is the array {3, 0, 4, 6, 3} and k is 2. What
is the significance of the value returned by mystery1 (what is the significance of answer)?
Extra: Below is another function called mystery2. It takes a single array of integers called
inputArray as an argument and returns nothing.
1 public static void mystery2(int[] inputArray) {
2 int index = 0;
3 while (index < inputArray.length) {
4 int targetIndex = mystery1(inputArray, index);
5 int temp = inputArray[targetIndex];
6 inputArray[targetIndex] = inputArray[index];
7 inputArray[index] = temp;
8 index = index + 1;
9 }
10 }
Describe what mystery2 will do and return if inputArray is the array {3, 0, 4, 6, 3}. Then,
explain in English what the method mystery2 does.
Extra: Implement fib2 in 5 lines or fewer that avoids redundant computation. fib2 takes in an
integer N and helper arguments k, f0, and f1 and returns an integer representing the Nth Fibonacci
number. If you’re stuck, try implementing fib1 iteratively and then see how you can transform
your iterative approach to implement fib2.
public static int fib2(int N, int k, int f0, int f1) {