CIS 163 |
Debugging |
Spring 2021 |
Objectives:
- Practice using a debugger
- Practice debugging code with the aid of a debugger
Activity
You may complete this lab in pairs.
Begin by following this GitHub Classroom link and cloning the resulting repository: https://classroom.github.com/g/u2zGlU19
The test testSummation
fails. Set a breakpoint in Buggy.summation
, step through the code, and identify the source of the bug.
- Add a comment to the
summation
method inBuggy.java
explaining when (which line of code, which iteration of the loop) during your step through the code do you first noticed evidence of a bug (e.g., see a variable with an incorrect value)? - Also add a comment describing the bug in your code and how you fixed the bug.
- Add test cases to
BuggyTest.java
thoroughly testingsummation
. Add comments explaining why you chose the test cases you did, and why you believe your set of test cases is sufficient.
The test testFactorial_v1
fails. Set a breakpoint in Buggy.factorial_v1
, step through the code, and identify the source of the bug.
- Add a comment to the
factorial_v1
method inBuggy.java
explaining when (which line of code, which iteration of the loop) during your step through the code do you first noticed evidence of a bug (e.g., see a variable with an incorrect value)? - Also add a comment describing the bug in your code and how you fixed the bug.
- Add test cases to
BuggyTest.java
thoroughly testingfactorial_v1
. Add comments explaining why you chose the test cases you did, and why you believe your set of test cases is sufficient.
The test testFactorial_v2_b
may enter an infinite loop. (That's why it isn't marked with @Test
: I didn't
want you to accidentally run a test that would cause your IDE to hang.). Add the @Test
annotation to this test, set a breakpoint in
Buggy.factorial_v2
, step through the code, and identify the source of the infinite loop.
- Add a comment to the
factorial_v2
method inBuggy.java
explaining when (which line of code, which iteration of the loop) during your step through the code do you first noticed evidence of a bug (e.g., see a variable with an incorrect value)? - Also add a comment describing the bug in your code and how you fixed the bug.
- Add test cases to
BuggyTest.java
thoroughly testingfactorial_v2
. Add comments explaining why you chose the test cases you did, and why you believe your set of test cases is sufficient. For now, assume the maximum input is 12.
Run all the tests in WordAnalyzerTest.java
. Some will pass and some will fail.
- Look through the stack trace generated by
testFRC_mate
. (Depending on your IDE, you may have to click on it.) As you look down from the top, what is the first line of code in codeWordAnalyzer.java
mentioned? - What happened on that line that caused an exception to be raised? (Use the debugger to step through the code, if necessary.)
- Add a comment describing the bug and how you fixed it.
- Add test cases to thoroughly test
firstRepeatedCharacter
. Add comments explaining why you chose the test cases you did, and why you believe your set of test cases is sufficient.
Use the debugger to find the bug in firstMultipleCharacter
. Note that the bug may actually be in the find
helper method.
As you debug this method, take note of the difference between "step over" and "step into". In particular, when you first step through firstMultipleCharacter
,
step over find
, examine the value returned by find
and see if you can guess what the bug is.
- Add a comment describing the bug and how you fixed it.
- Add test cases to thoroughly test
firstMultipleCharacter
. Add comments explaining why you chose the test cases you did, and why you believe your set of test cases is sufficient.
Use the debugger to find the bug in countRepeatedCharacters
.
- Add a comment describing the bug and how you fixed it.
- Add test cases to thoroughly test
countRepeatedCharacters
. Add comments explaining why you chose the test cases you did, and why you believe your set of test cases is sufficient.
Challenge Problem
If you compute 20! using either method in Buggy
, the answer will be negative. Why?
Submission
Commit the changes to the code. If you worked with a partner, be sure both names are in a comment at the head of each file.Acknowledgements
This lab is based on this lab by Cay Horstmann.
Updated Monday, 10 May 2021, 7:28 PM