Question: I need the Java code for the following: Implement a Java class named SquareRooter which includes a static method named sqrRoot. This method returns a

I need the Java code for the following:

Implement a 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 "SquareRootArgumentException" which is a custom exception class you will also need to implement.

The SquareRootArgumentException class extends the java.lang.IllegalArgumentException and includes only a single constructor which takes a String parameter named "msg" and invokes the super class's constructor with that parameter as an argument.

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. Your SquareRootArgumentException class can be included in this file if you do not declare the class as public.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!