Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is the code that is needed: public class Circle { // Student Starter code private double radius; // circle radius /** * Constructor -

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Here is the code that is needed:
public class Circle { // Student Starter code
private double radius; // circle radius
/**
* Constructor - Create a new circle
* @param inRadius radius of the circle
*/
public Circle(double inRadius ) {
// implement body here
}
/**
* Return the radius of the circle
* @return radius of the circle
*/
public double getRadius() {
return radius;
}
/**
* set the radius
* @param newRadius new radius of the circle
*/
public void setRadius(double newRadius) {
// implement body here
}
/**
* Compute and return the area of the circle
* @return the area of the circle
*/
public double area() {
return Math.PI * radius * radius;
}
/**
* Stretches circle size by multiplying
* the radius by the factor provided.
* @param factor stretch factor
*/
public void stretchBy(double factor ) {
// implement body here
}
/**
* Return a string representation of a circle.
* @return a string representing this circle
*/
public String toString() {
// implement body here
}
}
public class TestCircle {
public static void main(String [] args) {
// read a radius of the circle from command line
double radius = Double.parseDouble(args[0]);
// Instantiate a Circle object
Circle aCircle = new Circle(radius);
// Print current status of the circle
System.out.println(aCircle);
}
}
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 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. Atry/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. Atest 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 Page 1 of4 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-ine argument and uses it to create a Circle of the specified radius. Next, print out the radius value using the toString o method of the Circle class. t before Handling Exc (TestCircle.java) Usersphlics Documents key) xcept iont inithreadiniaine: peEacers"; TestCircle festcireiainestcirele aa: 12) 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 0 method of the Circle class. before Handling Exceptions (TestCircle.java) ersiphlies DeeumentsNkey>Java TestCirele 5 DocumentsU3Kapayac Teston:hape&ception cept ion nain Java. lang strig: at sun.misc.Float ingDecinal.readJavFormat String(Float ingDecinal Java:2 Le(Fl imal. java: 1 avuhttesteerea3* Java 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 (TestCi rel ejava) to another file named TestcircleA.java. Set up catch blocks for the following exceptions: Exception, ShapeException, ArrayIndexoutorBoundsException, 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. Handling Exceptions(TestCircleA.java) icsVocunentsUey)java TestCircleA 20 tUsersNebli ArrayindexOut tstkey> java TestCireleA aeceon docunent.xav TestCireleA -10 sers phlics Users phlles Documentskey) Java TestCircleA fubar Pape 2 of 4 Signature Have the instructor or TA sign here when Exercise 1 works correctly. Exercise 2 - Applications Using Exception Handling (5 points) 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 tryicatch for the following exceptions: Exception, ShapeException, InputMisnatchException, NoSuchElenentException 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: IGRASP 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. ut after Handling Exce key>java TestCircle tererge radius ntererseireles radiusent skey>Java TestCire les routMismatchExcept Lon oceurred.. :Users phlic nputMismatc User1\ph liesDocument sSke)ava sNkey>java TestCircleb TestCireleB :\Users\phics kediava TestCircleb STE-120 Page 3 o4 Part 2: Repeating Prompt (TestcircleC.java) Copy your program (TestCircleB.java) to another file named TestCircleC.java. Put the trylcatch 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 Part 2: Repeating Prompt (Testcirclec.java) Copy your program (TestCircl eB. j ava) to another file named TestCirclec. j ava. Put the trylcatch 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. progran key> java TestCircleC t MismatchExceptio radius: Input Mismat ehException eecurred. er a circle radius: nter rcle le radius: Users\phlies\Documents\key>java nter a circle radius: TestCirelec hape Exception occ nputHintchE ceation ersurE a circle radius : entered Progran terminated Users phlicsDocumentskey> Signature Have your instructor or TA sign here when Exercise 2 works correctly. STE-120 Page 4 of 4

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_2

Step: 3

blur-text-image_3

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

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions

Question

' Which areas are you most dependent on to get your work done?

Answered: 1 week ago