Question
Design a class to overload a function area() as follows:
(i) double area(double a. double b, double e) with three double arguments, returns the area of a scalene triangle.
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
area = 1/2 × height × (a + b)
(iii) double area(double diagonal1, double diagonal2) with two double arguments, returns the area of a rhombus
area = 1/2 × (diagonal1 × diagonal2)
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
public class Area
{
public double area(double a, double b, double c)
{
double s = (a + b + c) / 2;
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
return area;
}
public double area(int a, int b, int height)
{
double area = 0.5 * height * (a + b);
return area;
}
public double area(double diagonal1, double diagonal2)
{
double area = 0.5 * (diagonal1 * diagonal2);
return area;
}
}