Q:

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

belongs to collection: Java Class and Object Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Class and Object Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to count digits of a number using cla... >>
<< Java program to print message using class...