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());// } }
I have observed carefully your code . It is perfectly fine for handling the above mentioned exception and the logic for providing correct number of instances is also correct . The issue is with the TesterCircle class inside the try block . Fisrt statement for creating object i.e new CircleWithException(0); this will not throw any error and simple change the value of numberOfObject variable 0 to 1
Your second statement new CircleWithException(-5); This will throw an exception as this is negative radius and hence the value of numberOfObject variable will not change , it will remain 1 only.
Now , your third statement new CircleWithException(5); , this will not get executed ever as the statement above this new CircleWithException(-5) , has thrown an exception . Hence this will not get execute ever and will the numberOfObject will be 1 only .. That is why , it is showing the value 1 always.
Reason is , when any exception is thrown then the complier ignores all the statement below that statement where the exception was thrown and control get passed to the catch block .
if you change the order of your statements
CircleWithException c1 = new CircleWithException(0); CircleWithException c3 = new CircleWithException(-5); CircleWithException c2 = new CircleWithException(5);
to
CircleWithException c1 = new CircleWithException(0); CircleWithException c3 = new CircleWithException(5); CircleWithException c2 = new CircleWithException(-5);
then your code will work fine and will show output as 2 which is the correct expected answer.
Below is the image for cross testing