Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions