Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use the program (Listing 12.7 and 12.8, Page 463-464) and rewrite the program to do create RectangleWithException and TestRectangleWithException. Your exception should catch the input

Use the program (Listing 12.7 and 12.8, Page 463-464) and rewrite the program to do create RectangleWithException and TestRectangleWithException. Your exception should catch the input of any negative values for the sides of the rectangle.

12.7 listing

public class CircleWithException {

/** The radius of the circle */

private double radius;

/** The number of the objects created */

private static int numberOfObjects = 0;

/** Construct a circle with radius 1 */

public CircleWithException() {

this(1.0);

}

/** Construct a circle with a specified radius */

public CircleWithException(double newRadius) {

setRadius(newRadius);

numberOfObjects++;

}

/** Return radius */

public double getRadius() {

return radius;

}

/** Set a new radius */

public void setRadius(double newRadius)

throws IllegalArgumentException {

if (newRadius >= 0)

radius = newRadius;

else

throw new IllegalArgumentException(

"Radius cannot be negative");

}

/** Return numberOfObjects */

public static int getNumberOfObjects() {

return numberOfObjects;

}

/** Return the area of this circle */

public double findArea() {

return radius * radius * 3.14159;

}

}

12.8 listing

public class TestCircleWithException {

public static void main(String[] args) {

try {

CircleWithException c1 = new CircleWithException(5);

CircleWithException c2 = new CircleWithException(-5);

CircleWithException c3 = new CircleWithException(0);

}

catch (IllegalArgumentException ex) {

System.out.println(ex);

}

System.out.println("Number of objects created: " +

CircleWithException.getNumberOfObjects());

}

}

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

Databases DeMYSTiFieD

Authors: Andy Oppel

2nd Edition

0071747990, 978-0071747998

More Books

Students also viewed these Databases questions

Question

2.1 Discuss what ethics means and the sources of ethical guidance.

Answered: 1 week ago

Question

8 What personal development is elearning good at providing?

Answered: 1 week ago

Question

7 What are the principles of action learning?

Answered: 1 week ago