首页 > 文章列表 > 使用方法重载的Java程序来计算圆的面积

使用方法重载的Java程序来计算圆的面积

java 计算 方法重载 面积
475 2023-09-10

我们可以使用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 - 在公共类的 main 方法中初始化两个不同数据类型的变量。

第 3 步 - 在公共类的 main 方法中创建自定义类的对象。

第 4 步 - 调用特定方法以使用创建的自定义对象查找圆的面积。

示例 1

在此示例中,我们使用基本公式计算圆的面积并在 Java 中实现方法重载。

方法重载是通过改变areaOfCircle方法中的参数类型来实现的。现在,当用户将输入作为双参数值提供给areaOfCircle方法时,将调用Area类的第一个areaOfCircle方法并打印输出。如果用户提供float类型输入参数,则调用并执行第二个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”浮点类型变量。因此,当我们调用“areaOfCirclce”函数时,会执行带有 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 中的方法重载。