Question: The problem is this: 10.13 (Project: Shape Hierarchy) Implement the Shape hierarchy shown in Fig. 9.3. Each TwoDimensionalShape should contain method getArea to calculate the

The problem is this:

10.13 (Project: Shape Hierarchy) Implement the Shape hierarchy shown in Fig. 9.3. Each TwoDimensionalShape should contain method getArea to calculate the area of the two-dimensional shape. Each ThreeDimensionalShape should have methods getArea and getVolume to calculate the surface area and volume, respectively, of the three-dimensional shape. Create a program that uses an array of Shape references to objects of each concrete class in the hierarchy. The program should print a text description of the object to which each array element refers. Also, in the loop that processes all the shapes in the array, determine whether each shape is a TwoDimensionalShape or aThreeDimensionalShape. If its a TwoDimensionalShape, display its area. If its a ThreeDimensionalShape, display its area and volume.

page 439

From the book Java How to Program (Early Objects) 10th Edition

My code:

Shape:

package edu.pupr.ShapeHierarchy;

public abstract class Shape {

//Declare variables

private int x;

private int y;

//Parameter constructor

public Shape(int x, int y)

{

this.x=x;

this.y=y;

}

//Create mutators

/**

*

* @return x

*/

public int getX()

{

return x;

}

/**

*

* @param x

*/

public void setX(int x)

{

this.x = x;

}

/**

*

* @return y

*/

public int getY()

{

return y;

}

/**

*

* @param y

*/

public void setY(int y)

{

this.y = y;

}

//Method

public String toString()

{

return String.format(" [%d, %d] ", getX(), getY());

}

//Abstract methods

public abstract String getName();

}

ShapeTest:

package edu.pupr.ShapeHierarchy;

public class ShapeTest

{

public static void main(String args[])

{

Shape shapes[]= new Shape [4];

shapes[0]= new Circle(54, 96, 4);

shapes[1]= new Square(10, 25, 6);

shapes[2]= new Sphere (6, 70, 2);

shapes[3]= new Cube(92, 85, 8);

//For loop

for(Shape currentShape : shapes)

{

System.out.printf("%s: %s",

currentShape.getName(), currentShape);

if (currentShape instanceof

TwoDimensionalShape)

{

TwoDimensionalShape twoDimensionalShape =

(TwoDimensionalShape) currentShape;

System.out.printf("%s's area is %s ", currentShape.getName(),

twoDimensionalShape.getArea());

}

if (currentShape instanceof

ThreeDimensionalShape)

{

ThreeDimensionalShape threeDimensionalShape =

(ThreeDimensionalShape) currentShape;

System.out.printf("%s's area is %s ", currentShape.getName(),

threeDimensionalShape.getArea());

System.out.printf("%s's volume is %s ", currentShape.getName(),

threeDimensionalShape.getVolume()); This line gives me an error

}

System.out.println();

}

}

}

ThreeDimensionalShape:

package edu.pupr.ShapeHierarchy;

public abstract class ThreeDimensionalShape extends Shape {

private int dim1;

private int dim2;

private int dim3;

public ThreeDimensionalShape(int x, int y, int d1, int d2, int d3)

{

super(x,y);

dim1=d1;

dim2=d2;

dim3=d3;

}

public int getDim1() {

return dim1;

}

public int getDim2() {

return dim2;

}

public int getDim3() {

return dim3;

}

public abstract int getArea();

public abstract int getVolume();

}

The line in italics gives me this error:

Exception in thread "main" java.lang.AbstractMethodError: edu.pupr.ShapeHierarchy.ThreeDimensionalShape.getVolume()I

at edu.pupr.ShapeHierarchy.ShapeTest.main(ShapeTest.java:38)

.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!