Write an inheritance hierarchy of three-dimensional shapes. Make a top-level sha
ID: 3562278 • Letter: W
Question
Write an inheritance hierarchy of three-dimensional shapes. Make a top-level shape class that has methods for getting information such as the volume and surface area of a three- dimensional shape. Then make classes and subclasses that implement various shapes such as cubes, rectangular prisms, spheres, triangular prisms, cones, and cylinders. Place common behavior in superclasses whenever possible. Add methods to the subclasses to represent the unique behavior of each three-dimensional shape, such as a method to get a spheres radius. Please explain in detail step by step
Explanation / Answer
// Test.java // Driver for point, square, cube hierarchy import javax.swing.*; public class Test { public static void main( String args[] ) { Point point = new Point( 7, 11 ); Square square = new Square( 3.5, 22, 8 ); Cube cube = new Cube( 3.3, 10, 10 ); Shape[] arrayOfShapes = new Shape[ 3 ]; String result = ""; arrayOfShapes[ 0 ] = point; arrayOfShapes[ 1 ] = square; arrayOfShapes[ 2 ] = cube; result += point.getName() + ": " + point.toString(); result += " " + square.getName() + ": " + square.toString(); result += " " + cube.getName() + ": " + cube.toString(); for ( int i = 0; i < 3; i++ ) { result += " " + arrayOfShapes[ i ].getName() + ": " + arrayOfShapes[ i ].toString(); result += " " + "Area = " + arrayOfShapes[ i ].area(); result += " " + "Volume = " + arrayOfShapes[ i ].volume(); } JOptionPane.showMessageDialog( null, result, "Shapes", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } } // Point.java // Definition of class Point public class Point extends Shape { protected double x, y; public Point( double a, double b ) { setPoint( a, b ); } public void setPoint( double a, double b ) { x = a; y = b; } public double getX() { return x; } public double getY() { return y; } public String toString() { return "[" + x + ", " + y + "]"; } public String getName() { return "Point"; } } // Square.java // Definition of class Square public class Square extends Point { protected double side; public Square() { this( 0.0, 0.0, 0.0 ); } public Square( double s, double a, double b ) { super( a, b ); setSide( s ); } public void setSide( double s ) { side = ( s >= 0 ? s : 0 ); } public double getSide() { return side; } public double area() { return Math.pow( side, 2 ); } public String toString() { return "Corner = " + super.toString() + "; side = " + side; } public String getName() { return "Square"; } } // Cube.java // Definition of class Cylinder public class Cube extends Square { private double depth; public Cube( double s, double a, double b ) { super( s, a, b ); depth = s; } public double area() { return super.area() * 6; } public double volume() { return super.area() * depth; } public String toString() { return super.toString() + "; depth = " + depth; } public String getName() { return "Cube"; } } // Shape.java // Definition of abstract base class Shape public abstract class Shape { public double area() { return 0.0; } public double volume() { return 0.0; } public abstract String getName(); }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.