> For the complete documentation index, see [llms.txt](https://tomthestrom.gitbook.io/cs61b-fall-2022/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tomthestrom.gitbook.io/cs61b-fall-2022/lab-02-junit-tests-and-debugging.md).

# Lab 02: JUnit Tests and Debugging

[Prerequisites](/cs61b-fall-2022/prerequisites-to-running-the-assignments..md) to running the code.

Instructions: <https://fa22.datastructur.es/materials/lab/lab02/>

Solution: <https://github.com/tomthestrom/cs61b/tree/master/lab02>

This lab was fairly straightforward - as the title says - it was aimed at practicing the GUI of the debugger.&#x20;

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 [Part C](#part-c-tricky-intlists), 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.

```java
/* 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:

```java
public static void addConstant(IntList lst, int c) {
    IntList head = lst;
    while (head.rest != null) {
        head.first += c;
        head = head.rest;
    }
}
```

### Solution - Fixed addConstant:&#x20;

```java
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 <a href="#part-b-nested-helper-methods-and-refactoring-for-debugging" id="part-b-nested-helper-methods-and-refactoring-for-debugging"></a>

Instructions: <https://fa22.datastructur.es/materials/lab/lab02/#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`

```java
/**
 * 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`

```java
/** 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 <a href="#part-c-tricky-intlists" id="part-c-tricky-intlists"></a>

Instructions: <https://fa22.datastructur.es/materials/lab/lab02/#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`:

```java
@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`

```java
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`

```java
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;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tomthestrom.gitbook.io/cs61b-fall-2022/lab-02-junit-tests-and-debugging.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
