DEV Community

Discussion on: Introduction to Unit Testing with Java

Collapse
 
itzsaga profile image
Seth

Great intro. I hit an error on line 11 of the final code. I get an Error:(11, 16) java: Math() has private access in Math. IntelliJ's linter is yelling about it as well. My Java knowledge is minimal so I'm wondering how would I fix this error? I'm guessing it has something to do with line 2 of Math.java.

Collapse
 
chrisvasqm profile image
Christian Vasquez • Edited

Thanks for letting me know, Seth!

That's my fault.

Try removing this from the Math.java file:

private Math() {}

The entire class should be like this now:

public final class Math {

    public static int add(int firstNumber, int secondNumber) {
        return firstNumber + secondNumber;
    }

    public static int multiply(int multiplicand, int multiplier) {
        return multiplicand * multiplier;
    }

    public static double divide(int dividend, int divisor) {
        if (divisor == 0)
            throw new IllegalArgumentException("Cannot divide by zero (0).");

        return dividend / divisor;
    }
}

In case you or someone else also wonders why, the private Math() {} refers to the constructor of our Math class, I made it private at the beginning because all it's methods are static, which prevents anyone from trying to instantiate it. But later on I decided to also add an example where we had the need to use an object and I forgot to update it hahaha.

Collapse
 
itzsaga profile image
Seth

That works. Thanks!