The Shape class must have an encapsulated length data field with a default value
ID: 3759749 • Letter: T
Question
The Shape class must have an encapsulated length data field with a default value of 1. It must also have a no-arg constructor, another constructor that creates a shape with the specified length, and a getter method for length. The TwoDimensionalShape class must have a getArea method to calculate the area of the two-dimensional shape. The ThreeDimensionalShape class must have methods getSurfaceArea and getVolume to calculate the surface area and volume, respectively, of the three-dimensional shape. Assume that the length data field measures the diameter for a circle and the side for all other shapes. Complete all classes such that they have methods to initialize their data field and compute the correct areas and volumes. You may search for the formulas on the Internet. Assume that all shapes are regular i.e. all sides of a shape have the same length. Each class must also have an overridden toString method that returns a string description of its shape and its length.
Write a test program that creates an ArrayList of Shape objects. Create an object for each class in the hierarchy and add it to the list. Then loop through all elements in the list and print a text description of the referred shape. Determine the shape for each element, and display its area if it’s a TwoDimensionalShape, and its surface area and volume if it’s a ThreeDimensionalShape.
Explanation / Answer
Shape Class:
public class Shape
{
private double length;
public Shape()
{
this(1);
}
public Shape(double length)
{
this.length = length;
}
public double getLength()
{
return this.length;
}
}
//====================================================================
TwoDimensionalShape Class:
public abstract class TwoDimensionalShape extends Shape
{
public TwoDimensionalShape()
{
super();
}
public TwoDimensionalShape(double length)
{
super(length);
}
public abstract double getArea();
}
//=========================================================================
Circle Class:
public class Circle extends TwoDimensionalShape
{
public Circle()
{
super();
}
public Circle(double diameter)
{
super(diameter);
}
@Override
public double getArea() {
return Math.PI*getLength()*getLength()/4.0;
}
public String toString()
{
return "Shape: Circle, Diameter: "+getLength();
}
}
//=======================================================================
Square Class:
public class Square extends TwoDimensionalShape
{
public Square()
{
super();
}
public Square(int length)
{
super(length);
}
@Override
public double getArea() {
return getLength()*getLength();
}
public String toString()
{
return "Shape: Square, Side: "+getLength();
}
}
//======================================================================
Triangle Class:
public class Triangle extends TwoDimensionalShape
{
public Triangle()
{
super();
}
public Triangle(int side)
{
super(side);
}
@Override
public double getArea() {
return Math.sqrt(3)*getLength()*getLength()/4.0;
}
public String toString()
{
return "Shape: Triangle, Side: "+getLength();
}
}
//======================================================================
ThreeDimensionalShape Class:
public abstract class ThreeDimensionalShape extends Shape
{
public ThreeDimensionalShape()
{
super();
}
public ThreeDimensionalShape(double length)
{
super(length);
}
public abstract double getSurfaceArea();
public abstract double getVolume();
}
//===================================================================
Cube Class:
public class Cube extends ThreeDimensionalShape
{
public Cube()
{
super();
}
public Cube(double side)
{
super(side);
}
@Override
public double getSurfaceArea() {
return 6*getLength()*getLength();
}
@Override
public double getVolume() {
return getLength()*getLength()*getLength();
}
public String toString()
{
return "Shape: Cube, Side: "+getLength();
}
}
//====================================================================
Sphere Class:
public class Sphere extends ThreeDimensionalShape
{
public Sphere()
{
super();
}
public Sphere(int diameter)
{
super(diameter);
}
@Override
public double getSurfaceArea() {
return Math.PI*getLength()*getLength();
}
@Override
public double getVolume() {
return Math.PI*getLength()*getLength()*getLength()/6.0;
}
public String toString()
{
return "Shape: Sphere, Diameter: "+getLength();
}
}
//===========================================================================
Tetrahedron Class:
public class Tetrahedron extends ThreeDimensionalShape
{
public Tetrahedron()
{
super();
}
public Tetrahedron(double side)
{
super(side);
}
@Override
public double getSurfaceArea() {
return Math.sqrt(3)*getLength()*getLength();
}
@Override
public double getVolume() {
return getLength()*getLength()*getLength()/(6.0*Math.sqrt(2));
}
public String toString()
{
return "Shape: Tetrahedron, Side: "+getLength();
}
}
//==================================================================
Driver Class:
public class Driver
{
public static void main(String[] args)
{
Shape[] shapes = new Shape[6];
shapes[0] = new Circle();
shapes[1] = new Square(2);
shapes[2] = new Triangle(3);
shapes[3] = new Sphere(4);
shapes[4] = new Cube(5);
shapes[5] = new Tetrahedron(6);
System.out.println("==============Shape Details===============");
for(int i=0;i<6;i++)
{
System.out.println(shapes[i].toString());
if(shapes[i] instanceof TwoDimensionalShape)
{
System.out.println("Area: "+((TwoDimensionalShape)shapes[i]).getArea());
}
else
{
System.out.println("Surface Area: "+((ThreeDimensionalShape)shapes[i]).getSurfaceArea());
System.out.println("Volume: "+((ThreeDimensionalShape)shapes[i]).getVolume());
}
System.out.println("-------------------------------------");
}
System.out.println("==============================================");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.