You are provided with a simple Java project.create an abstract class 3DShape for 3D shapes.
Create Cube and Cylinder 3D shapes by extending the 3DShape class you’ve just created.
Create an interface to calculate volume and implement necessary code to calculate correct value of volume of all types of your 3D shapes.
As a bonus, implement a design where 2DShape supertype is the parent of the 3DShape supertype, and respective methods for area and volume calculation are inherited from the relevant supertype for the concrete 3D objects.
public abstract class Shape2D {
public abstract double area();
}
public abstract class Shape3D extends Shape2D implements Volume{
}
public interface Volume {
double volume();
}
public class Cylinder3D extends Shape3D {
private double radius;
private double height;
public Cylinder3D(double radius, double height) {
this.radius = radius;
this.height = height;
}
@Override
public double volume() {
return Math.PI * radius * radius * height;
}
@Override
public double area() {
return 2 * Math.PI * radius * (height + radius);
}
}
public class Cube3D extends Shape3D {
private double side;
public Cube3D(double side) {
this.side = side;
}
@Override
public double volume() {
return Math.pow(side, 3);
}
@Override
public double area() {
return 6 * side * side;
}
}
Comments
Leave a comment