Answer to Question #241931 in Java | JSP | JSF for Deniz

Question #241931

You are provided with a simple Java project. Using the Shape class as a guide, 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. 


1
Expert's answer
2021-09-24T22:18:30-0400
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;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS