144k views
4 votes
Write an inheritance hierarchy of three-dimensional shapes. Make a toplevel shape interface 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, and use abstract classes as appropriate. Add methods to the subclasses to represent the unique behavior of each three-dimensional shape, such as a method to get a sphere's radius.

User Mista
by
5.3k points

1 Answer

4 votes

Answer:

Check the explanation

Step-by-step explanation:

Test.java

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

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

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

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

public abstract class Shape {

public double area() { return 0.0; }

public double volume() { return 0.0; }

public abstract String getName();

}

User Leifcr
by
5.6k points