Question: Implement a custom exception class named SquareRootArgumentException which extends the IllegalArgumentException class and includes a default constructor and an overloaded constructor which accepts a String
Implement a custom exception class named SquareRootArgumentException which extends the IllegalArgumentException class and includes a default constructor and an overloaded constructor which accepts a String parameter named "msg" and invokes the super class's constructor with that parameter as an argument.
No instance variables are required for the SquareRootArgumentException.
Then implement a public Java class named "SquareRooter" which includes a static method named "sqrRoot". This method returns a double and takes one double parameter named val, e.g.
public static double sqrRoot(double val)
If val is 0 or positive, the method returns the result of calling "Math.sqrt" with val. If val is negative, the method throws a new SquareRootArgumentException with an informative message.
Implement a main method in your SquareRooter class which includes a try/catch block that tests your sqrRoot method using a positive and negative value (for instance: 4 and -1). Sample output is shown below. Your main method should print the stack trace of the exception when caught.
Do not display any output in the sqrRoot method, all output should be generated from the main method.
Expected Output (your line numbers may vary):
calling sqrRoot with 4
sqr root of 4 is 2.0
calling sqrRoot with -1
SquareRootArgumentException: cannot calculate square root of negatives
at SquareRooter.sqrRoot(SquareRooter.java:25)
at SquareRooter.main(SquareRooter.java:13)
Attach and submit your SquareRooter.java file to this assignment. Do not declare your SquareRootArgumentException class as public so it can be included in the SquareRooter.java file.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
