CS51 - Spring 2010 - Lecture 17

  • quiz: did you look at the problems?

  • hand back TP1

  • Show DrawingPanelWithShapeArray demo
       - We'll use an array to keep track of the shapes:
          - DrawableInterface[] shapes = new DrawableInterface[MAX_OBJECTS];
          - int numShapes = 0;
          - what does numShapes do?
       - how do we add an item?
          - create it and add it to the shapes array
             - shapes[numShapes] = new ... (depending on settings of the drop down menus)
          - increment numShapes
             - numShapes
       - how do we delete an item?
          - figure out which object we clicked on
          - remove it from the canvas
          - remove it from our shapes array
          - let's look at each of these in more detail
             - how do we find out which object we clicked on?
                // returns the index into shapes of the object that contains point
                // or -1 if none contain that point
                public int getIndexOf(Location point)
             - how do we deal with overlapping items, i.e. what if we click and two items contain the point?
                - where are we adding more recent items?
                   - to the end of the array
                - therefore, items towards the end of the array are more recent
                - look at getIndexOf method in DrawingPanelWithShapeArray code
                   - start from the back not the front
                   - return the first one we find
             - how do we remove something at a particular index from the shapes array?
                - starting at that index, shift everything down
                - set the last thing to null
                - decrement numShapes
                - look at removeEltWithIndex method
       - How do we implement move?
          - two key ideas
             - when we're moving an object, we move it to the front of the canvas/screen
             - we still need to make sure to keep the shapes array appropriately ordered (with things that are closer to the front towards the end of the array)
          - all of the action happens in onMousePress
             - use getIndexOf to get the index of the object clicked on
                - again, this will search from back to front to handle overlapping items
                - code reuse!
             - if the index != -1
                - "select" this object, so that we know which one to move around in onMouseDrag
                - move this object to the front of the screen
                - change the ordering of the shapes array!
                   - we need to move it to the end of the array!
                   - how?
                      - delete it using removeEltWithIndex
                      - add it back on to the end of the array
                      - look at selectShapeAt method
       - How do we implement Recolor? (should be easy now)
          - getIndexOf(point)
          - recolor that objects

  • Show TicTacToe demo
       - We need to keep track of the state of the board
       - How can we do this?
          - 9 entries, could have 9 variables
          - could have a 9 entry array
       - What is the natural way to talk about entries?
          - rows and columns

  • Multidimensional arrays
       - declaration
          - int[][] a2dIntArray;
          - String[][] a2dStringArray;
          - FramedRect[][] a2dRectArray;
       - initialization
          - a2dIntArray = new int[10][100]; // a 10 by 100 array
       - draw picture
          - notice that it's really an array of arrays
       - accessing elements (remember, arrays start at 0)
          - a2dIntArray[0][0] // the first element in the first row
          - a2dIntArray[1][5] // the sixth element in the second row
       - how would we find the sum of all the elements in an integer array, given numRows and numCols?
          public int sum(int[][] a2dIntArray, int numRows, int numColumns){
             int sum = 0;

             for( int row = 0; row < numRows; row++ ){
                for( int col = 0; col < numColumns; col++ ){
                   sum += a2dIntArray[row][col];
                }
             }

             return sum;
          }
       - what if we wanted to traverse by columns and then rows?
       - how would we find the sum of all the elements in an integer array, with just the array specified?
          public int sum(int[][] a2dIntArray){
             int sum = 0;

             for( int i = 0; i < a2dIntArray.length; i++ ){
                for( int j = 0; j < a2dIntArray[i].length; i++ ){
                   sum += a2dIntArray[i][j];
                }
             }

             return sum;
          }

  • Simon lab
       - 5 classes
          - we'll give you full implementations of 2 of these
       - 1 interface
          - we've defined one to listen to the NoisyButton
          - if you're rusty on interfaces and listeners, review the notes from KeyListener
       - the Song class will be used to keep track of what sequence of buttons the user should press, as well as where the user is in the song:
          - how can we represent a song?
          - play the song (this will involve creating a new SongPlayer object)
          - determine the next button the user is expected to click (so you can tell if it's the correct one)
          - add a note to the song (when the user gets the sequence right)
          - others
       - Design!
          - worth 20% of your grade
          - spend some time on this
             - this lab is very straightforward if you think about the design beforehand. If you don't, it can be challenging.