.java file Implement the Shape hierarchy shown in Fig. 9.3 in the text. Each Two
ID: 670016 • Letter: #
Question
.java file
Implement the Shape hierarchy shown in Fig. 9.3 in the text. Each TwoDimensionalShape should contain methods getArea and getPerimeter to calculate the area and perimeter 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 a ThreeDimensionalShape . If it's a TwoDimensionalShape, display its area and perimeter. If it's a ThreeDimensionalShape, display its area and volume. The 3 top level classes should be abstract and should have at least one abstract method. You can instantiate a triangle object with 5 data points: base, height, and the length of each of the three sides.Explanation / Answer
public abstract class Shape2d
{
public Shape2d (String id)
{
this.id = id;
}
public abstract double getArea();
public String getId()
{
return id;
}
public String toString( )
{
return "Shape2d[id="+id+",area="+getArea()+"]";
}
private String id;
}
public abstract class Shape3d
{
public Shape3d (String id)
{
this.id = id;
}
public abstract double getArea();
public abstract double getVolume();
public String getId()
{
return id;
}
public String toString()
{
return "Shape3d[id="+id+",area="+getArea()+"]";
}
private String id;
}
public class Circle extends Shape2d
{
public Circle (String name, double r)
{
spurer(name);
radius = r;
}
public double getArea()
{
return Math.PI * radius * radius;
}
public double getRadius()
{
return radius;
}
public void setRadius(double newRadius)
{
radius = newRadius;
}
public String toString( )
{
return "Circle["radius="+radius+","+super.toString()+"];
}
private double radius;
}
public class Triangle extends Shape2d
{
public Triangle (String name, double w, double h)
{
super(name);
width = w;
height = h;
}
public double getArea()
{
return width*height;
}
public double getWidth()
{
return width;
}
public double getHeight()
{
return height;
}
public void setWidthHeight(double newWidth, double newHeight)
{
width = newWidth;
height = newHeight;
}
public String toString( )
{
return "Triangle["width="+width+",height="+height+","+super.toString()+"];
}
private double width, height;
}
public class Rectangle extends Shape2d
{
public Rectangle (String name, double w, double h)
{
super(name);
width = w;
height = h;
}
//Override the abstract method declared in Shape
public double getArea()
{
return width*height;
}
public double getWidth()
{
return width;
}
public double getHeight()
{
return height;
}
public void setWidthHeight(double newWidth, double newHeight)
{
width = newWidth;
height = newHeight;
}
public String toString( )
{
return "Rectangle["width="+width+",height="+height+","+super.toString()+"];
}
private double width, height;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.