Open In App

Java Program to Find Area of circle Using Method Overloading

A circle is a simple shape consisting of all the points in the plane that are equidistant from a point known as the center of the circle. In this article, we will learn how to find the area of the circle using the method overloading.

Terminology:



Area of the Circle

The area of the circle is the product of the square of the radius of the circle and the value of PI. We can simply calculate the area of the circle using the following formula:



Formula:

Area of the circle:  A = π * r2

Here, r is the radius of the circle.

Formula:

Area of the circle:  A = (π / 4) * d2

Here, r is the radius of the circle.

Note: The value of PI in java is 3.141592653589793. 

Below is the implementation of the above approach:

Example 1:




// Java program to find the area of
// the circle using Method Overloading
 
import java.io.*;
 
class Circle {
 
    static final double PI = Math.PI;
 
    // Overloaded Area() function to
    // calculate the area of the circle.
    // It takes one double parameter
    void Area(double r)
    {
        double A = PI * r * r;
 
        System.out.println("Area of the circle is :" + A);
    }
 
    // Overloaded Area() function to
    // calculate the area of the circle.
    // It takes one float parameter
    void Area(float r)
    {
        double A = PI * r * r;
 
        System.out.println("Area of the circle is :" + A);
    }
}
 
class GFG {
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Creating object of Circle class
        Circle obj = new Circle();
 
        // Calling function
        obj.Area(5);
        obj.Area(2.5);
    }
}

Output
Area of the circle is :78.53981633974483
Area of the circle is :19.634954084936208

Time Complexity: O(1)

Auxiliary Space: O(1)

Example 2:




// Java program to find the area of
// the circle when the diameter is given
// using Method Overloading
 
import java.io.*;
 
class Circle {
 
    static final double PI = Math.PI;
 
    // Overloaded Area() function to
    // calculate the area of the circle.
    // It takes one double parameter
    void Area(double D)
    {
        double A = (PI / 4) * D * D;
 
        System.out.println("Area of the circle is :" + A);
    }
 
    // Overloaded Area() function to
    // calculate the area of the circle.
    // It takes one float parameter
    void Area(float D)
    {
        double A = (PI / 4) * D * D;
 
        System.out.println("Area of the circle is :" + A);
    }
}
 
class GFG {
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Creating object of Circle class
        Circle obj = new Circle();
 
        // Calling function
        obj.Area(10);
        obj.Area(20.4);
    }
}

Output
Area of the circle is :78.53981633974483
Area of the circle is :326.851299679482

Time complexity: O(1) since performing constant operations

Auxiliary Space: O(1)


Article Tags :