Wednesday, September 13, 2023

Java Program to calculate Area of Circle, Example

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 the circle and the trick to get input from the user in Java. If you know these two already then 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 the purpose of our example. Now, since the execution of the 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 the area of a circle in the console. But, for better coding experience purposes, 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 methods 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 the 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 a rectangle or square in Java, just change the formula.

You can see from the 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 an 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.

How to calculate area of circle in Java




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 circles and how to get input from the user. The 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 the familiar System.out.println() method to print answers in the console.


Other Java Programs for Practice
  • Write a program to reverse a String in Java? (answer)
  • How to print the Fibonacci series in Java using recursion? (solution)
  • How to print Alphabets in upper and lower case in Java? (solution)
  • Java Program to calculate Simple Interest? (answer)
  • 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)
Thanks for reading this article so far. If you have any questions or feedback about this Java Programming exercise then please ask.  Btw, what is your favorite coding exercise? prime number, palindrome or this one?

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.