Skip to main content

CIS 163

Debugging

Spring 2021

Objectives:

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.

  1. Add a comment to the summation method in Buggy.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)?
  2. Also add a comment describing the bug in your code and how you fixed the bug.
  3. Add test cases to BuggyTest.java thoroughly testing summation. 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.

  1. Add a comment to the factorial_v1 method in Buggy.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)?
  2. Also add a comment describing the bug in your code and how you fixed the bug.
  3. Add test cases to BuggyTest.java thoroughly testing factorial_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.

  1. Add a comment to the factorial_v2 method in Buggy.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)?
  2. Also add a comment describing the bug in your code and how you fixed the bug.
  3. Add test cases to BuggyTest.java thoroughly testing factorial_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.

  1. 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 code WordAnalyzer.java mentioned?
  2. What happened on that line that caused an exception to be raised? (Use the debugger to step through the code, if necessary.)
  3. Add a comment describing the bug and how you fixed it.
  4. 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.

  1. Add a comment describing the bug and how you fixed it.
  2. 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.

  1. Add a comment describing the bug and how you fixed it.
  2. 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

W3c Validation