Write a Java program to Find area of a circle using class


The Area_Circle class contains a main method which creates an object of the AreaOfCircle class and uses it to calculate the area of a circle. The program first calls the readRadius method of the AreaOfCircle object, which prompts the user to enter the radius of a circle and reads in the user's input using a Scanner. Then, the program calls the getArea method of the AreaOfCircle object, which calculates the area of the circle using the formula area = pi * r * r, where pi is the mathematical constant and r is the radius of the circle. The calculated area is then returned and printed to the console using System.out.println.

The AreaOfCircle class contains two instance variables, rad and area, which are initialized to 0.0f. The readRadius method uses a Scanner object to prompt the user to enter the radius of a circle and reads in the user's input. The getArea method calculates the area of the circle using the formula area = pi * r * r and returns the calculated value. The area instance variable is updated with the calculated value.

Source Code

import java.util.*;
public class Area_Circle
{
	public static void main(String args[])
	{
		AreaOfCircle area = new AreaOfCircle();
 
		area.readRadius();
		System.out.println("Area of Circle : " + area.getArea());
	}
}
class AreaOfCircle
{
	private float rad = 0.0f;
	private float area = 0.0f;
 
	public void readRadius()
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the Radius : ");
		rad = input.nextFloat();
	}
 
	public float getArea()
	{
		area = (float)Math.PI*rad*rad;
		return area;
	}
}
 

Output

Enter the Radius : 12
Area of Circle : 452.38934