CS51 - Fall 2009 - Lecture 26

  • Book problem 14.9.3

  • Debugging functionality
       - set breakpoints
       - step into (F5)
       - step over (F6)
       - step return (F7)
       - continue

  • java.util.ArrayList
       - get(index) // returns value at index
       - set(index, value) // sets value at index to be value
       - add(value) // adds value to the end of the array

  • Quick review of scope
       - What is the scope of a variable?
          - the section of code that variable can be referred to by that name
       - What scoping options do you have?
          - public member variable
          - private member variable
          - local variable (and varying nestings, for example within for and while loops)
       - When designing a program, how do you decide what scope is appropriate for a variable?
          - you should pick the smallest scope possible that accomplishes your task

  • 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?
             - 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?
             - 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;
             }
       - Using a multidimensional array, how can we keep track of the board?
          - private int NUM_ROWS = 3;
          - private int NUM_COLS = 3;
          - 3 by 3 array of integers: int[][] marks = new int[NUM_ROWS][NUM_COLS];
             - private int EMPTY_MARK = 0;
             - private int X_MARK = 1;
             - private int O_MARK = 2;
       - What are the questions we'll want to ask about the board?
       - How do we check for a win? (group work?)
          - need to check four cases:
             - rows all the same
             - columns all the same
             - top-left to bottom-right diagonal all the same
             - top-right to bottom-left diagonal all the same

  • Dubrovnik reconstrution