Write overloaded methods named area. One calculates the area of a circle, one of a rectangle, and one of a triangle.
public class AreaCalculator {
public static double area(double radius) {
return Math.PI * radius * radius;
}
public static double area(double width, double height) {
return width * height;
}
public static double area(double base, double h, boolean isTriangle) {
return 0.5 * base * h;
}
public static void main(String[] args) {
System.out.println(area(5.0));
System.out.println(area(4.0, 6.0));
System.out.println(area(4.0, 6.0, true));
}
}
Each version has a different parameter list, so Java knows which one to call. The circle version takes argument, the rectangle takes , and the triangle takes .