Java Program to calculate Area of Circle

You can calculate the area of a circle in Java by just writing a class and a method. All you need to know is the formula to calculate the area of circle and trick to get input from the user in Java. If you know these two already than the calculating area of a circle is very easy. Since every program must have a class in Java, we need to create a class. I have created a class called Circle for our examples purpose. Now, since the execution of Java program starts from the main method, I have provided a public static void main() method in our program. This single method is enough to put all the code required for this program e.g. getting input from the user, calculating area and displaying area of a circle in the console. But, for better coding experience purpose, we'll just create a method to calculate the area of a circle.

It would have been better if we would have followed object-oriented programming by its true spirit and made an instance method in circle class called area with the radius as an instance variable, but for the sake of simplicity I have created a static method and I have passed radius to it.

I had to make this method static because you cannot call a non-static method from a static context in Java.

In order to take input I have used the Scanner class, even though you can use other ways like using a Reader or InputStream, but Scanner provides lots of utility method to read any kind of data e.g. int, float, String etc, it's preferred class to read input from the console. See that article for more details on Scanner class in Java.





How to calculate Area of Circle in Java

Here is our sample Java program to calculate the area of a circle. As I have explained, we have used a static method to get the job done.  You can use the same technique to calculate the area of rectangle or square in Java, just change the formula.

How to calculate area of circle in Java
You can see from above diagram that how the formula for calculating the area of a circle arrives. It's like converting the circle to a rectangle and then calculating are of a rectangle.  It's the interesting theory for Maths lovers.

For Java developers, I recommend reading Core Java Volume 1 - Fundamentals by Cay S. Horstmann for learning Java programming from scratch. This book provides comprehensive yet readable coverage of both Java language and API. One of the better books on Java and the good part is it also covers Java SE 8.

Java Program to calculate area of circle



Java Program to find area of Circle
import java.util.Scanner;

/*
 * Java Program to calculate area of circle
 */
public class Circle {

    public static void main(String args[]) {

        // creating scanner to accept radius of circle
        Scanner scanner = new Scanner(System.in);
        System.out.println("Welcome in Java program to calculate area of circle");
        System.out.println("Formula for calculating area of circle is 'PI*R^2' ");

        System.err.println("Please enter radius of circle:");
        float radius = scanner.nextFloat();

        double area = area(radius);

        System.out.println("Area of Circle calculate by Java program is : "
                + area);

        scanner.close();
    }

    public static double area(float radius) {
        double interest = Math.PI * radius * radius;
        return interest;
    }
}

Output
Welcome in Java program to calculate area of circle
Formula for calculating area of circle is 'PI*R^2' 
Please enter radius of circle:
2
Area of Circle calculate by Java program is : 12.566370614359172


That's all about how to calculate the area of a circle in Java. In order to write this program, you need to know the formula for calculating circle and how to get input from the user. Rest of them are pretty basic, we have just written a static method so that we can call it directly from main() and we have used familiar System.out.println() method to print answer in the console.

Other Java Programs for Practice
  • Write a Java Program to check if a number is a palindrome or not? (solution)
  • Program to check if a number is an Armstrong number or not? (solution)
  • Write a program to check if a number is even or odd in Java? (solution)
  • How to print Fibonacci series in Java using recursion? (solution)
  • How to print Alphabets in upper and lower case in Java? (solution)
  • Write a program to reverse a String in Java? (answer)
  • Java Program to calculate Simple Interest? (answer)


No comments:

Post a Comment