Write A Program In Java Which Has A Class Shape Having 2 Overloaded Methods Area(Float Radius) And Area(Float Length, Float Width). Display The Area Of Circle And Rectangle Using Overloaded Methods

0

Write A Program In Java Which Has A Class Shape Having 2 Overloaded Methods Area(Float Radius) And Area(Float Length, Float Width). Display The Area Of Circle And Rectangle Using Overloaded Methods.

Write A Program In Java Which Has A Class Shape Having 2 Overloaded Methods Area(Float Radius) And Area(Float Length, Float Width). Display The Area Of Circle And Rectangle Using Overloaded Methods

In this program, we will create a java program in which class shape having 2 overloaded methods area(float radius) and area(float length, float width) and then display the area of circle and rectangle.


Program

public class Shape {
    // Method to calculate the area of a circle
    public float area(float radius) {
        return (float) (Math.PI * radius * radius);
    }

    // Method to calculate the area of a rectangle
    public float area(float length, float width) {
        return length * width;
    }

    public static void main(String[] args) {
        Shape shape = new Shape();
        float radius = 5.0f;
        float length = 10.0f;
        float width = 6.0f;

        // Calculate and display the area of a circle
        System.out.println("Area of circle with radius " + radius + " is " + shape.area(radius));

        // Calculate and display the area of a rectangle
        System.out.println("Area of rectangle with length " + length + " and width " + width + " is " + shape.area(length, width));
    }
}

Output
Area of circle with radius 5.0 is 78.53982 Area of rectangle with length 10.0 and width 6.0 is 60.0

Explanation
In this program, we define a shape class with two overloaded methods area(float radius) and area(float length, float width). These methods calculate the area of a circle and rectangle respectively. In the main method, we create an instance of the shape class and use it to calculate and display the areas of a circle and rectangle.

❤️ I Hope This Helps You Understand, Click Here To Do More Exercise In Java Programming.

Tags

Post a Comment

0Comments
Post a Comment (0)