ronantakizawa/codereview-bench huggingface dataset:
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/maths/MedianTest.java (Java)

Code before:
    }

    @Test
        //Many values case
    void medianDecimalValueReturn() {
        //1, 2, 3, 4, 5, 6, 8, 9
        int[] arr = {1, 2, 3, 4, 5, 6, 8, 9};
        assertEquals(4.5, Median.median(arr));
    }

    @Test
        //Negative value case
    void medianNegativeValues() {
        //-27, -16, -7, -4, -2, -1
        int[] arr = {-27, -16, -7, -4, -2, -1};
        assertEquals(-5.5, Median.median(arr));
    }

    @Test
        //Unsorted values case
    void medianUnsortedValues() {
        //before sorting : 16, 3, 8, 12, 13, 1, 5
        //after sorting : 1, 3, 5, 8, 12, 13, 16
        int[] arr = {16, 3, 8, 12, 13, 1, 5};
        assertEquals(8, Median.median(arr));
    }
}


Review comment (suggestion):
I think this test should be removed because the behavior of this function (Java function, not math function) is not well-defined for unsorted array

Code after:
package com.thealgorithms.maths;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class MedianTest {
    @Test
    void medianSingleValue() {
        int[] arr = {0};
        assertEquals(0, Median.median(arr));
    }

    @Test
    void medianTwoValues() {
        int[] arr = {1, 2};
        assertEquals(1.5, Median.median(arr));
    }

    @Test
    void medianThreeValues() {
        int[] arr = {1, 2, 3};
        assertEquals(2, Median.median(arr));
    }

    @Test
    void medianDecimalValueReturn() {
        int[] arr = {1, 2, 3, 4, 5, 6, 8, 9};
        assertEquals(4.5, Median.median(arr));
    }

    @Test
    void medianNegativeValues() {
        int[] arr = {-27, -16, -7, -4, -2, -1};
        assertEquals(-5.5, Median.median(arr));
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/dynamicprogramming/ClimbingStairs.java (Java)

Code before:
    public static int numberOfWays(int n) {

        if(n <= 1){
            return n;
        }
        int prev = 1;
        int curr = 1;

        int next;

        for(int i = 2; i <= n; i++){
            next = curr+prev;
            prev = curr;

            curr = next;
        }

        return curr;

    }

    public static void main(String[] args) {

       int result = numberOfWays(5);
        System.out.println(result);
    }
}


Review comment (suggestion):
Please remove `main`, it's redundant as there are tests

Code after:
package com.thealgorithms.dynamicprogramming;

/* A DynamicProgramming solution for Climbing Stairs' problem Returns the
    distinct ways can you climb to the staircase by either climbing 1 or 2 steps.

    Link : https://medium.com/analytics-vidhya/leetcode-q70-climbing-stairs-easy-444a4aae54e8
*/
public class ClimbingStairs {

    public static int numberOfWays(int n) {

        if(n == 1 || n == 0){
            return n;
        }
        int prev = 1;
        int curr = 1;

        int next;

        for(int i = 2; i <= n; i++){
            next = curr+prev;
            prev = curr;

            curr = next;
        }

        return curr;

    }
}
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/dynamicprogramming/MinimumPathSumTest.java (Java)

Code before:
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class MinimumPathSumTest {

    @Test
    public void testMinimumPathSumWithRegularGrid() {
        int[][] grid = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}};
        assertEquals(7, MinimumPathSum.minimumPathSum(grid));
    }

    @Test
    public void testMinimumPathSumWithOneRowOneColumnGrid() {
        int[][] grid = {{2}};
        assertEquals(2, MinimumPathSum.minimumPathSum(grid));
    }

    @Test
    public void testMinimumPathSumWithEmptyGrid() {
        int[][] grid = {{}};
        assertEquals(0, MinimumPathSum.minimumPathSum(grid));
    }

    @Test
    public void testMinimumPathSumWithOneColumnGrid() {
        int[][] grid = {{1}, {2}, {3}};
        assertEquals(6, MinimumPathSum.minimumPathSum(grid));

Review comment (suggestion):
```suggestion
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
```

Code after:
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

public class MinimumPathSumTest {

    @Test
    public void testMinimumPathSumWithRegularGrid() {
        int[][] grid = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}};
        assertEquals(7, MinimumPathSum.minimumPathSum(grid));
    }

    @Test
    public void testMinimumPathSumWithOneRowOneColumnGrid() {
        int[][] grid = {{2}};
        assertEquals(2, MinimumPathSum.minimumPathSum(grid));
    }

    @Test
    public void testMinimumPathSumWithEmptyGrid() {
        int[][] grid = {{}};
        assertEquals(0, MinimumPathSum.minimumPathSum(grid));
    }

    @Test
    public void testMinimumPathSumWithOneColumnGrid() {
        int[][] grid = {{1}, {2}, {3}};
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/audiofilters/IIRFilter.java (Java)

Code before:
package com.thealgorithms.audiofilters;

/**
 * N-Order IIR Filter Assumes inputs are normalized to [-1, 1]
 * <p>
 * Based on the difference equation from
 * <a href="https://en.wikipedia.org/wiki/Infinite_impulse_response">...</a>
 */
public class IIRFilter {

    private final int order;
    private final double[] coeffsA;
    private final double[] coeffsB;
    private final double[] historyX;
    private final double[] historyY;

    /**
     * Construct an IIR Filter
     *
     * @param order the filter's order
     * @throws IllegalArgumentException if order is zero or less
     */
    public IIRFilter(int order) throws IllegalArgumentException {
        if (order < 1) {
            throw new IllegalArgumentException("order must be greater than zero");
        }

        this.order = order;
        coeffsA = new double[order + 1];
        coeffsB = new double[order + 1];

        // Sane defaults

Review comment (suggestion):
```suggestion
 * <a href="https://en.wikipedia.org/wiki/Infinite_impulse_response">here</a>
```

Code after:
package com.thealgorithms.audiofilters;

/**
 * N-Order IIR Filter Assumes inputs are normalized to [-1, 1]
 *
 * Based on the difference equation from
 * <a href="https://en.wikipedia.org/wiki/Infinite_impulse_response">Wikipedia link</a>
 */
public class IIRFilter {

    private final int order;
    private final double[] coeffsA;
    private final double[] coeffsB;
    private final double[] historyX;
    private final double[] historyY;

    /**
     * Construct an IIR Filter
     *
     * @param order the filter's order
     * @throws IllegalArgumentException if order is zero or less
     */
    public IIRFilter(int order) throws IllegalArgumentException {
        if (order < 1) {
            throw new IllegalArgumentException("order must be greater than zero");
        }

        this.order = order;
        coeffsA = new double[order + 1];
        coeffsB = new double[order + 1];

        // Sane defaults
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/Factorial.java (Java)

Code before:
package com.thealgorithms.maths;

public class Factorial {
    /**
     * Calculate factorial N using iteration
     *
     * @param n the number
     * @return the factorial of {@code n}
     */
    public static long factorial(int n) {
        if (n < 0) {
            throw new IllegalArgumentException("Input number cannot be negative!");
        }
        long factorial = 1;
        for (int i = 1; i <= n; ++i) {
            factorial *= i;
        }
        return factorial;
    }
}


Review comment (suggestion):
```suggestion
    public static long factorial(final int n) {
```

Code after:
package com.thealgorithms.maths;

public class Factorial {
    /**
     * Calculate factorial N using iteration
     *
     * @param n the number
     * @return the factorial of {@code n}
     */
    public static long factorial(int n) {
        if (n < 0) {
            throw new IllegalArgumentException("Input number cannot be negative");
        }
        long factorial = 1;
        for (int i = 1; i <= n; ++i) {
            factorial *= i;
        }
        return factorial;
    }
}
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/maths/FactorialTest.java (Java)

Code before:
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

public class FactorialTest {
    private static final String EXCEPTION_MESSAGE = "Input number cannot be negative!";

    @Test
    public void testWhenInvalidInoutProvidedShouldThrowException() {
        IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1));
        assertEquals(exception.getMessage(), EXCEPTION_MESSAGE);
    }

    @Test
    public void testCorrectFactorialCalculation() {
        assertEquals(1, Factorial.factorial(0));
        assertEquals(1, Factorial.factorial(1));
        assertEquals(120, Factorial.factorial(5));
        assertEquals(3628800, Factorial.factorial(10));
    }
}


Review comment (suggestion):
```suggestion
    private static final String EXCEPTION_MESSAGE = "Input number cannot be negative";
```
I wouldn't be so emotional in the exception message :)

Code after:
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

public class FactorialTest {
    private static final String EXCEPTION_MESSAGE = "Input number cannot be negative";

    @Test
    public void testWhenInvalidInoutProvidedShouldThrowException() {
        IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1));
        assertEquals(exception.getMessage(), EXCEPTION_MESSAGE);
    }

    @Test
    public void testCorrectFactorialCalculation() {
        assertEquals(1, Factorial.factorial(0));
        assertEquals(1, Factorial.factorial(1));
        assertEquals(120, Factorial.factorial(5));
        assertEquals(3628800, Factorial.factorial(10));
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/Factorial.java (Java)

Code before:
package com.thealgorithms.maths;

public class Factorial {
    /**
     * Calculate factorial N using iteration
     *
     * @param n the number
     * @return the factorial of {@code n}
     */
    public static long factorial(int n) {
        if (n < 0) {
            throw new IllegalArgumentException("Input number cannot be negative!");
        }
        long factorial = 1;
        for (int i = 1; i <= n; ++i) {
            factorial *= i;
        }
        return factorial;
    }
}


Review comment (suggestion):
```suggestion
            throw new IllegalArgumentException("Input number cannot be negative");
```

Code after:
package com.thealgorithms.maths;

public class Factorial {
    /**
     * Calculate factorial N using iteration
     *
     * @param n the number
     * @return the factorial of {@code n}
     */
    public static long factorial(int n) {
        if (n < 0) {
            throw new IllegalArgumentException("Input number cannot be negative");
        }
        long factorial = 1;
        for (int i = 1; i <= n; ++i) {
            factorial *= i;
        }
        return factorial;
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java (Java)

Code before:
package com.thealgorithms.bitmanipulation;

/**
 * Find Highest Set Bit
 * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
 */

public class HighestSetBit {
    public static int findHighestSetBit(int num) {
        if (num < 0) {
            throw new IllegalArgumentException("Input cannot be negative");
        }

        if (num == 0) {
            return -1; // No set bits
        }

        int position = 0;
        while (num > 0) {
            num >>= 1;
            position++;
        }

        return position - 1;
    }
}

Review comment (refactor):
```suggestion
public final class HighestSetBit {
    private HighestSetBit() {
    }
```

Please note that there are two things here:
- having the _default_ constructor marked as `private` makes it impossible to have objects of that class (it does not make sense to have any object of that class),
- making this class as `final` makes it impossible to derive new ones from of (again: it does not make sense to do it).

Above makes that class a _proper utility class_.

Code after:
package com.thealgorithms.bitmanipulation;
import java.util.Optional;

/**
 * Find Highest Set Bit
 * This class provides a function calculating the position (or index)
 * of the most significant bit being set to 1 in a given integer.
 * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
 */

public final class HighestSetBit {
    private HighestSetBit() {
    }

    public final static Optional<Integer> findHighestSetBit(int num) {
        if (num < 0) {
            throw new IllegalArgumentException("Input cannot be negative");
        }

        if (num == 0) {
            return Optional.empty();
        }

        int position = 0;
        while (num > 0) {
            num >>= 1;
            position++;
        }

        return Optional.of(position - 1);
    }
}
Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/bitmanipulation/HighestSetBitTest.java (Java)

Code before:
package com.thealgorithms.bitmanipulation;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

/**
 * Test case for Highest Set Bit
 * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
 */

class HighestSetBitTest {

    @Test
    void testHighestSetBit() {
        assertEquals(5, HighestSetBit.findHighestSetBit(37).orElse(-1));
        assertFalse(HighestSetBit.findHighestSetBit(0).isPresent());
        assertEquals(0, HighestSetBit.findHighestSetBit(1).orElse(-1));
        assertThrows(IllegalArgumentException.class, () -> HighestSetBit.findHighestSetBit(-37).orElse(-1));
    }
}

Review comment (suggestion):
In order to _get_ the value from some `Optional` object, you can use the [`get()`](https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#get--) method. If the value is not there, a suitable exception will be thrown.

```suggestion
        assertEquals(5, HighestSetBit.findHighestSetBit(37).get());
        assertFalse(HighestSetBit.findHighestSetBit(0).isPresent());
        assertEquals(0, HighestSetBit.findHighestSetBit(1).get());
        assertThrows(IllegalArgumentException.class, () -> HighestSetBit.findHighestSetBit(-37));
```

Code after:
package com.thealgorithms.bitmanipulation;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

/**
 * Test case for Highest Set Bit
 * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
 */

class HighestSetBitTest {

    @Test
    void testHighestSetBit() {
        assertFalse(HighestSetBit.findHighestSetBit(0).isPresent());
        assertEquals(0, HighestSetBit.findHighestSetBit(1).get());
        assertEquals(1, HighestSetBit.findHighestSetBit(2).get());
        assertEquals(1, HighestSetBit.findHighestSetBit(3).get());
        assertEquals(2, HighestSetBit.findHighestSetBit(4).get());
        assertEquals(2, HighestSetBit.findHighestSetBit(5).get());
        assertEquals(2, HighestSetBit.findHighestSetBit(7).get());
        assertEquals(3, HighestSetBit.findHighestSetBit(8).get());
        assertEquals(3, HighestSetBit.findHighestSetBit(9).get());
        assertEquals(3, HighestSetBit.findHighestSetBit(15).get());
        assertEquals(4, HighestSetBit.findHighestSetBit(16).get());
        assertEquals(4, HighestSetBit.findHighestSetBit(17).get());
        assertEquals(4, HighestSetBit.findHighestSetBit(31).get());
        assertEquals(5, HighestSetBit.findHighestSetBit(32).get());
        assertEquals(5, HighestSetBit.findHighestSetBit(33).get());
        assertEquals(7, HighestSetBit.findHighestSetBit(255).get());
        assertEquals(8, HighestSetBit.findHighestSetBit(256).get());
        assertEquals(8, HighestSetBit.findHighestSetBit(511).get());
        assertEquals(9, HighestSetBit.findHighestSetBit(512).get());
        assertThrows(IllegalArgumentException.class, () -> HighestSetBit.findHighestSetBit(-37));
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java (Java)

Code before:
 * Find Highest Set Bit
 * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
 */

public final class HighestSetBit {
    private HighestSetBit() {
    }

    public final static Optional<Integer> findHighestSetBit(int num) {
        if (num < 0) {
            throw new IllegalArgumentException("Input cannot be negative");
        }

        if (num == 0) {
            return Optional.empty();
        }

        int position = 0;
        while (num > 0) {
            num >>= 1;
            position++;
        }

        return Optional.of(position - 1);
    }
}

Review comment (suggestion):
```suggestion
}

```

~~Does anyone know why `clang-format` is not complaining about that?~~ I will suggest a change fixing that.

Code after:
 * Find Highest Set Bit
 * This class provides a function calculating the position (or index)
 * of the most significant bit being set to 1 in a given integer.
 * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
 */

public final class HighestSetBit {
    private HighestSetBit() {
    }

    public final static Optional<Integer> findHighestSetBit(int num) {
        if (num < 0) {
            throw new IllegalArgumentException("Input cannot be negative");
        }

        if (num == 0) {
            return Optional.empty();
        }

        int position = 0;
        while (num > 0) {
            num >>= 1;
            position++;
        }

        return Optional.of(position - 1);
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java (Java)

Code before:
package com.thealgorithms.bitmanipulation;
import java.util.Optional;

/**
 * Find Highest Set Bit
 * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
 */

public final class HighestSetBit {
    private HighestSetBit() {
    }

    public final static Optional<Integer> findHighestSetBit(int num) {
        if (num < 0) {
            throw new IllegalArgumentException("Input cannot be negative");
        }

        if (num == 0) {
            return Optional.empty();
        }

        int position = 0;
        while (num > 0) {
            num >>= 1;
            position++;
        }

        return Optional.of(position - 1);
    }
}

Review comment (suggestion):
I would [still suggest](https://github.com/TheAlgorithms/Java/pull/4330#pullrequestreview-1605356890) to add some explanation what the _highest set bit_ is.

Code after:
package com.thealgorithms.bitmanipulation;
import java.util.Optional;

/**
 * Find Highest Set Bit
 * This class provides a function calculating the position (or index)
 * of the most significant bit being set to 1 in a given integer.
 * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
 */

public final class HighestSetBit {
    private HighestSetBit() {
    }

    public final static Optional<Integer> findHighestSetBit(int num) {
        if (num < 0) {
            throw new IllegalArgumentException("Input cannot be negative");
        }

        if (num == 0) {
            return Optional.empty();
        }

        int position = 0;
        while (num > 0) {
            num >>= 1;
            position++;
        }

        return Optional.of(position - 1);
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java (Java)

Code before:
 * Find Highest Set Bit
 * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
 */

public final class HighestSetBit {
    private HighestSetBit() {
    }

    public final static Optional<Integer> findHighestSetBit(int num) {
        if (num < 0) {
            throw new IllegalArgumentException("Input cannot be negative");
        }

        if (num == 0) {
            return Optional.empty();
        }

        int position = 0;
        while (num > 0) {
            num >>= 1;
            position++;
        }

        return Optional.of(position - 1);
    }
}

Review comment (bug):
```suggestion
}

```
After merging #4343, not having a _new line symbol_ at the end of the file will cause [Clang format linter](https://github.com/TheAlgorithms/Java/blob/09950d6097ea7b2590fb80336d144ee402e61226/.github/workflows/clang-format-lint.yml) to fail.

In any case, please _rebase_ before merging.

Code after:
 * Find Highest Set Bit
 * This class provides a function calculating the position (or index)
 * of the most significant bit being set to 1 in a given integer.
 * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
 */

public final class HighestSetBit {
    private HighestSetBit() {
    }

    public final static Optional<Integer> findHighestSetBit(int num) {
        if (num < 0) {
            throw new IllegalArgumentException("Input cannot be negative");
        }

        if (num == 0) {
            return Optional.empty();
        }

        int position = 0;
        while (num > 0) {
            num >>= 1;
            position++;
        }

        return Optional.of(position - 1);
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/bitmanipulation/HighestSetBit.java (Java)

Code before:
package com.thealgorithms.bitmanipulation;
import java.util.Optional;

/**
 * Find Highest Set Bit
 * This function calculates the position (or index) of the highest set bit (the leftmost 1 bit)
 * in a given binary number.
 * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
 */

public final class HighestSetBit {
    private HighestSetBit() {
    }

    public final static Optional<Integer> findHighestSetBit(int num) {
        if (num < 0) {
            throw new IllegalArgumentException("Input cannot be negative");
        }

        if (num == 0) {
            return Optional.empty();
        }

        int position = 0;
        while (num > 0) {
            num >>= 1;
            position++;
        }

        return Optional.of(position - 1);
    }
}

Review comment (suggestion):
I am not sure what _leftmost_ means here. I think it is more safe to explain what this function does in the following way:
```suggestion
 * This class provides a function calculating the position (or index)
 * of the most significant bit being set to 1 in a given integer.
```

Code after:
package com.thealgorithms.bitmanipulation;
import java.util.Optional;

/**
 * Find Highest Set Bit
 * This class provides a function calculating the position (or index)
 * of the most significant bit being set to 1 in a given integer.
 * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
 */

public final class HighestSetBit {
    private HighestSetBit() {
    }

    public final static Optional<Integer> findHighestSetBit(int num) {
        if (num < 0) {
            throw new IllegalArgumentException("Input cannot be negative");
        }

        if (num == 0) {
            return Optional.empty();
        }

        int position = 0;
        while (num > 0) {
            num >>= 1;
            position++;
        }

        return Optional.of(position - 1);
    }
}