Question: Exercise 1 Exception Handling Concept (5 points) Part 1: Throw Exceptions (Checked Exceptions) (Circle.java) Open the file Circle.java in todays downloads. Add the body to

Exercise 1 Exception Handling Concept (5 points)

Part 1: Throw Exceptions (Checked Exceptions) (Circle.java)

Open the file Circle.java in todays downloads. Add the body to the Constructor that is already present in the Circle class. Check to see if the radius parameter entered is greater than 0.0 or not (i.e. is the circle valid?) If the value of the radius is less than zero, throw an Exception. Otherwise, proceed as normal by initializing the instance variable radius to the parameter entered. A try/catch block is not needed here, but the method signature will require modification.

Similarly, in the setRadius() method, add the implementation to the body such that if the newRadius parameter is less than or equal to 0.0, throw an Exception.

Otherwise, set the radius to this newRadius value.

Again, in the stretchBy() method, add the implementation to the body such that if the factor parameter is less than or equal to 0.0, throw an Exception. Otherwise, change the radius by multiplying it by the factor.

Finally, write the toString() method to return the string Circle: with the value of the radius appended to the end. Make sure the class compiles. A test class will be necessary to run the program.

Part 2: User-Defined Exception Class (ShapeException)

Create a file called ShapeException.java with the class ShapeException

which extends the Exception class

This ShapeException class has only ONE method, the constructor that takes ONE parameter, a String message stating the nature of the exception. Inside the constructor, call the parent's one-parameter constructor with the message as a parameter. Note: this is a very short class!

Now change the Circle class by replacing Exception with ShapeException.

When a ShapeException is thrown (throw new ShapeException(""), include

an appropriate message as the argument to the constructor. Compile both classes. Again, a test class will be required.

Part 3: Handle the Exceptions (TestCircle.java)

Use TestCircle from todays downloads.

This program reads in the first command-line argument and uses it to create a Circle of the specified radius. Next, print out the radius value using the toString() method of the Circle class.

Sample Output before Handling Exceptions (TestCircle.java)

 Exercise 1 Exception Handling Concept (5 points) Part 1: Throw Exceptions

Now it's time to use try/catch to prevent the Exceptions shown in the above sample output from ever making it to the terminal.

Copy the above program (TestCircle.java) to another file named TestCircleA.java. Set up catch blocks for the following exceptions: Exception, ShapeException, ArrayIndexOutOfBoundsException, and NumberFormatException. Pay attention to the order of the catch blocks. For each exception, it is OK to print out a message saying what kind of Exception occurred.

Sample Output after Handling Exceptions(TestCircleA.java)

(Checked Exceptions) (Circle.java) Open the file Circle.java in todays downloads. Add the

Signature:

Have the instructor or TA sign here when Exercise 1 works correctly.

Exercise 2 Applications Using Exception Handling (5 points)

Part 1: Catching Exceptions when using Scanner (TestCircleB.java)

Copy TestCircleA.java to a file named TestCircleB.java.

Modify the program to print a prompt for the input value and to read in the double input value using the Scanner class. Do not test the input using the hasNextInt method.

Modify your program to catch the exceptions thrown by the Scanner class. Set up try/catch for the following exceptions: Exception, ShapeException, InputMismatchException, NoSuchElementException.

When the exception for entering the Control-C occurs, print the message CTRL+C entered Program terminated. For all other exceptions, it is sufficient to print out a message saying what kind of exception occurred.

NOTE: jGRASP catches a Control-C and does not pass it on to your program. You must execute your program from a command prompt window to see if your program catches the Control-C properly. Run your program and when it prompts for a value, press Control-C.

Sample Output after Handling Exceptions

body to the Constructor that is already present in the Circle class.

Part 2: Repeating Prompt (TestCircleC.java)

Copy your program (TestCircleB.java) to another file named TestCircleC.java.

Put the try/catch in a loop so that the user gets to re-enter the input until it is a valid integer.

Modify your program to catch the exceptions thrown by the Scanner class, print a message, and then allow the user to re-enter the input. When valid input is entered, print the radius of the circle and have the program terminate.

Notes:

Use a boolean variable to determine when to exit the loop.

When a Control-C is entered, print a message Control-C entered program terminated.

Try to be efficient use as few statements as possible in the try block.

Sample Output after Handling Exceptions

Check to see if the radius parameter entered is greater than 0.0

Signature:

Have your instructor or TA sign here when Exercise 2 works correctly.

C: Windows system321cmd.exe C: Users ph1ics Documentskey>java TestCircle S Circle: 5.0 Ca Users phlics Documents key> jaya TestCircle-10 Exception in thread "main" ShapeException: ShapeException occurred at Circle.Kinit CCircle.java: 25) at TestCircle.main(TestCircle. java: 12) C: UsersVphlics Documents Nkey> java TestCircle fubar Except ion in thread "main" java. lang.NumberFormat Exception:For input string: "f ubar at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal. java: 2 43) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java: 110) at java. lang. Double.parseDouble (Double. java: 538 at TestCircle.main(TestCircle. java: 9) C: Users phlicsDocuments key> C: Windows system321cmd.exe C: Users ph1ics Documentskey>java TestCircle S Circle: 5.0 Ca Users phlics Documents key> jaya TestCircle-10 Exception in thread "main" ShapeException: ShapeException occurred at Circle.Kinit CCircle.java: 25) at TestCircle.main(TestCircle. java: 12) C: UsersVphlics Documents Nkey> java TestCircle fubar Except ion in thread "main" java. lang.NumberFormat Exception:For input string: "f ubar at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal. java: 2 43) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java: 110) at java. lang. Double.parseDouble (Double. java: 538 at TestCircle.main(TestCircle. java: 9) C: Users phlicsDocuments key>

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!