JAVA-17

Q.17 Create a method area(),overload it to calculate area of circle ,rectangle and square.

Solution :- 

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

    // Method to calculate area of a rectangle
    double area(double length, double breadth) {
        return length * breadth;
    }

    // Method to calculate area of a square
    double area(int side) {
        return side * side;
    }
}

public class AreaOverloading {
    public static void main(String[] args) {
        Shape shape = new Shape();

        // Circle area
        System.out.println("Area of Circle: " + shape.area(5.0));
        
        // Rectangle area
        System.out.println("Area of Rectangle: " + shape.area(5.0, 10.0));
        
        // Square area
        System.out.println("Area of Square: " + shape.area(4));
    }
}

TRY IT YOURSELF

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.