calculate area and perimeter of a circle In JAVA

This Tutorial is very important for JAVA Beginner.The given example will teach you the method for preparing a program to calculate the area and perimeter of a circle In JAVA.

First of all name a class as “AreaofCircle” under Java I/O package and define and integer c=o, which is the radius of the circle.

Now use try exception to handle errors and other exceptional events.

As we have to input the value of radius here create a buffered class with an object as ‘reader1 ‘.

This create a buffering character input stream that uses a default sized input buffer.

The InputStreamReader here works as a translator that converts byte stream to character stream.

Now type message that “Enter radius of circle” in the System.out.println method. 

Now use the parseInt() method of the Integer class in order to convert from external numeric format to internal format.

Now create the Math class in which all the mathematical functions are defined. This Math class can be imported from the java.lang.* package.

Write the program for both the cases: radius and perimeter. 

Before ending the program use the Catch mechanism that detects and catch user input errors.

In the end compile and run the program and enter your desired value as radius for calculating the radius and perimeter of the circle.

Here is the code of the program:

import java.io.*;
class AreaofCircle{
public static void main(String[] args){
int c=0;
try{
BufferedReader reader1 = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter Radius of Circle : “);
c = Integer.parseInt(reader1.readLine());
double area = java.lang.Math.PI*c*c;
System.out.println(“Area of Circle : “+area);
double perimeter =2*java.lang.Math.PI*c ;
System.out.println(“Perimeter of Circle : “+perimeter);
}
catch(Exception e){
System.out.println(“Error : “+e);
}
}
}

Enjoy Java Code.