teaching machines

CS 145 Lecture 9 – Debugging

September 22, 2014 by . Filed under cs145, fall 2014, lectures.

Agenda

What Does This Do 1?

  1. public static String ?(?) {
      return text + " :)";
    }
  2. public static ? ?(String text) {
      int index = text.indexOf('.');
      return text.substring(0, index + 1);
    }
  3. public static ? ?(?) {
      return s.charAt(s.length() - 1);
    }
  4. public static ? ?(?) {
      return g.nextInt(6) + 1 + g.nextInt(6) + 1;
    }
  5. public static ? ?(String foo) {
      Scanner in = new Scanner(foo);
      in.useDelimiter(",");
      System.out.println(in.next() + " begat " + in.next() + " begat " + in.next());
    }

Debugging

Maurice Wilkes said:

As soon as we started programming, we found to our surprise that it wasn’t as easy to get programs right as we had thought. Debugging had to be discovered. It was on one of my journeys between the EDSAC room and the punching equipment that ‘hesitating at the angles of stairs’ the realisation came over me with full force that a good part of the remainder of my life was going to be spent finding errors in my own programs.

What Does This Do 2?

What is the output of the following program? More importantly, what happens in memory as the program executes?

public class Wdtd2 {
  public static void main(String[] args) {
    int n = 5;
    negate(n);
    System.out.println(n);
  }

  private static void negate(int n) {
    n = -n;
  }
}

Code

Lissajous.java

package lecture0922;

import java.awt.AWTException;
import java.awt.Robot;

public class Lissajous {
  public static void main(String[] args) throws AWTException {
    final int RADIUS = 120;
    Robot r = new Robot();
    
    for (int t = 0; t <= 360 * 10; t += 2) {
      double x = 4.0 * Math.sin(Math.toRadians(t));
      double y = Math.sin(2 * Math.toRadians(t));
//      System.out.printf("%d %d%n", (int) (RADIUS * x + 600), (int) (RADIUS * y + 300));
      r.mouseMove((int) (RADIUS * x + 600), (int) (RADIUS * 1.5 * y + 300));
      r.delay(10);
    }
  }
}

Haiku

on magic potions:
I had a problem
Pen and Paper said, “We’ll help!”
We figured it out