Write a class that has three overloaded methods for calculating the areas of the following geometric shapes:
• Circle
• Rectangle
• Cylinder
Here are the formulae for calculating the area of the respective shapes:
Shape: Circle
Area Formula: A = πr2
Notes: π is Math.PI and r is the circle’s radius
Shape: Rectangle
Area Formula: Area = Width * Height
Shape: Cylinder
Area Formula: Area = 2πrh + 2πr2
Notes: π is Math.PIand r is the radius of the cylinder’s base h is the cylinder’s height
package com.task;
public abstract class Shape {
public static void main(String[] args) {
// write your code here
}
public abstract double area();
}
package com.task;
public class Circle extends Shape{
private double radius;
@Override
public double area() {
return 3.14*radius*radius;
}
}
package com.task;
public class Rectangle extends Shape {
private double height;
private double width;
@Override
public double area() {
return height*width;
}
}
package com.task;
public class Cylinder extends Shape {
private double radius;
private double height;
@Override
public double area() {
return 2*3.14*radius*height+2*3.14*radius*radius;
}
}
Comments
Leave a comment