Java 使用方法重载计算圆的面积
我们可以使用 方法重载 在Java中计算圆的面积。「方法重载」是Java中的一项功能,允许在同一个类中使用相同的方法名编写多个方法。它使我们能够声明多个具有相同名称但签名不同的方法,即方法中的参数数量可能不同或参数的数据类型可能不同。方法重载帮助我们增加代码的可读性,以便我们可以以不同的方式使用相同的方法。
现在,让我们通过一个 「圆的面积」 的示例来实现Java中的方法重载。在继续之前,让我们了解本文中使用的术语。
什么是圆
「圆」 是一组点的二维表示,这些点与一个特定的点「中心」等距离。
什么是圆的半径
半径 是圆的中心点与位于圆周上的一组点之间的距离。
什么是圆的直径
直径 是圆的中心点与位于圆周上的一组点之间的两倍距离。
Diameter of a Circle : 2r
where
r: radius of circle.
圆的面积
圆的面积是二维平面上由圆覆盖的区域。
Area of Circle : πr^2
where
π : A Greek mathematical Symbol = 3.14 or 22/7.
r : radius of circle.
在下面的示例中,我们将使用圆的面积作为示例来实现Java中的方法重载,通过改变参数的数据类型。
步骤
步骤1 - 编写一个自定义类来找到圆的面积。
步骤2 - 在公共类的主方法中初始化两个不同数据类型的变量。
步骤3 - 在公共类的主方法中创建一个自定义类的对象。
步骤4 - 使用创建的自定义对象调用特定的方法来找到圆的面积。
示例1
在这个示例中,我们使用基本公式计算圆的面积,并在Java中实现方法重载。
通过更改”areaOfCircle”方法中参数的类型来实现方法重载。现在,当用户给出双精度参数值作为areaOfCircle方法的输入时,将调用Area类的第一个areaOfCircle方法并打印输出。如果用户给出浮点型输入参数,则调用和执行第二个areaOfCircle方法。
//Java Code to achieve Method Overloading in Java by Area of Circle
import java.io.*;
class Area {
// In this example area method is overloaded by changing the type of parameters.
double PI = Math.PI;
//Math.PI is a constant value in Java in the Math library.
public void areaOfCircle(double radius) {
double area = 0;
area = PI * radius * radius;
System.out.println("Area of the circle is :" + area);
}
public void areaOfCircle(float radius ) {
double area= 0;
area = PI * radius * radius;
System.out.println("Area of the circle is :" + area);
}
}
public class Main {
public static void main(String args[]) {
Area Object = new Area();
float radius_1 = 7;
Object.areaOfCircle(radius_1);
double radius_2 = 3.5;
Object.areaOfCircle(radius_2);
}
}
输出
Area of the circle is :153.93804002589985
Area of the circle is :38.48451000647496
时间复杂度:O(1) 辅助空间:O(1)
Math.PI 是Java中Math库中的一个常量值。它在Java中的值是3.141592653589793。
我们可以使用一种替代公式来计算圆的面积,其中我们使用直径,在Java中实现方法重载。
推导出圆的半径的替代公式
Area of Circle = πr^2
Substituting ‘r=d/2’ value in the above equation.
Area of Circle = π〖(d/2)〗^2
以下是使用上述公式的Java代码的实现
示例2:使用圆的直径
在下面的示例中,通过改变“areaOfCircle”方法中参数的类型来实现方法重载。将14赋值给“diameter_1”变量,它的类型是double,所以执行参数类型为double的“areaOfCircle”方法。然后将7赋值给“diameter_2”变量,它的类型是float。因此,在调用“areaOfCirlce”函数时,将执行带有float类型参数的函数。
//Java Code to achieve Method Overloading in Java by Area of Circle by alternative formula.
import java.io.*;
import java.util.*;
class Area {
// In this example area method is overloaded by changing the type of parameters.
double PI = Math.PI;
//Math.PI is a constant value in Java in Math library.
public void areaOfCircle(double diameter) {
double area = 0;
area = PI * (diameter/2)*(diameter/2);
System.out.println("Area of the circle is :" + area);
}
public void areaOfCircle(float diameter) {
double area= 0;
area = PI * (diameter/2)*(diameter/2);
System.out.println("Area of the circle is :" + area);
}
}
public class Main {
public static void main(String args[]) {
Area Object = new Area();
double diameter_1 = 14;
float diameter_2 = 7;
Object.areaOfCircle(diameter_1);
Object.areaOfCircle(diameter_2);
}
}
输出
Area of the circle is :153.93804002589985
Area of the circle is :38.48451000647496
时间复杂度:O(1)辅助空间:O(1)
因此,在本文中,我们已经学习了如何在Java中通过改变参数的数据类型来实现方法重载,以求解圆的面积为例。