Question:
The MathClassMethods class contains various methods for performing mathematical operations. Consider the following code snippet:
public class MathClassMethods {
// Returns the square root of a number
public static double squareRoot(double num) {
return Math.sqrt(num);
}
// Returns the absolute value of a number
public static double absoluteValue(double num) {
return Math.abs(num);
}
// Returns the value of a number raised to a power
public static double power(double base, double exponent) {
return Math.pow(base, exponent);
}
// Returns the floor value of a number
public static int floorValue(double num) {
return (int) Math.floor(num);
}
}
You have been asked to test these methods thoroughly to ensure they are working correctly. Write a Java program that includes appropriate test cases for each of the methods in the MathClassMethods class. Make sure to include test cases that cover different scenarios and edge cases. Provide the complete code for your Java program, including the test cases, in the given space below.
Answer:
public class MathClassMethodsTester {
public static void main(String[] args) {
testSquareRoot();
testAbsoluteValue();
testPower();
testFloorValue();
}
public static void testSquareRoot() {
// Positive number
double result1 = MathClassMethods.squareRoot(16);
System.out.println("Square root of 16: " + result1); // Expected output: 4
// Zero
double result2 = MathClassMethods.squareRoot(0);
System.out.println("Square root of 0: " + result2); // Expected output: 0
// Negative number
double result3 = MathClassMethods.squareRoot(-25);
System.out.println("Square root of -25: " + result3); // Expected output: NaN
}
public static void testAbsoluteValue() {
// Positive number
double result1 = MathClassMethods.absoluteValue(10);
System.out.println("Absolute value of 10: " + result1); // Expected output: 10
// Negative number
double result2 = MathClassMethods.absoluteValue(-15);
System.out.println("Absolute value of -15: " + result2); // Expected output: 15
// Zero
double result3 = MathClassMethods.absoluteValue(0);
System.out.println("Absolute value of 0: " + result3); // Expected output: 0
}
public static void testPower() {
// Positive base and positive exponent
double result1 = MathClassMethods.power(2, 3);
System.out.println("2 raised to the power of 3: " + result1); // Expected output: 8
// Negative base and positive exponent
double result2 = MathClassMethods.power(-2, 4);
System.out.println("-2 raised to the power of 4: " + result2); // Expected output: 16
// Zero base and positive exponent
double result3 = MathClassMethods.power(0, 5);
System.out.println("0 raised to the power of 5: " + result3); // Expected output: 0
// Positive base and zero exponent
double result4 = MathClassMethods.power(3, 0);
System.out.println("3 raised to the power of 0: " + result4); // Expected output: 1
}
public static void testFloorValue() {
// Positive number
int result1 = MathClassMethods.floorValue(7.8);
System.out.println("Floor value of 7.8: " + result1); // Expected output: 7
// Negative number
int result2 = MathClassMethods.floorValue(-3.2);
System.out.println("Floor value of -3.2: " + result2); // Expected output: -4
// Integer value
int result3 = MathClassMethods.floorValue(10);
System.out.println("Floor value of 10: " + result3); // Expected output: 10
}
}
In the provided solution, the MathClassMethodsTester
class contains four test methods: testSquareRoot()
, testAbsoluteValue()
, testPower()
, and testFloorValue()
. Each test method calls the respective method from MathClassMethods
class and prints the expected output along with the actual output. The test cases cover various scenarios and edge cases for each of the methods.
By running the MathClassMethodsTester
class, you can verify the correctness of the methods defined in MathClassMethods
.