Java program to find area and perimeter of a circle using class

In this java program, we will read radius of a circle and find their area and perimeter, this program will be implementing using class and objects. Here value will be reading and printing through class methods.

In this example we will read radius of a circle and then calculate area, perimeter of a circle. We will create a class to find the area and perimeter.

In this program we will use Math.PI to use value of PI.

/* Java program to create class to calculate
area and perimeter of circle. */

import java.util.*;

class AreaOfCircle {
  private float radius = 0.0f;
  private float area = 0.0f;
  private float perimeter = 0.0f;

  //function to read radius
  public void readRadius() {
    //Scanner class - to read value from keyboard
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter radius:");
    radius = sc.nextFloat(); //to read float value from keyboard
  }

  //funtction to calculate area
  //return value - will return calculated area
  public float getArea() {
    area = (float)Math.PI*radius*radius;
    return area;
  }

  //funtction to calculate perimeter
  //return value - will return calculated perimeter
  public float getPerimeter() {
    perimeter = 2 * (float)Math.PI * radius;
    return perimeter;
  }
}
public class circle {
  public static void main(String[] s) {
    AreaOfCircle area = new AreaOfCircle();

    area.readRadius();
    System.out.println("Area of circle:" + area.getArea());
    System.out.println("Perimeter of circle:" + area.getPerimeter());
  }
}

Output

Compile: javac circle.java
Run: java circle

Output: Enter radius:15.50 Area of circle:754.385 Perimeter of circle:97.340004

Java Class and Object Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.