Java Program to Find Area of Square, Rectangle and Circle using Method Overloading
In this tutorial, we will learn how to find the area of squares, rectangles, and circles using method overloading. The area of the rectangle is the product of its length and width/breadth. The area of the circle is the product of the square of the radius of the circle and the value of PI. The area of the square is the square of its sides. If a class has multiple methods having the same name but different in parameters, it is known as Method Overloading. But before moving forward if you are not familiar with the concept of method overloading in java, then do check Method Overloading in Java.
Input: Area(3)
Area(3,2)
Area(3.2)
Output:
The area of the square is 9 sq. units.
The area of the rectangle is 6 sq. units.
The area of the circle is 32.15 sq. units.
Let us look at the below examples for a better understanding.
Method 1: Java Program to Find Area of Square, Rectangle, and Circle using Method Overloading
In this program, we will see how to find the area of a square, rectangle, and circle using Method Overloading.
Algorithm:
- Start
- Declare three different classes for rectangle, square, and circle.
- Declare two methods of the same name but with a different number of arguments or with different data types.
- Call these methods using objects.
- Call the corresponding methods as per the number of arguments or their data types.
- Display the result.
- Stop.
Let us look at the below example for a better understanding of the above algorithm.
//Java Program to Find the Area of Square, Rectangle and Circle using Method Overloading
public class Main
{
//Driver Code
public static void main(String[] args)
{
Rectangle obj = new Rectangle();
// Calling function
obj.Area(30, 20);
obj.Area(12.5, 4.5);
Circle obj1 = new Circle();
// Calling function
obj1.Area(3);
obj1.Area(5.5);
Square obj2 = new Square();
// Calling function
obj2.Area(20);
obj2.Area(5.2);
}
}
class Square
{
// Overloaded function to
// calculate the area of the square
// It takes one double parameter
void Area(double side)
{
System.out.println("Area of the Square: "+ side * side);
}
// Overloaded function to
// calculate the area of the square
// It takes one float parameter
void Area(float side)
{
System.out.println("Area of the Square: "+ side * side);
}
}
class Circle
{
static final double PI = Math.PI;
// Overloaded 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("The area of the circle is :" + A);
}
// Overloaded 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("The area of the circle is :" + A);
}
}
class Rectangle
{
// Overloaded function to
// calculate the area of the rectangle
// It takes two double parameters
void Area(double l, double b)
{
System.out.println("Area of the rectangle: " + l * b);
}
// Overloaded function to
// calculate the area of the rectangle.
// It takes two float parameters
void Area(int l, int b)
{
System.out.println("Area of the rectangle: " + l * b);
}
}
Area of the rectangle: 600
Area of the rectangle: 56.25
The area of the circle is:28.274333882308138
The area of the circle is:95.03317777109123
Area of the Square: 400.0
Area of the Square: 27.040000000000003
Method 2: Java Program to Find Area of Square, Rectangle, and Circle using Method Overloading
In this program, we will see how to find the area of the square, rectangle, and circle using Method Overloading.
Algorithm:
- Start
- Declare three methods of the same name but with a different number of arguments or with different data types.
- Call these methods using objects.
- Call the corresponding methods as per the number of arguments or their data types.
- Display the result.
- Stop.
Let us look at the below example for a better understanding of the above algorithm.
//Java Program to Find the area of Square, Rectangle and Circle using Method Overloading
public class Main
{
//Driver Code
public static void main(String[] args)
{
CalculateArea ob = new CalculateArea();
ob.area(4);
ob.area(10,12);
ob.area(5.5);
}
}
class CalculateArea
{
void area(float x)
{
System.out.println("The area of the square is "+Math.pow(x, 2)+" sq units");
}
void area(float x, float y)
{
System.out.println("The area of the rectangle is "+x*y+" sq units");
}
void area(double x)
{
double z = 3.14 * x * x;
System.out.println("The area of the circle is "+z+" sq units");
}
}
The area of the square is 16.0 sq units
The area of the rectangle is 120.0 sq units
The area of the circle is 94.985 sq units