CS51 - Spring 2010 - Lecture 15

http://xkcd.com/645/

  • admin
       - good job on the last quiz (keep looking at the problems)
       - TP1 back soon...
       - CS colloquium (http://www.cs.pomona.edu/colloquium.html)

  • show FallingObjectsWithCows demo
       - how did we do this?
          - FallingObject class
             - Cow, Cecil, FallingLeaf, Hail and Tomato all inherit from this class
          - how do these 5 classes differ?
             - constructor
             - what happens when Drawable2DInterface object reaches the bottom of the screen

  • look at FallingObjectsWithCows code
       - look at the FallingObject class
          - we've made an explicit method called hitBottom that simply removes the object from the canvas
          - how does this help us?
             - classes that extend this class can override that method
          - why is it protected?
             - if we made it public then other classes could call it whenever they wanted
             - if we made it private, then we couldn't override it when we inherited from that class
       - what will the Cecil and Cow classes look like?
          - like the others, in the constructor, create a new VisibleImage, etc.
          - will only have to override the hitBottom method to change the functionality!
          - don't need to override the run method

  • draw the hierarchy tree for these classes

  • classes can only extend one class. Why?
       - avoid ambiguity
          - what if you inherited from two classes that both defined the same method? Which one would be called?

  • inheritance and types
       - which assignment statements do you think are legal?
          Cow c;
          FallingObject f;

          c = new FallingObject(...);
          f = new Cow(...);

          c = f;
          f = c;

       - you can use a subclass anywhere you could use the superclass, but NOT vice versa, why?
          - In the example above, we know that the cow has at least all of the functionality of a FallingObject, so anywhere we could use a FallingObject, we would use a Cow. The reverse is not true.
       - must be able to be figure it out from ONLY looking at that exact statement (i.e. not the history)
          FallingObject f = new Cow(...);

          Cow c = f; // would not be a valid statement!

          - if you know it's true, you can typecast it:
             Cow c = (Cow)f; // now it's valid

       - the "instanceof" command allows you to ask the boolean question of whether an object is of the specified type
          FallingObject f = new Cow(...);
          FallingObject f2 = new FallingObject(...);

          System.out.println(f instanceof Cow);
             - true
          System.out.println(f instanceof FallingObject);
             - true
          System.out.println(f2 instanceof Cow);
             - false
          System.out.println(f2 instanceof FallingObject);
             - true

  • 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?

  • 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 many objects of the same type, 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, represents a set of indexable objects
             - a[index] is just an object! (of type int)
             - They are indexed starting at 0, so 0 is the first element, 1 the second, etc
                - we're computer scientists, so we start counting at 0
             - 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(...);