Use the program (Listing 12.7 and 12.8, Page 463-464) and rewrite the program to
ID: 3740402 • Letter: U
Question
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());
}
}
Explanation / Answer
Hi, here are the two files:
RectangleWithException.java:
public class RectangleWithException {
/** The length and breadth of the rectangle */
private double length;
private double breadth;
/** The number of the objects created */
private static int numberOfObjects = 0;
/** Construct a rectangle with length and breadth 1 */
public RectangleWithException() {
this(1.0, 1.0);
}
/** Construct a rectangle with specified length and breadth*/
public RectangleWithException(double newLength, double newBreadth) {
setLength(newLength);
setBreadth(newBreadth);
numberOfObjects++;
}
/**
* @return the length
*/
public double getLength() {
return length;
}
/**
* @param length the length to set
*/
public void setLength(double newLength) throws IllegalArgumentException{
if (newLength >= 0)
length = newLength;
else
throw new IllegalArgumentException(
"Length cannot be negative");
}
/**
* @return the breadth
*/
public double getBreadth() {
return breadth;
}
/**
* @param breadth the breadth to set
*/
public void setBreadth(double newBreadth) {
if (newBreadth >= 0)
length = newBreadth;
else
throw new IllegalArgumentException(
"Breadth cannot be negative");
}
/** Return numberOfObjects */
public static int getNumberOfObjects() {
return numberOfObjects;
}
/** Return the area of this rectangle */
public double findArea() {
return length * breadth;
}
}
TestRectangleWithException.java:
public class TestRectangleWithException {
public static void main(String[] args) {
try {
RectangleWithException c1 = new RectangleWithException(5,5);
RectangleWithException c3 = new RectangleWithException(0,0);
RectangleWithException c4 = new RectangleWithException(5,-5);
RectangleWithException c2 = new RectangleWithException(-5,-5);
}
catch (IllegalArgumentException ex) {
System.out.println(ex);
}
System.out.println("Number of objects created: " +
RectangleWithException.getNumberOfObjects());
}
}
Feel free to ask in case of question(s), if any :)
Alse, rate if this solves your query :)
Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.