Compsci201_Recitation3

advertisement
Debugging
James Wei
Professor Peck
1/24/2013
http://goo.gl/kbbdmP
The Eclipse Workbench
1.Perspective
2.Task list
3.Project
Explorer view
4.Outline
5.Problems
view
Debugging a Program
1. Debug perspective
2. Select the file
3. Make a breakpoint in
your program
1. Double-click on the
LHS
2. If you can’t see it,
right click on the
LHS ribbon and
select Toggle
Breakpoint
4. Click on the Debug
icon.
Debugging a Program
• Step Over
• Step Into
• Step Return
• Resume
• Terminate
Watching an Expression
1: public class MysteryClass {
2:
3: public static void main(String args[])
4: {
5:
System.out.println(mysteryMethod(12));
6: }
7:
8: public static int mysteryMethod(int n) {
9: 
}
}
Watching an Expression
breakpoint
8: public static int mysteryMethod(int n) {
9:
boolean array [] = new boolean[n];
10:
11:
for (int i = 0; i < n; i++)
12:
{
13:
array[i] = true;
14:
}
15:
16:
for (int i = 2; i < n; i++)
17:
{
18:
for(int j = 2*i; j<n; j=j+i)
19:
array[j] = false;
20:
}
21:
22:
int count = 0;
23:
for (int i = 2; i < n; i++)
24:
if(array[i])
25:
count++;
26:
27:
28:
return count;
29: }
Watching an Expression
Go to Window  Show View 
Expressions
Watching an Expression
• Add array[i] to the
Expressions view.
• Add i to the Expressions view.
Watching an Expression
Step through: you can step over using
Each time you step over, watch what happens to the
values in the Expressions view in each iteration
•
•
•
•
What is the value of array[i] when i = 6?
What is the value of array[i] when i = 3?
What is the value of array[i] when i = 5?
What do you think this program is doing?
Enjoy the rest of your Friday!
Don’t forget to submit your answers to get credit for today!
Download