In: Computer Science
Revise this code to handle exceptions and provide correct total number of valid objects.
(1)
/**
* class: CircleWithException
* description: This program creates CircleWithException specification by using an instance variable/data/attribute named
* radius.
* Then, also we will create a static variable named numberOfObjects to count number of object when you create in a tester program.
* In this program, you will learn how you declare an exception and define the exception.
* This is a part of checked exception process.
* Regarding the checked exception, besides run time error exceptions, all other exceptions declared must be checked when
* a caller calls the method with a checked exception.
*/
public class CircleWithException {
private double radius;
private static int numberOfObject = 0;
//1
public CircleWithException(){
this(1.0);
}
//2
public CircleWithException(double rad){
//this.radius = rad;
setRadius(rad);
numberOfObject++;
}
public double getRadius() {
return radius;
}
/**
* method: setRadius
* @param radius
* @throws IllegalArgumentException
*
* description: setRadius method throws an exception if the radius is negative.
* this case, this method will throw an IllegalArgumentException.
* throws is the keyword to indicate that this method throw this type of exception.
* the caller must MUST MUST handle the exception using a try-catch block or we put "throws any exception from
* the main method.
* We can throw either checked or unchecked exceptions.
*
* The keyword throws will allow the compiler to help you write a code to handle this type of error.
* BUT it does not prevent the abnormal termination of the problem. With the help of the throws keyword,
* you can provide the information to the caller of the method about the type of the exceptions the method might throw.
*/
public void setRadius(double radius) throws IllegalArgumentException { //declare the IllegalArgumentException
if(radius >= 0)
this.radius = radius;
else
throw new IllegalArgumentException("Radius cannot be a negative number. ");
}
//eureka
public static int getNumberOfObject() {
return numberOfObject;
}
//if you see static then it is a class method. a class method is also called a static method.
//static method is called by the classname when it is called outside of the class which defined that method.
//inside of CircleWithException, you call getNumberOfObject()
//outside of CircleWithException, you call getNumberOfObject by using this format: CircleWithException.getNumberOfObject()
//if you do not include static, then it is an instance method. it is also called non-static method.
//with an example of area method, if you create an object name c1, then you call area() by the object named c1.
//c1.area()
public double area() {
return radius * radius * Math.PI;
}
}
(2)
public class TesterCircle {
public static void main(String[] args) {
try {
CircleWithException c1 = new CircleWithException(0);
CircleWithException c3 = new CircleWithException(-5);
CircleWithException c2 = new CircleWithException(5);
}
catch(IllegalArgumentException e){
System.out.println(e);
}
System.out.println("Number of Objects created: " + CircleWithException.getNumberOfObject());//
}
}
Thanks for the question. You only need to change the TesterCircle. Each line of creating an object of CircleWithException type needs to be enclosed in a separate try -catch block.
Now the total number of objects is coming as 2 and not 1 as what it was happening previously.
===============================================================================
public class TesterCircle {
public static void main(String[] args) {
try {
CircleWithException c1 = new CircleWithException(0);
}
catch(IllegalArgumentException e){
System.out.println(e);
}
try {
CircleWithException c2 = new CircleWithException(5);
}
catch(IllegalArgumentException e){
System.out.println(e);
}
try {
CircleWithException c3 = new CircleWithException(-5);
}
catch(IllegalArgumentException e){
System.out.println(e);
}
System.out.println("Number of Objects created: " + CircleWithException.getNumberOfObject());//
}
}
====================================================================
