This lab was fairly straightforward - as the title says - it was aimed at practicing the GUI of the debugger.
The files provided had some bugs and the tasks involved finding these bugs by running pre-written unit tests while using the debugger and in , writing our own unit tests to accomplish the same.
Part A: IntList Iteration
In this part, we will be debugging the addConstant method in IntListExercises.java. This method is intended to take in an IntList and mutatively add a constant to each element of the list.
public static void addConstant(IntList lst, int c) {
IntList head = lst;
while (head.rest != null) {
head.first += c;
head = head.rest;
}
}
Solution - Fixed addConstant:
public static void addConstant(IntList lst, int c) {
IntList next = lst.rest;
lst.first += c;
while (next != null) {
next.first += c;
next = next.rest;
}
}
Part B: Nested Helper Methods and Refactoring for Debugging
Problem - setToZeroIfMaxFEL method is buggy, fix it.
The bug was actually in the firstDigitEqualsLastDigit method
Original code - setToZeroIfMaxFEL, firstDigitEqualsLastDigit
/**
* Part B: Buggy method that sets node.first to zero if
* the max value in the list starting at node has the same
* first and last digit, for every node in L
*
* @param L IntList from Lecture
*/
public static void setToZeroIfMaxFEL(IntList L) {
IntList p = L;
while (p != null) {
if (firstDigitEqualsLastDigit(max(p))) {
p.first = 0;
}
p = p.rest;
}
}
public static boolean firstDigitEqualsLastDigit(int x) {
int lastDigit = x % 10;
while (x > 10) {
x = x / 10;
}
int firstDigit = x % 10;
return firstDigit == lastDigit;
}
Solution - fixed firstDigitEqualsLastDigit
/** Returns true if the last digit of x is equal to
* the first digit of x.
*/
public static boolean firstDigitEqualsLastDigit(int x) {
int lastDigit = x % 10;
while (x >= 10) {
x = x / 10;
}
int firstDigit = x % 10;
return firstDigit == lastDigit;
}
Part C: Tricky IntLists
Problem: the squarePrimes method is buggy. It’s your job to find and fix the bug! In order to do this, we recommend that you:
Create JUnit Test(s) that test the squarePrimes method over a variety of different inputs. Make sure to test that it makes updates to the passed-in IntList correctly and returns the correct boolean value.
Once you’ve created a JUnit Test on which squarePrimes fails, you’ve made progress! Woohoo! Now use the Java Debugger to step through the problem and isolate the bug.
Finally, write a fix to the bug. For this particular bug, the fix is not many lines of code. Finding the bug is much more difficult than fixing it!
public static boolean squarePrimes(IntList lst) {
// Base Case: we have reached the end of the list
if (lst == null) {
return false;
}
boolean currElemIsPrime = Primes.isPrime(lst.first);
if (currElemIsPrime) {
lst.first *= lst.first;
}
return currElemIsPrime || squarePrimes(lst.rest);
}
Solution - fixed squarePrimes
public static boolean squarePrimes(IntList lst) {
// Base Case: we have reached the end of the list
if (lst == null) {
return false;
}
boolean currElemIsPrime = Primes.isPrime(lst.first);
if (currElemIsPrime) {
lst.first *= lst.first;
}
return squarePrimes(lst.rest) || currElemIsPrime;
}