Hi Folks,
I am getting the error "Constructor NegativeException() is undefined" in my Root class. Can anyone point me in the right direction?
SAMPLE QUESTION
Attempting to get the square root of a negative number is an illegal mathematical operation.
Java supplies a square root method in the Math class i.e.:
public static double sqrt(double a)
This method returns a special constant double value (NaN i.e. not a number) if the parameter had a negative value. It does not throw an exception.
a) Write the Java code for a custom exception class to represent an exception resulting from an attempt to get the square root of a negative number (double). This class should inherit from ArithmeticException, and only needs a simple constructor whose parameter holds the value of the number which has caused the exception.
b) Write the Java code for a Root class which contains a sqrt method which will return the square root of a double passed to it as a parameter or throw an exception of the type defined in (a) if the parameter is negative. It should call Math.sqrt() if no exception occurs.
c) Write the code for a main method in a
test class which calls the method defined in (b), inside a try/catch block.