Wednesday, March 28, 2018

AREA OF A CIRCLE USING EXCEPTION IN JAVA

A simple program that I wrote to compute the area of the circle with exception handling capabilities.

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.



Sample Program Output


Program Listing

AreaCircle.java


package exceptiondemo;

import java.util.Scanner;
import java.util.InputMismatchException;
import java.text.DecimalFormat;

/**
 * NetBeans IDE 8.2
 * @author Mr. Jake R. Pomperada
 * March 20, 2018  Tuesday
 * Bacolod City, Negros Occidental
 */
public class AreaCircle {

    public static final DecimalFormat TWO_DECIMAL = new DecimalFormat(".##");
     
    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
      
               final double PI = 3.14159;
               boolean goodData = false;
                
              while(!goodData) {
              try {
                System.out.print("AREA OF A CIRCLE USING EXCEPTION");
                System.out.println("\n");
                System.out.print("What is the area?  : ");
double radius=sc.nextDouble();
                
                 double area = (PI * radius * radius);
                
                System.out.println("\n");
                System.out.println("The Area of Circle is " 
                        + TWO_DECIMAL.format(area) +".");
                System.out.println("\n");
goodData = true;
       
              } catch(InputMismatchException e) {
            
                sc.next();
                System.out.println("You Entered a Bad Data." );
                System.out.println("Please Try Again." );
                System.out.println("\n");
                }
            }  // while loop end
System.out.print("\t END OF PROGRAM");
        System.out.println("\n");
     }
}



No comments:

Post a Comment