📜  Java程序使用方法重载查找圆的面积

📅  最后修改于: 2022-05-13 01:55:30.231000             🧑  作者: Mango

Java程序使用方法重载查找圆的面积

是一种简单的形状,由平面上所有与圆心等距的点组成。在本文中,我们将学习如何使用重载方法求圆的面积。

术语:

  • 方法重载:方法重载允许不同的方法具有相同的名称,但具有不同的签名,其中签名可能因输入参数的数量或输入参数的类型或两者而异。
  • 面积:表示二维图形或形状在平面中的范围的量称为面积。
  • 半径:从圆心到圆的任意一点的线段称为半径。
  • 直径:端点在圆上并通过圆心的线段称为圆的直径。它也称为圆上任意两点之间的最大距离。

圆的面积

圆的面积是圆半径的平方与 PI 值的乘积。我们可以使用以下公式简单地计算圆的面积:

  • 使用圆的半径:

公式:

其中,r 是圆的半径。

  • 使用圆的直径:

公式:

其中,r 是圆的半径。

下面是上述方法的实现:

示例 1:

Java
// 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);
    }
}


Java
// 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);
    }
}


输出
Area of the circle is :78.53981633974483
Area of the circle is :19.634954084936208

示例 2:

Java

// 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);
    }
}
输出
Area of the circle is :78.53981633974483
Area of the circle is :326.851299679482