Monday, December 12, 2022

Overloading Function Area In ICSE BlueJ Program

 Design a class to overload a function area() as follows:


(i) double area(double a, double b, double c) with three double arguments, 

returns the area of a scalene triangle using the formula:area = √(s(s – a)(s – b)(s – c))

where s = (a + b + c) / 2.


(ii) double area(int a, int b, int height) with three integer arguments,

 returns the area of a trapezium using the formula:area = 1/2 × height × (a + b)


(iii) double area(double diagonal1, double diagonal2) with two double arguments, 

returns the area of a rhombus using the formula:area = 1/2 × (diagonal1 × diagonal2)


Program


                

class Area
{
    public double area(double a, double b, double c)
{
        double s = (a + b + c) / 2.0;
    System.out.println(s);
        double areaTr = Math.sqrt(s * (s - a) * (s - b) * (s - c));
        return areaTr;
    }
    public double area(int a, int b, int height)
{
        double areaT = 1.0 / 2 * height * (a + b);
        return areaT;
    }
    public double area(double diagonal1, double diagonal2)
{
        double areaR = 1.0 / 2 * (diagonal1 * diagonal2);
        return areaR;
    }
 
public static void main(String args[])
  {
    Area ob = new Area();
    System.out.println(ob.area(3.0,9.0,7.5));
    System.out.println(ob.area(8,9,9));
    System.out.println(ob.area(3.0,3.2)); 
  }
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner