CS51 - Spring 2010 - Lecture 16

  • exercise 17.5.1
       public class A {...}
       public class B extends D {...}
       public class C extends A {...}
       public class D extends A {...}
       public class E extends D {...}
       
       private A someA;
       private B someB;
       private C someC;
       private D someD;
       private E someE;

       - draw the inheritance hierarchy
       - which assignments will cause problems?
          - someD = someB
          - someE = someA
          - someA = someE
          - someD = someC;
       - will the following always, sometimes or never fail:
          someA = new D(...);
          someB = (B)someA;

          someA = new B(...);
          someD = (D)someA;

          someA = new C(...);
          someD = (D)someA;

          someA = someD;
          someB = (B)someA;

          someA = someD;
          someC = (C)someA;

  • Object class
       - all java classes inherit from the Object class
          - if you create a new class and you don't have an extend statement, it implicitly extends from Object
       - from the Object class, each class inherits
          - equals method
             - though the default just checks using ==, so you should override it in your class if you want it to do the correct thing
          - toString method
             - which allows you to say:
                System.out.println(object);
       - An Object variable can hold any object:
          Object o = new FramedRect(...);
          o = new Tomato(...);
          o = new Ball(...);
       - In general, though, this is discouraged unless necessary. Why?

  • while loops
       - while(<condition>){
          // do the following
       }
       - as long as <condition> is true, continue in the loop

  • Take a look at our FallingLeaves demo
       - what pattern did we use to generate 10 leaves in Tree.java?
       - let's look at the construction of the while loop in Tree.java
          - What role does the treeCount variable play?
          - initialize a variable for counting
          - while loop
             - conditioned on the counter
             - inside the while loop we increment the count

          <initial_statement>
          while( <condition> ){
             // do the following
             <update_statement>
          }

  • for loop
       - doesn't add any functionality to the language
       - makes our life easier
       - makes code easier to understand for certain types of loops
       - for( <initial_statement>; <condition>; <update_statement>){
          // do the following
       }
       - When we first execute the for loop, execute <initial_statement>
          - often used to initialize a counter variable
          - only happens once
       - As long as <condition> is true, continue in the loop
       - At the end of each iteration in the loop, execute <update_statement>
          - often used to increment a counter variable
          - happens EVERY iteration

  • How can we change our Tree class to use a for loop?

  • Show Knitting demo again

  • Show Knitting code
       - change this code to use 'for' loops

  • Fibonacci numbers
       - 1, 1, 2, 3, 5, 8, 13, ...
          - first Fibonacci number is 1
          - second is 1
          - thirst is 2
          - etc.
       - what's the pattern?
          - the current number is equal to the sum of the previous two numbers
       - let's write a recursive method to give us the nth fibonacci number
          public int fib(int n)

          - what is the base case?
             - if n is less than or equal to 2
                - return 1
          - what is the recursive case
             fib(n-1) + fib(n-1)

          public int fib(int n){
             if( n <= 2 ){
                return 1;
             }else{
                return fib(n-1) + fib(n-2);
             }
          }

       - How efficient is Fibonacci?
          - look at the call graph for fib

  • Fibonacci without recursion
       - Do fib(10) on paper...
       - How did you do it?
       - Started at 1, 1, then wrote the numbers out
       - We'd like to write a function that does it this way
       - What do we need?
          - We need some way of keeping track of the intermediary numbers
       - How many numbers do we have?
          - n, if we include the final number
       - Arrays allow us to keep track of a sequence of things, using just one variable
       - Arrays are just like other variables:
          - They have a declaration
             - <type_of_thing>[] <variable_name>;
             - int[] a;
             - double[] b;
             - FramedRect[] c;
          - We can create new arrays: we must specify the length!
             - a = new int[10];
             - b = new FramedRect[1000];
          - Array variables are like any other variables, b's type is "FramedRect[]" for example, that is a FramedRect array
          - An array, contains a set of indexable variables
             - a[index] is just another variable! (of type int)
             - They are indexed starting at 0, so 0 is the first element, 1 the second, etc
             - We can assign to individual elements of arrays
                - a[0] = 50;
                - b[30] = new FramedRect(...);
             - We can access elements in an array
                - int val = a[1] + a[2];
                - b[30].move(...);
       - Back to Fibinacci... How do arrays help us?

       private int fib(int n){
          int[] fibArray = new int[n];
          
          fibArray[0] = 1;
          fibArray[1] = 1;
          
          for( int i = 2; i < n; i++ ){
             fibArray[i] = fibArray[i-1] + fibArray[i-2];
          }
          
          return fibArray[n-1];
       }

  • Write a function that given an array of ints, returns the largest number in the array
       - what does the function definition look like?
       
       public int largestNumber(int[] numbers){
          int largest = numbers[0];

          for( int i = 0; i < numbers.length; i++ ){
             if( numbers[i] > largest ){
                largest = numbers[i];
             }
          }
       }

       - for loops are very natural for traversing arrays
       - note that length is a public variable not a member function

  • Show SlidesF09 demo: how can we do this? Hint: use arrays!

  • Look through SlidesF09 code