Discussion 2

Introduction to Java

Announcements

  1. Lab 1, Lab 2, and HW 0 due Friday 1/28 (all of these CANNOT be dropped)
  2. HW 1 released, due next Tuesday 2/1
  3. OH starts this week completely online - schedule on course website

Me!

  • Ethan (he / him), 1st year MS CS (CS education / pedagogy)
  • Discussion: 11 AM Wednesday Soda 320 (CS Scholars)
  • Lab: 4 PM Wednesday Soda 275
  • eordentl [AT] berkeley.edu
  • 2nd time teaching CS 61B
  • Puzzles! Crossword, logic, escape rooms, etc.
Ethan's staff picture

From Python to Java

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();
                    }
                  
                

Anatomy of a Function

            
              /** 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();
              }
            
          

Anatomy of a Class

            
              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) {
                  // ...
                }
              }
            
          

Instantiating + Using Classes

            
              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));
                }
              }
            
          

Worksheet

1. Old Town Code

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]);
            
          

2. Reading Code: A Mystery

              
                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 = 2

what does it return? Describe the method in English.

2. Reading Code: A Mystery (Extra)

              
                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.

3. Recursion Practice: Fibonacci

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) {








                }
              
            

3. Recursion Practice: Fibonacci (Extra)

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) {








                }