CS51 - Spring 2010 - Lecture 16

  • http://xkcd.com/557/

  • notes on TP1 style
       - if you find yourself repeating
          - code (even just a few lines): consider using a private method
          - variables: use an array (you didn't know about it yet for TP1)
       - variable scope
          - the only reason you should have a private instance variable is if you use it in more than one method
          - otherwise, make a local variable!
          - I didn't take off points this time, but will next time
       - constants in multiple classes
          - NEVER define the same constant in multiple classes
             - define it in the one where it makes the most sense, and pass as a parameter to others
             - very, very rarely, make it a public constant (come talk to me before you do this :)
       - think about while loops

  • CS lunch tomorrow

  • 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];
       }


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

  • Look through SlidesF09 code

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

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

       public int largestNumber(int[] numbers){
          int largest = numbers[0];

          for( int number: numbers ){
             if( number > largest ){
                largest = number;
             }
          }
       }

  • show TextStat demo
       - String member method: split
          - public String[] split(String splitString);
       - We're going to write the four supporting methods to make this class
          - private String getLongestWord(String[] words)
          - private String getShortestWord(String[] words)
          - private int getNumberOfWords(String[] words)
          - private int getNumberOfCharacters(String[] words)

  • Show DrawingPanelWithShapeArray demo
       - Note how we can move things around and different objects are "in front" of other objects
       - Hard to get this behavior with recursion
       - We'll do this using arrays:
          - we'll construct an array with some maximum size, call it MAX_OBJECTS
             - private DrawableInterface[] shapes = new DrawableInterface[MAX_OBJECTS];
          - initially, the array will be empty
          - as we add things, add the objects to the array
          - how can we do this?
             - keep an integer (int numShapes) that tells us how many things are in the array so far   
       - How do we add a new item?
          - construct the shape
             - figure out the shape from the menu and create a new shape
             - figure out the color from the menu and set color
          - how do we add it to the shapes array?
             - remember, we're using numShapes to keep track of how many items are already in the array
             - set shapes[numShapes] to the new object
             - increment numShapes

  • Other misc. array things
       - what happens here?
          int[] numbers = new array[10];
          int currentIndex = 0;
          ...
          currentIndex = 10;
          numbers[currentIndex]++;
          
          ** ArrayOutOfBounds exception
       - what happens here?
          FramedRect[] rects = new FramedRect[10];
          rect[0].move(10, 10);

          ** null pointer exception
          - we've just created an array of FramedRect variables
          - as like any other FramedRect variable, their initial value is null until you assign something to them