CS 145 Lecture 9 – Debugging
September 22, 2014 by Chris Johnson. Filed under
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378
Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379
Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380
Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381
Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382
Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
cs145, fall 2014, lectures.
Agenda
- what ?s
- methods elsewhere
- what does this do 1
- visualizing memory and methods
- what does this do 2
- call by value
- the debugger
- methods elsewhere
What Does This Do 1?
-
public static String ?(?) {
return text + " :)";
}
-
public static ? ?(String text) {
int index = text.indexOf('.');
return text.substring(0, index + 1);
}
-
public static ? ?(?) {
return s.charAt(s.length() - 1);
}
-
public static ? ?(?) {
return g.nextInt(6) + 1 + g.nextInt(6) + 1;
}
-
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