Slip No 18 Q A

Q.Write a Java program to calculate area of Circle, Triangle & Rectangle.(Use Method Overloading) [15 M]

import java.util.Scanner;

class Area
{
    // Area of Circle
    void area(double r)
    {
        double result = 3.14 * r * r;
        System.out.println("Area of Circle = " + result);
    }

    // Area of Rectangle
    void area(double l, double b)
    {
        double result = l * b;
        System.out.println("Area of Rectangle = " + result);
    }

    // Area of Triangle
    void area(float base, float height)
    {
        double result = 0.5 * base * height;
        System.out.println("Area of Triangle = " + result);
    }
}

class OverloadDemo
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        Area a = new Area();

        System.out.print("Enter radius of circle: ");
        double r = sc.nextDouble();
        a.area(r);

        System.out.print("\nEnter length and breadth of rectangle: ");
        double l = sc.nextDouble();
        double b = sc.nextDouble();
        a.area(l, b);

        System.out.print("\nEnter base and height of triangle: ");
        float base = sc.nextFloat();
        float height = sc.nextFloat();
        a.area(base, height);
    }
}
Spread the love

Leave a Comment

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

Scroll to Top