Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

10.13 (Project: Shape Hierarchy) Implement the Shape hierarchy shown below. Each

ID: 3817155 • Letter: 1

Question

10.13 (Project: Shape Hierarchy) Implement the Shape hierarchy shown below. 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. The program should print a text description of the object to which each array element refers. In the loop that walks the shapes array, determine whether each shape is a TwoDimensionalShape or a ThreeDimensionalShape. If it’s a TwoDimensionalShape, display its area. If it’s a ThreeDimensionalShape, display its area and volume.

Make Circle class, a Square class, and a Cube class.

Shape Hierarchy

Now consider the Shape inheritance hierarchy in Fig. 9.3. This hierarchy begins with superclass Shape, which is extended by subclasses TwoDimensionalShape and ThreeDimensionalShape—Shapes are either TwoDimensionalShapes or ThreeDimensionalShapes. The third level of this hierarchy contains specific types of TwoDimensionalShapes and ThreeDimensionalShapes. As in Fig. 9.2, we can follow the arrows from the bottom of the diagram to the topmost superclass in this class hierarchy to identify several is-a relationships. For example, a Triangle is a TwoDimensionalShape and is a Shape, while a Sphere is a ThreeDimensionalShape and is a Shape. This hierarchy could contain many other classes. For example, ellipses and trapezoids also are TwoDimensionalShapes.

Fig. 9.3 | Inheritance hierarchy UML class diagram for Shapes.

Not every class relationship is an inheritance relationship. In Chapter 8, we discussed the has-a relationship, in which classes have members that are references to objects of other classes. Such relationships create classes by composition of existing classes. For example, given the classes Employee, BirthDate and TelephoneNumber, it’s improper to say that an Employee is a BirthDate or that an Employee is a TelephoneNumber. However, an Employee has a BirthDate, and an Employee has a TelephoneNumber.

It’s possible to treat superclass objects and subclass objects similarly—their commonalities are expressed in the superclass’s members. Objects of all classes that extend a common superclass can be treated as objects of that superclass—such objects have an is-a relationship with the superclass. Later in this chapter and in Chapter 10, we consider many examples that take advantage of the is-a relationship.

A subclass can customize methods that it inherits from its superclass. To do this, the subclass overrides (redefines) the superclass method with an appropriate implementation, as we’ll see in the chapter’s code examples.

Shape Two Dimensional Shape Circle Triangle Square Fig. 9.3 l Inheritance hierarchy for Shapes Three Dimensional Shape Tetrahedron Sphere Cube

Explanation / Answer

I'm explaining step by step procedure. First we need a shape class which contains two coordinates and a constructor with two orguments.

step 2: Two dimensional class
step 3: Three dimensional class


package main;

public abstract class Shape
{

private double x, y;


public Shape( double x, double y )
{

this.x = x;
this.y = y;
}

public void seta( double x )
{

this.x = x; }

public double getx()
{

return x;
}

public void sety( double y)

{

this.y = y;
}

public double gety()
{

return y;
}

public String toString()
{

return String.format("Area");
}


public abstract String getName();


}

public abstract class TwoDimensionalShape extends Shape
{

private double Dimension1, Dimension2;


public TwoDimensionalShape( double x, double y, double dime1, double dime2 ) {

super(x, y);
Dimension1 = dime1;
Dimension2 = dime2;
}


public void setDimension1(double dime1) {

this.Dimension1 = dime1; }


public double getDimension1() {
return Dimension1; }


public void setDimension2(double dime2) {

this.Dimension2 = dime2; }


public double getDimension2() {
return Dimension2;}


public abstract double getArea();
  
}


public abstract class ThreeDimensionalShape extends Shape{

private double Dimension1, Dimension2, Dimension3;


public ThreeDimensionalShape(double x, double y, double dime1, double dime2, double dime3 ){

super( x, y );
Dimension1 = dime1;
Dimension2 = dime2;
Dimension3 = dime3; }


public void setDimension3(double dime3) {

this.Dimension2 = dime3; }

public double getDimension3() {
return Dimension3;}


public abstract double getArea();
  
public abstract double getVolume();

}

package main;

public class Circle extends TwoDimensionalShape {

private double radius;

public Circle(double x, double y, double radius){

super(x, y, radius, radius);

if(radius <= 0.0){

throw new IllegalArgumentException("Radius >= 0.0"); }
else{

this.radius = radius;
}
}


public double getRadius(){

return radius;
}

public void setRadius(double newRadius){

if(radius <= 0.0){

throw new IllegalArgumentException("Radius >= 0.0"); }
else{

radius = radius;
}
}

public double getArea() {
return radius * radius * Math.PI;
}

@Override
public String getName() {
throw new UnsupportedOperationException("Not supported yet.");
}


}

public class Square extends TwoDimensionalShape
{

private double side;


public Square( double x, double y, double side )
{
super( x, y, side, side );

if(side <= 0.0){

throw new IllegalArgumentException("Side >= 0.0"); }
else{

this.side = side;
}
}

public void setSide(double side){
if(side <= 0.0){

throw new IllegalArgumentException("Side >= 0.0"); }
else{

this.side = side;
}
}

public double getSide(){
return side;
}

public double getArea(){
return side * side;
}





@Override
public String getName() {
throw new UnsupportedOperationException("Not supported yet.");
}



}

public class Traingle extends TwoDimensionalShape{

private double base;
private double height;

public Traingle( double x, double y, double base, double height){

super(x, y, base, height);

if(height <= 0.0){

throw new IllegalArgumentException("Radius >= 0.0"); }
else{

this.base = base;
this.height = height;
}
}

public void setBase(double base){
if(base <= 0.0)
throw new IllegalArgumentException("Base length > 0");
this.base = base;
}
public double getBase(){
return base;
}

public void setHieght(double hieght){
if(hieght <= 0.0)
throw new IllegalArgumentException("Hieght length > 0");
this.height = hieght;
}

public double getHieght(){
return height;
}

public double getArea(){
return (getBase()*getHieght()/2);
}

@Override
public String toString(){
return String.format("%s = %.2f%n%19s = %.2f%n%17s = %.2f", "Base", getBase(), "Hieght", getHieght(),super.toString(), getArea());
}

@Override
public String getName() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

public class Cube extends ThreeDimensionalShape
{

public Cube( int x, int y, int side )
{
super( x, y, side, side, side );
}
}

public class ShapeTest
{

public static void main( String args[] )
{
Shape shapes[] = new Shape[ 4 ];
shapes[ 0 ] = new Circle( 22, 88, 4 );
shapes[ 1 ] = new Square( 71, 96, 10 );
shapes[ 2 ] = new Sphere( 8, 89, 2 );
shapes[ 3 ] = new Cube( 79, 61, 8 );

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() );
}
System.out.println();
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote