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

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

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

正方形是平面中的简单平面形状,由四个角处的四个点定义。它有四个等长的边和四个直角的角。

方法重载:方法重载允许不同的方法具有相同的名称,但具有不同的签名,其中签名可能因输入参数的数量或输入参数的类型或两者而异。

在本文中我们将学习如何使用重载方法求正方形的面积。

广场面积

正方形的面积是它长的正方形我们可以使用以下公式简单地计算正方形的面积

公式:

这里,a 是正方形的边。

下面是上述方法的实现:

示例 1:

显示了 Area()函数的方法重载,该函数具有不同数据类型( float 、 int 、 double )的正方形边的不同参数

Java
// Java program to find the area of
// the square using Method Overloading
// of parameters with different datatype
// of sides
  
import java.io.*;
  
class Square {
  
    // Overloaded Area() 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 Area() 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 GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating object of square class
        Square obj = new Square();
  
        // Calling function
        obj.Area(10);
        obj.Area(3.2);
    }
}


Java
// Java program to find the area of
// the multiple shapes
// using Method Overloading
  
import java.io.*;
  
class Shape {
  
    static final double PI = Math.PI;
  
    // Overloaded Area() function to
    // calculate the area of the square
    // It takes one float parameter
    void Area(float a)
    {
        float A = a * a;
        System.out.println("Area of the Square: " + A);
    }
  
    // Overloaded Area() function to
    // calculate the area of the circle
    // It takes one double parameter
    void Area(double a)
    {
        double A = PI * a * a;
        System.out.println("Area of the Circle: " + A);
    }
  
    // Overloaded Area() function to
    // calculate the area of the rectangle
    // It takes two int parameters
    void Area(int a, int b)
    {
        int A = a * b;
        System.out.println("Area of the Rectangle: " + A);
    }
}
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating object of Shape class
        Shape obj = new Shape();
  
        // Calling function
        obj.Area(10.5);
        obj.Area(3);
        obj.Area(5, 4);
    }
}


输出
Area of the Square: 100.0
Area of the Square: 10.240000000000002

示例 2:

显示 Area()函数的方法重载,该函数返回不同图形的面积,如(正方形、圆形、矩形)

Java

// Java program to find the area of
// the multiple shapes
// using Method Overloading
  
import java.io.*;
  
class Shape {
  
    static final double PI = Math.PI;
  
    // Overloaded Area() function to
    // calculate the area of the square
    // It takes one float parameter
    void Area(float a)
    {
        float A = a * a;
        System.out.println("Area of the Square: " + A);
    }
  
    // Overloaded Area() function to
    // calculate the area of the circle
    // It takes one double parameter
    void Area(double a)
    {
        double A = PI * a * a;
        System.out.println("Area of the Circle: " + A);
    }
  
    // Overloaded Area() function to
    // calculate the area of the rectangle
    // It takes two int parameters
    void Area(int a, int b)
    {
        int A = a * b;
        System.out.println("Area of the Rectangle: " + A);
    }
}
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating object of Shape class
        Shape obj = new Shape();
  
        // Calling function
        obj.Area(10.5);
        obj.Area(3);
        obj.Area(5, 4);
    }
}
输出
Area of the Circle: 346.36059005827474
Area of the Square: 9.0
Area of the Rectangle: 20