Csc 216/s08/high virtue: Difference between revisions
No edit summary |
|||
(14 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 | 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 given here: | 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'. | ||
=== | ===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: [http://people.engr.ncsu.edu/efg/216/s08/video_002/g9/ Debugging Demonstration] | |||
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