Write a program that finds the area of circle, triangle, rectangle and square using a function named area. In this program, use a single class named shape which contains various implementations of ‘area’. Your program should dynamically decide which shapes area has to be found depending on the type of input. The radius is float value, base and height of triangle are double values, sides of rectangle and square are integer values.
class Shape {
static double area(float radius) {
return Math.PI * radius * radius;
}
static double area(double base, double height) {
return base * height / 2;
}
static int area(int sideA, int sideB) {
return sideA * sideB;
}
static int area(int side) {
return side * side;
}
}
public class Main {
public static void main (String[] args){
System.out.println(Shape.area(3.0f));
System.out.println(Shape.area(3.0, 4.0));
System.out.println(Shape.area(3, 4));
System.out.println(Shape.area(3));
}
}
Comments
Leave a comment