Explicit types
variable = "Hello!"
String variable = "Hello!";
Braces
if condition:
do_something()
elif condition2:
do_something_2()
else:
do_something_else()
if (condition) {
do_something();
} else if (condition2) {
do_something2();
} else {
do_something_else();
}
/** Print all primes up to and including LIMIT. */
private static void printPrimes(int limit) {
for (int p = 2; p < = limit; p += 1) {
if (isPrime(p)) {
System.out.print(p + " ");
}
}
System.out.println();
}
public class CS61BStudent {
public int idNumber;
public int grade;
public static String professor = "Hilfinger";
public CS61BStudent(int id) {
this.idNumber = id;
this.grade = 100;
}
public void watchLecture() {
// ...
}
public static String gradeToLetter(int grade) {
// ...
}
}
public class CS61BLauncher {
public static void main(String[] args) {
CS61BStudent studentOne;
studentOne = new CS61BStudent(32259);
CS61BStudent studentTwo = new CS61BStudent(19234);
studentOne.watchLecture();
System.out.println(CS61BStudent.gradeToLetter(85));
// System.out.println(studentTwo.gradeToLetter(70));
}
}
What does this output?
int x = 7;
String chorus = "Thank u, next";
Singer queen = new Singer("Ariana");
while (x > 0) {
x -= 1;
queen.sing(chorus); // Returns a String, prints nothing
}
String[] phrases = {"love", "patience", "pain", "what does the fox say?"};
for (int i = 0; i < 3; i += 1) {
System.out.println("One taught me " + phrases[i]);
}
System.out.println(phrases[phrases.length - 1]);
public static int mystery1(int[] inputArray, int k) {
int x = inputArray[k];
int answer = k;
int index = k + 1;
while (index < inputArray.length) {
if (inputArray[index] < x) {
x = inputArray[index];
answer = index;
}
index = index + 1;
}
return answer;
}
When this function is called with arguments:
inputArray = [3, 0, 4, 6, 3]k = 2what does it return? Describe the method in English.
public static void mystery2(int[] inputArray) {
int index = 0;
while (index < inputArray.length) {
int targetIndex = mystery1(inputArray, index);
int temp = inputArray[targetIndex];
inputArray[targetIndex] = inputArray[index];
inputArray[index] = temp;
index = index + 1;
}
}
When this function is called with arguments:
inputArray = [3, 0, 4, 6, 3]
and mystery1 as defined before, what does it do? Describe the method in
English.
Implement a function fib1 that recursively computes the N-th Fibonacci
number. As a reminder, the definition of the N-th Fibonacci number is:
fib(0) = 0
fib(1) = 1
fib(N) = fib(N - 1) + fib(N - 2)
public static int fib1(int N) {
}
Implement a function fib2 that recursively computes the N-th Fibonacci
number without redundant computation. To compute the N-th Fibonacci number, we would
call
fib2(N, /*k=*/0, /*f0=*/0, /*f1=*/1)
Hint: Try writing the iterative version of fib1 first.
public static int fib1(int N, int k, int f0, int f1) {
}