Discussion 4

Objects

Announcements

  1. Project 0 due Friday, 2/11
  2. No lab, converted to Proj 0 OH! See lab spec.
  3. Optional Test 1 accommodations form due Friday, 2/11
  4. Exam review sessions (Piazza):
    • CS 61B Tutors, Friday 2-4
    • CSM, Saturday 10-12
    • HKN, Saturday 2-4
  5. If you want to, you can attend multiple discussion / lab sections!

Review: Static vs Instance

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

Static variables and functions belong to the class as a whole. The professor is a shared characteristic of all CS61BStudents.

Instance variables and functions belong to a particular instance. Each CS61BStudent has its own independent idNumber.

Preview: OOP Inheritance

Course slides (mine will be done next week)

Worksheet

1a. Avatar

            
              public class Avatar {
                public static String electricity;
                public String fluid;
            
                public Avatar(String str1, String str2) {
                  Avatar.electricity = str1;
                  this.fluid = str2; 
                }
            
                public static void main(String[] args) {
                  Avatar foo1 = new Avatar(“one”, “two”);
                  Avatar foo2 = new Avatar(“three”, “four”);
                  System.out.println(foo1.electricity + foo1.fluid);
                  foo1.electricity = “I declare ”;
                  foo1.fluid = “a thumb war”;
                  System.out.println(foo2.electricity + foo2.fluid);
                }
            }
            
          

What's printed after executing main?

1b. Avatar

          
            public class Avatar {
              public static String electricity;
              public static String fluid;
          
              public Avatar(String str1, String str2) {
                Avatar.electricity = str1;
                this.fluid = str2; 
              }
          
              public static void main(String[] args) {
                Avatar foo1 = new Avatar(“one”, “two”);
                Avatar foo2 = new Avatar(“three”, “four”);
                System.out.println(foo1.electricity + foo1.fluid);
                foo1.electricity = “I declare ”;
                foo1.fluid = “a thumb war”;
                System.out.println(foo2.electricity + foo2.fluid);
              }
          }
          
        

Does this code compile after changing lines 2 and 3?

1c. Avatar

          
            public class Avatar {
              public static String electricity;
              public String fluid;
          
              // New method!
              public static String getFluid() {
                return fluid;
              }
          
              // ...
          }
          
        

After adding this method, does the code compile?

2. Static Shock (extra)

            
              public class Shock {
                public static int bang;
                public static Shock baby;
                public Shock() { this.bang = 100; }
                public Shock(int num) {
                    this.bang = num;
                    baby = starter();
                    this.bang += num;
                }
                public static Shock starter() {
                    Shock gear = new Shock();
                    return gear;
                }
                public static void shrink(Shock statik) {
                  statik.bang -= 1;
                }
                public static void main(String[] args) {
                    Shock gear = new Shock(200);
                    System.out.println(gear.bang);    
                    shrink(gear);
                    shrink(starter());
                    System.out.println(gear.bang);    
                }
            }            
            
          

What's printed after executing main?

3. Reversing an Array

            
              /** Destructively reverses A in place. */
              public static void reverse(int[] A) {






              }
            
          

Hint: element 0 swaps with element N-1, element 1 swaps with element N-2...

3. Reversing a Diagonal (extra)

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

1 2 9 4
5 6 7 8
3 10 11 12
13 14 15 16
            
              /** Destructively reverses the items along the diagonal in B */
              public static void reverseDiagonal(int[][] B, int diagonal) {






              }
            
          

Hint: diagonal 2 is a sequence of elements at indices [2][0], [1][1], [0][2].

4. Circular Buffer

2 3 4 5 6 7 0 1
6


0

0 1 2 3 4 5 6 7 8
0
            
              public static int[] overflow(int[] A, int i, int k) {









              }
            
          

Use System.arraycopy to copy chunks of the input array!

5. Transposing a 2D Array (extra)

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
            
              public static int[] overflow(int[] A, int i, int k) {









              }