CS241: Spring 2015 -- Lecture 6

  1. Quiz feedback: on models and static thinking
  2. Finishing up Life with animation information
    1. Initializing the Board with a glider:
      public class Board {
          int size;
          Cell[][] cells;
          
          Board(int n) {
              size = n;
              cells = new Cell[size][size];
              
              for (int row=0; row<size; row++) {
                  for (int col=0; col<size; col++) {
                      cells[row][col] = new Cell();  // create each Cell
                  }
              }
              
              cells[5][4].setAlive(true);  // put a glider in the middle
              cells[5][5].setAlive(true);
              cells[5][6].setAlive(true);
              cells[6][6].setAlive(true);
              cells[7][5].setAlive(true);
          }
    2. Example: magic squares
      1. mousePressed
      2. highlighting a square
    3. Animating
      1. Using a JPanel with public void paintComponent(Graphics) inside a JFrame (with a BorderLayout to have the Panel fill the whole drawable area, or setSize(int,int) for just part) to get double buffering by default
      2. Hijacking the psvm Thread to do animation
      3. random walk, or whatever interesting behavior you'd like...
  3. Two new data structures: stack and queue
  4. Expression evaluation
    1. Introduction to expressions: prefix, infix, and postfix
    2. Evaluating arbitrary infix expressions; not so easy!!
    3. postfix evaluation
      while (more input) {
         next = nextToken()
         if (next is operand)
            push(next)
         else apply next to top two operands and push the result
      }
      answer is on top of the stack