JAVA: Design and implement a set of classes that define a series of three-dimens
ID: 668586 • Letter: J
Question
JAVA: Design and implement a set of classes that define a series of three-dimensional geometric shapes. For each shape, store fundamental data about its size and provide methods to access and modify this data. In addition, provide appropriate methods to compute each shape’s circumference, area, and volume. In your design, consider how shapes are related and thus where inheritance can be implemented. Create a driver class to instantiate several shapes of differing types and exercise the behavior you provided.
Explanation / Answer
import java.util.Arrays;
public class task {
public static interface GeometricShapes {
public double getArea();
public double getVolume();
}
public static abstract class ThreeDShapes implements GeometricShapes {
protected double a, h;
}
public static class Cones extends ThreeDShapes {
@Override public double getArea() {
return Math.PI * this.a * Math.sqrt(a * a + h * h);
}
@Override public double getVolume() {
return (1d / 3d) * Math.PI * Math.pow(a, 2) * a * h;
}
@Override public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Cone [a=").append(a).append("; ").append("h=")
.append(h).append(" ( area=").append(this.getArea())
.append("; volume=").append(this.getVolume()).append(" )]");
return builder.toString();
}
}
public static void main(String[] args) {
System.out.println(Arrays.toString(new ThreeDShapes[] {
new Cones() {{ a = 3; h = 4;}},
new Cones() {{ a = 4; h = 5;}}
}));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.