Java Top

I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE COURSE

1. Overview

In this quick tutorial, we’ll illustrate how to calculate the area of a circle in Java.

We’ll be using the well-known math formula: r^2 * PI.

2. A Circle Area Calculation Method

Let’s first create a method that will perform the calculation:

private void calculateArea(double radius) {
    double area = radius * radius * Math.PI;
    System.out.println("The area of the circle [radius = " + radius + "]: " + area);
}

2.1. Passing the Radius as a Command Line Argument

Now we can read the command line argument and calculate the area:

double radius = Double.parseDouble(args[0]);
calculateArea(radius);

When we compile and run the program:

java CircleArea.java
javac CircleArea 7

we’ll get the following output:

The area of the circle [radius = 7.0]: 153.93804002589985

2.2. Reading the Radius from a Keyboard

Another way to get the radius value is to use input data from the user:

Scanner sc = new Scanner(System.in);
System.out.println("Please enter radius value: ");
double radius = sc.nextDouble();
calculateArea(radius);

The output is the same as in the previous example.

3. A Circle Class

Besides calling a method to calculate the area as we saw in section 2, we can also create a class representing a circle:

public class Circle {

    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    // standard getter and setter

    private double calculateArea() {
        return radius * radius * Math.PI;
    }

    public String toString() {
        return "The area of the circle [radius = " + radius + "]: " + calculateArea();
    }
}

We should note a few things. First of all, we don’t save the area as a variable, since it is directly dependent on the radius, so we can calculate it easily. Secondly, the method that calculates the area is private since we use it in the toString() method. The toString() method shouldn’t call any of the public methods in the class since those methods could be overridden and their behavior would be different than the expected.

We can now instantiate our Circle object:

Circle circle = new Circle(7);

The output will be the, of course, the same as before.

4. Conclusion

In this short and to-the-point article, we showed different ways of calculating the area of a circle using Java.

As always, complete source code can be found over on GitHub.

Java bottom

I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE LESSONS

2
Leave a Reply

avatar
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Loredana CrusoveanuGuppyface Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Guppyface
Guest
Guppyface

Just started learning Java and I appreciate the varied difficulties you provide.

I couldn’t get it to work until I found the main package on your Github repo, which did require a bit of digging.

Can I post the git repo here? Let me try:

https://github.com/eugenp/tutorials/tree/master/core-java/src/main/java/com/baeldung/area/circle

Thanks again for the tutorial.

Loredana Crusoveanu
Editor

Hello,

Sure, thanks. We usually only post the link to the module that contains the code samples, as the package can be changed easier in future updates. You can also use GitHub’s search feature to look for a specific class.

Cheers.