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


The Perimeter_Circle class contains a main method which creates an object of the PerimeterOfCircle class and uses it to calculate the perimeter of a circle. The program first calls the readRadius method of the PerimeterOfCircle 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 getPerimeter method of the PerimeterOfCircle object, which calculates the perimeter of the circle using the formula peri = 2 * pi * r, where pi is the mathematical constant and r is the radius of the circle. The calculated perimeter is then returned and printed to the console using System.out.println.

The PerimeterOfCircle class contains two instance variables, rad and peri, 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 getPerimeter method calculates the perimeter of the circle using the formula peri = 2 * pi * r and returns the calculated value. The peri instance variable is updated with the calculated value.

Source Code

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

Output

Enter the Radius : 23.4
Perimeter of Circle : 147.02654