Java 程序:使用方法重载求正方形、矩形和圆形面积
在本教程中,我们将学习如何使用方法重载来查找正方形、矩形和圆形的面积。矩形的面积是它的长度和宽度的乘积。圆的面积是圆半径的平方和圆周率的乘积。正方形的面积是它的边的平方。如果一个类有多个同名但参数不同的方法,称为方法重载。但是在继续之前,如果你不熟悉 java 中方法重载的概念,那么一定要检查一下 Java 中的方法重载。
输入:区域(3)
面积(3,2)
区域(3.2)
输出:
这个广场的面积是 9 平方。单位。
这个长方形的面积是 6 平方。单位。
这个圆的面积是 32.15 平方。单位。
让我们看看下面的例子,以便更好地理解。
Java 程序:程序 1:使用方法重载寻找正方形、矩形和圆形区域
在这个程序中,我们将看到如何使用方法重载来找到正方形、矩形和圆形的面积。
算法:
- 开始
- 为矩形、正方形和圆形声明三个不同的类。
- 声明两个同名但具有不同数量参数或不同数据类型的方法。
- 使用对象调用这些方法。
- 根据参数数量或数据类型调用相应的方法。
- 显示结果。
- 停下来。
让我们看看下面的例子,以更好地理解上述算法。
//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);
}
}
矩形面积:600 矩形面积:56.25 圆面积:28.274333882308138 圆面积:95.03317777109123 广场面积:400.0 广场面积:27.040000000000003
程序 2: Java 程序使用方法重载寻找正方形、矩形和圆形的面积
在这个程序中,我们将看到如何使用方法重载来找到正方形、矩形和圆形的面积。
算法:
- 开始
- 用不同数量的参数或不同的数据类型声明三个同名的方法。
- 使用对象调用这些方法。
- 根据参数数量或数据类型调用相应的方法。
- 显示结果。
- 停下来。
让我们看看下面的例子,以更好地理解上述算法。
//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");
}
}
正方形的面积是 16.0 平方米 长方形的面积是 120.0 平方米 圆形的面积是 94.985 平方米