💿
CS61B Fall 2022
  • CS61B Fall 2022
  • Prerequisites to running the assignments.
  • Lab 01: Setting up your computer
  • Lab 02: JUnit Tests and Debugging
  • Project 0: 2048
  • Lab 03: Timing Tests and Randomized Comparison Tests
  • Lab 04: A Debugging Mystery
  • Project 1: Deques
  • HW 2: Percolation
  • Lab 07: BSTMap
  • Project 2a: NGordNet (NGrams)
  • Lab 08: HashMap
  • Project 2b: NGordNet (WordNet)
  • Lab 11: BYOW Introduction
  • Project 3 BYOW
    • Phase 1: World Generation
Powered by GitBook
On this page
  • Part A: IntList Iteration
  • Original code - addConstant:
  • Solution - Fixed addConstant:
  • Part B: Nested Helper Methods and Refactoring for Debugging
  • Original code - setToZeroIfMaxFEL, firstDigitEqualsLastDigit
  • Solution - fixed firstDigitEqualsLastDigit
  • Part C: Tricky IntLists
  • Added JUnit testcases for the squarePrimes:
  • Original code - squarePrimes
  • Solution - fixed squarePrimes

Lab 02: JUnit Tests and Debugging

PreviousLab 01: Setting up your computerNextProject 0: 2048

Last updated 1 year ago

to running the code.

Instructions:

Solution:

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.

/* Expected Behavior */

IntList lst = IntList.of(1, 2, 3);

addConstant(lst, 1);
System.out.println(lst.toString());
// Output: 2 -> 3 -> 4

addConstant(lst, 4);
System.out.println(lst.toString());
// Output: 6 -> 7 -> 8

Original code - addConstant:

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:

  1. 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.

  2. 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.

  3. 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!

Added JUnit testcases for the squarePrimes:

@Test
public void testSquarePrimes2() {
    IntList lst = IntList.of(1, 2, 3, 4, 5, 6, 7);
    boolean changed = IntListExercises.squarePrimes(lst);
    assertEquals("1 -> 4 -> 9 -> 4 -> 25 -> 6 -> 49", lst.toString());
    assertTrue(changed);
}

@Test
public void testSquarePrimes3() {
    IntList lst = IntList.of(8, 9, 10);
    boolean changed = IntListExercises.squarePrimes(lst);
    assertEquals("8 -> 9 -> 10", lst.toString());
    assertFalse(changed);
}

Original code - 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 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;
}

Instructions:

Instructions:

https://fa22.datastructur.es/materials/lab/lab02/#part-b-nested-helper-methods-and-refactoring-for-debugging
https://fa22.datastructur.es/materials/lab/lab02/#part-c-tricky-intlists
Prerequisites
https://fa22.datastructur.es/materials/lab/lab02/
https://github.com/tomthestrom/cs61b/tree/master/lab02
Part C