CS241: Spring 2017 -- Theta, bubble sort, and Fibonacci

  1. Quiz - 5 minutes...
  2. A difference between theory and practice: can something be trivial theoretically, yet impossible practically?
  3. You can trade space for time (and vice-versa). E.g. calculating Fibonacci numbers.
  4. Running until the state is constant, or alternating
  5. Displaying Conway's Life graphically.
  6. Reading files (and parsing them).
    1. A wrapper class for BufferedReader, MyReader
      1. Reading from a particular file (instead of making the user chose one each run!). NetBeans considers the "current directory", aka "." to be the directory where the project file is. Thus, if you wish to read from the directory where the source code is, you must append "src/packageName/" before the name of the file you wish to read. Alternatively, you could put the input file in the project directory!
      2. String[] split()
      3. one character at a time
      4. StringTokenizer
      5. Scanner
    2. Pseudocode for reading a file
                          open the file, using MyReader mr
                          while (mr has more data) {
                              input = mr.giveMeTheNextLine();
                              for each char in input {
                                  set the state of the cell in the next column of the current row (!? current row??) to...
                                  if '-'
                                      dead
                                  else alive
                              }
                          }
                          
    3. Given a line of text (composed of '-'s and '*'s), how to store the proper values in the array?
  7. Java animation
    1. Set layout of Frame to Border
    2. Add a new MyJPanel(this), then when you sent the Frame repaint(), paintComponent(Graphics) will be sent to the Panel
    3. Make the Panel visible; set its layout to null
    4. Write public void paintComponent(Graphics) in the Panel
                      public void paintComponent(Graphics g) {
                          super.paintComponent(g);
                          myLife.paint(g);
                      }
    5. Add an infinite loop which will control the looping and do the animation
          void loop() {
              for (;;) { // forever!
                  if (running)
                      step();
                  delay();
              }
          }
      
          private void step() {
              whateverIsStepping.step();
              display();
          }
      
          private void delay() {
              try {
                  Thread.sleep(50);  // sleep for 50 msecs
              } catch (Exception e) {}
          }
      
                          
    6. Start that loop from the Main Thread (usually as the last line of the Frame constructor)
    7. Add a Button in the Panel to start/stop the looping