Write a class that has three overloaded methods for calculating the areas of the following geometric shapes:
• Circle
• Rectangle
• Cylinder
class Shape {
public double calculateArea(double radius) {
return Math.PI * radius * radius;
}
public double calculateArea(double width, double height) {
return width * height;
}
public double calculateArea(double radius, double height, int k) {
return 2 * Math.PI * radius * (radius + height);
}
}
class App {
public static void main(String[] args) {
Shape s = new Shape();
System.out.println("The area of circle is: " + s.calculateArea(5));
System.out.println("The area of rectangle is: " + s.calculateArea(5, 4));
System.out.println("The area of cylinder is: " + s.calculateArea(5, 4, 5));
}
}
Comments
Leave a comment