[Solved]Iste 120 Lab 14 Exceptions Exercise 1 Exception Handling Concept 5 Points Part 1 Throw Exc Q37172286





Please help me solve this(java) I really need help!!
ISTE-120 Lab 14: Exceptions Exercise 1 – Exception Handling Concept (5 points) Part 1: Throw Exceptions (Checked Exceptions) (Circle.java) Open the file Circle.java in today’s downloads. Add the body to the Constructor that is already present in the ci 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. Atry/catch block is not needed here, but the method signature will require modification. rcle class. Check to see if the radius parameter entered is Similarly, in the setRadius() method, add the implementation to the body such that if the newRadius parameter is less than or equal to o.o, 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 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 LAB 14 ISTE-120 Page 1 of 4 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 today’s 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. 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) CYWi stem321cmd.exe C: NUsers ph1ics Documents key>java TestCircle 5 Circle: 5. C:Usersph l icsDocumentskey〉java Tes t Circle -10 Exception in thread “main” ShapeException: ShapeException occurred… at Çircle. <init>(Circlejava: 25) at TestCircle.main TestCircle. java: 12) C: Users Nphlics Documents key> java TestCircle fubar Exception in thread “main” java. lang. NumberFormatException: For input string: “f ubar’ at sun.misc.FloatingDecimal.readJavaFormatString (FloatingDecimal. java: 20 43) at sun.misc. Float ingDecimal.parseDouble (FloatingDecimal. java: 110) at java.lang. Double.parseDouble (Double. java: 538 at TestCircle.main(TestCircle. java: 9) C: Users phlics Documents key> 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) C:Windows system321cmd.exe C: AUsers ph1ics Documents key>java TestCircleA 20 Circle: C: Users phlics Documents key> java TestCircleA ArrayIndexOut Of Bounds Exception occurred.. . 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-Q Sample Output after Handling Exceptions m32cmd.exe CWindo C: Users phlics Documents Nkey>java TestCircleB Enter a circle radiuusTS Circle: 15.0 C: Users phlics Documents Nkey>java TestCircleB Enter a circle radius 12.aa InputMismatchException occurred. . . C: XUsers phlics Documents key> java TestCircleB Enter a circle radius fubar InputMismatchExcept ion occurred.. . C:AUsers hlcs Documents key> java TestCircleB Enter a circle radius-25 Shape E×ception ccurred C: Users ph1ics Documents key>java TestCircleB Enter a circle radius: CTRL+C Program stops… C:NUsers phlics Documents key 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 terminated.” Try to be efficient… use as few statements as possible in the try block – progranm Sample Output after Handling Exceptions CWindo C: Users phlics Documents key>java TestCircleC Enter a circle radius: 12.aa nputMis matchException occurred… Enter a circle radius fubar InputMismatchException occurred… Enter a circle radius -20 Shape Exception occurred… Enter a circle radius 25 Circle: 25.0 C: Users phlics Documents key>java TestCircleC Enter a circle radius-10 Shape Exception occurred… Enter a circle radius: 38. ww InputMismatchException occurred.. Enter a circle radius: CTRL+C entered-Program terminated C:NUsers phlics Documentskey Show transcribed image text ISTE-120 Lab 14: Exceptions Exercise 1 – Exception Handling Concept (5 points) Part 1: Throw Exceptions (Checked Exceptions) (Circle.java) Open the file Circle.java in today’s downloads. Add the body to the Constructor that is already present in the ci 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. Atry/catch block is not needed here, but the method signature will require modification. rcle class. Check to see if the radius parameter entered is Similarly, in the setRadius() method, add the implementation to the body such that if the newRadius parameter is less than or equal to o.o, 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
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 LAB 14 ISTE-120 Page 1 of 4 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 today’s 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.
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) CYWi stem321cmd.exe C: NUsers ph1ics Documents key>java TestCircle 5 Circle: 5. C:Usersph l icsDocumentskey〉java Tes t Circle -10 Exception in thread “main” ShapeException: ShapeException occurred… at Çircle. (Circlejava: 25) at TestCircle.main TestCircle. java: 12) C: Users Nphlics Documents key> java TestCircle fubar Exception in thread “main” java. lang. NumberFormatException: For input string: “f ubar’ at sun.misc.FloatingDecimal.readJavaFormatString (FloatingDecimal. java: 20 43) at sun.misc. Float ingDecimal.parseDouble (FloatingDecimal. java: 110) at java.lang. Double.parseDouble (Double. java: 538 at TestCircle.main(TestCircle. java: 9) C: Users phlics Documents key> 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) C:Windows system321cmd.exe C: AUsers ph1ics Documents key>java TestCircleA 20 Circle: C: Users phlics Documents key> java TestCircleA ArrayIndexOut Of Bounds Exception occurred.. .
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-Q Sample Output after Handling Exceptions m32cmd.exe CWindo C: Users phlics Documents Nkey>java TestCircleB Enter a circle radiuusTS Circle: 15.0 C: Users phlics Documents Nkey>java TestCircleB Enter a circle radius 12.aa InputMismatchException occurred. . . C: XUsers phlics Documents key> java TestCircleB Enter a circle radius fubar InputMismatchExcept ion occurred.. . C:AUsers hlcs Documents key> java TestCircleB Enter a circle radius-25 Shape E×ception ccurred C: Users ph1ics Documents key>java TestCircleB Enter a circle radius: CTRL+C Program stops… C:NUsers phlics Documents key
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 terminated.” Try to be efficient… use as few statements as possible in the try block – progranm Sample Output after Handling Exceptions CWindo C: Users phlics Documents key>java TestCircleC Enter a circle radius: 12.aa nputMis matchException occurred… Enter a circle radius fubar InputMismatchException occurred… Enter a circle radius -20 Shape Exception occurred… Enter a circle radius 25 Circle: 25.0 C: Users phlics Documents key>java TestCircleC Enter a circle radius-10 Shape Exception occurred… Enter a circle radius: 38. ww InputMismatchException occurred.. Enter a circle radius: CTRL+C entered-Program terminated C:NUsers phlics Documentskey
Expert Answer
Answer to ISTE-120 Lab 14: Exceptions Exercise 1 – Exception Handling Concept (5 points) Part 1: Throw Exceptions (Checked Excepti… . . .
OR

