CS241: Spring 2015 -- Lecture 3

  1. Parameter Linkage and types of variables on p13...
  2. Every non-Singleton class you write should have (well... almost every!).
    1. Accessors (to access the instance variables) - for encapsulation
    2. public String toString() - for debugging
    3. An initializing constructor - for ease of use
  3. The Class maker app, which is not so easy to run due to "security enhancements". Fortunately, NetBeans does mostly the same thing (right-click/Insert code (or ^i)). Example: Exam, to keep track of student exam scores. String name;, and int score.
  4. Recursive int[] seed(int), with a new (?) reserved word, final
        ...
        final int[] baseCase = {1,2};           // only 2 players, 1 plays 2
        ...
    
        int [] seed(int n) {  // this assumes n is a power of 2 >= 2
            if (n == 2) {  // base case?
                return baseCase;
            return expand(seed(n/2));  // if not, recurse with n/2
        }
                
        int[] expand(int[] small) {     // fill in their opponents to double the size
            int[] returnMe = new int[small.length*2];  // twice as big
            ...
            return returnMe;
        }
        ...
        display(seed(8));   // test it on n=8
           
                
  5. 2-dimensional arrays
    1. declaration
      int[][] sq = new int[N][N];
                          
    2. iterating over
              for (int row = 0; row < N; row++) {
                  for (int col = 0; col < N; col++) {
                      ...sq[row][col]...
                  }//inner for
              }//outer for
    3. display
      1. to output -- making it line up
      2. to a TextArea -- making it line up
    4. Example: magic squares (with clicks to highlight/switch rows/cols)