Csc 216/s08/high virtue: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
(25 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Interactive Debugging==
==Interactive Debugging==


This exercise illustrates stepping through a program line by line to debug it.
This exercise illustrates stepping through a program line by line to debug it. It should help students understand the usefulness of a line by line approach to debugging like the one found in Eclipse. 


===The Problem===
===The Problem===


A simple example program is given here but many more could be designed.
A simple program is given below. Feel free to write your own programs to apply this exercise to as well.
 
<pre>
import java.util.Scanner;
public class Debug {
public static void main(String[] args) {
System.out.println("Enter an integer");
Scanner s = new Scanner(System.in);
//this line should store the input in the variable x.
        int x = s.nextInt();
//this line should add 5 to x.
        x = x+5;
//this line should return the additive inverse of x
x = -1*Math.abs(x);
//this line should double the integer
x = Math.round(x/2);
//this line should print -2*(x+5).
System.out.println(x);
}
}
</pre>


===Participants and Props===
===Participants and Props===


This exercise will require members of the class to act as lines of codes and one to be the programmer/ debugger.  Cards with code on one side and comments on the other should be prepared before class.  An example of a card for this program is below.
This exercise will require members of the class to act as lines of codes and one to be the programmer/ debugger.  Cards with code on one side and comments on the other should be prepared before class.  An example of a card for this program is given here: [http://pg-server.csc.ncsu.edu/mediawiki/index.php/Image:Side1.jpg Side 1] and [http://pg-server.csc.ncsu.edu/mediawiki/index.php/Image:Side2.jpg Side 2]. An object, such as a ball, that can be passed between students will be needed to represent a 'flag'.  


[[Image:Example.jpg]] 


===The Script===
===How to Play===


Pick one student to be the programmer. Pick other students to represent lines of code, as many students as cards. It would be helpful to display the entire program on a screen for the whole class to see.  
Choose a programmer to run and debug the program. Choose several students to represent the lines of code in the program. Each student is handed a card with code on one side and the comment explaining what that code is supposed to be doing on the other side. The students who represent the lines of code should keep the side of the card with the code on it towards themselves and only show the programmer the side with the comments. The students stand in the order the code is supposed to come in. They then wait for the programmer to run them. The programmer passes a parameter of the correct type to the program. Each line of code then executes on the parameter given. Based upon the comments that the programmer is allowed to see and the parameter that he or she passed, they should be able to deduce what the desired, correct output should be. If the lines of code execute and the end result is different from what the programmer computed then it is necessary for the programmer to debug the program. The programmer will start debug mode and hand a flag, which represents the break point, to the first line of code. The first line of code will execute, and the rest of the program waits. The programmer can then query what the current value of the variable is. If satisfied the programmer will step to the next line of code, modeled by the chosen programmer saying, "Pass the flag down to the next line." If not satisfied the programmer will ask the student to show him the code side of the card. The programmer will then make the necessary corrections and continue the debugging process.


The programmer should place a flag where they think the program's problems start, likely with the first line of code represented by a student.  Model this by having the student lines of code pass a ball or flag.
Here is a demonstration of the game: [http://people.engr.ncsu.edu/efg/216/s08/video_002/g9/ Debugging Demonstration]
The programmer should be able to read the comments on the card but not the code which is on the back side of the card. The programmer can ask the student line of code for the current value of any variables of interest, in this example, x. If the value is not what is expected, the programmer will know to change this line of code. 
When satisfied with one line, the programmer should ask to step to the next line.  The student line of code will then pass the ball/ flag to the next student line of code.  Again, the programmer can ask for the current value of any variables.  Continue stepping through the program and editing lines as you move along until the end is reached.
The programmer will need to try several inputs to ensure all bugs are removed.

Latest revision as of 19:13, 6 November 2008

Interactive Debugging

This exercise illustrates stepping through a program line by line to debug it. It should help students understand the usefulness of a line by line approach to debugging like the one found in Eclipse.

The Problem

A simple program is given below. Feel free to write your own programs to apply this exercise to as well.

import java.util.Scanner;
public class Debug {
	public static void main(String[] args) {
		System.out.println("Enter an integer");
		Scanner s = new Scanner(System.in);
		//this line should store the input in the variable x.
	        int x = s.nextInt();
		//this line should add 5 to x.
	        x = x+5;
		//this line should return the additive inverse of x
		x = -1*Math.abs(x);
		//this line should double the integer
		x = Math.round(x/2);
		//this line should print -2*(x+5).
		System.out.println(x);
	}
}

Participants and Props

This exercise will require members of the class to act as lines of codes and one to be the programmer/ debugger. Cards with code on one side and comments on the other should be prepared before class. An example of a card for this program is given here: Side 1 and Side 2. An object, such as a ball, that can be passed between students will be needed to represent a 'flag'.


How to Play

Choose a programmer to run and debug the program. Choose several students to represent the lines of code in the program. Each student is handed a card with code on one side and the comment explaining what that code is supposed to be doing on the other side. The students who represent the lines of code should keep the side of the card with the code on it towards themselves and only show the programmer the side with the comments. The students stand in the order the code is supposed to come in. They then wait for the programmer to run them. The programmer passes a parameter of the correct type to the program. Each line of code then executes on the parameter given. Based upon the comments that the programmer is allowed to see and the parameter that he or she passed, they should be able to deduce what the desired, correct output should be. If the lines of code execute and the end result is different from what the programmer computed then it is necessary for the programmer to debug the program. The programmer will start debug mode and hand a flag, which represents the break point, to the first line of code. The first line of code will execute, and the rest of the program waits. The programmer can then query what the current value of the variable is. If satisfied the programmer will step to the next line of code, modeled by the chosen programmer saying, "Pass the flag down to the next line." If not satisfied the programmer will ask the student to show him the code side of the card. The programmer will then make the necessary corrections and continue the debugging process.

Here is a demonstration of the game: Debugging Demonstration