Create class definitions using the following: Classes: Circle, Cube, Shape, Sphe
ID: 3709408 • Letter: C
Question
Create class definitions using the following:
Classes: Circle, Cube, Shape, Sphere, Square, ThreeDimensionalShape, TwoDimensionalShape
Functions: Draw, GetArea, GetDimensions, GetHeight, GetLength, GetSurfaceArea, GetVolume
Members: height, length, width – all of type double
Don’t actually write function implementations, just define the classes as you would in a header file. Use inheritance as appropriate. Use keywords such as “const” and “virtual” as appropriate. Make functions and members public, private, or protected as appropriate. Don’t forget constructors and destructors. Some classes may be abstract, meaning some functions may be pure virtual. Hopefully, given your knowledge of shapes, you can determine which Functions and Members belong to which classes.
Explanation / Answer
//Shape
package ShapeHierarchy;
public abstract class Shape
{
private double length;
private double width;
public Shape()
{
length = 0;
width = 0;
}
public Shape(double len)
{
setLength(len);
}
public Shape(double len, double wid)
{
setLength(len);
setWidth(wid);
}
public void setLength(double len)
{
length = len > 0? len : 0;
}
public double getLength()
{
return length;
}
public void setWidth(double wid)
{
width = wid > 0? wid : 0;
}
public double getWidth()
{
return width;
}
public abstract double getArea();
}
// TwoDimensionalShape
package ShapeHierarchy;
public abstract class TwoDimensionalShape extends Shape
{
public TwoDimensionalShape()
{}
public TwoDimensionalShape(double len)
{
super(len);
}
public TwoDimensionalShape(double len, double wid)
{
super(len, wid);
}
public abstract double getArea();
}
// Square
package ShapeHierarchy;
public class Square extends TwoDimensionalShape
{
public Square(double len)
{
super(len);
}
@Override
public double getArea()
{
return super.getLength() * super.getLength();
}
@Override
public String toString()
{
return String.format("Square Side: %.2f ",
super.getLength());
}
}
// Circle
package ShapeHierarchy;
public class Circle extends TwoDimensionalShape
{
private final double PI = 3.14159;
public Circle(double len)
{
super(len);
}
@Override
public double getArea()
{
return PI * super.getLength() * super.getLength();
}
@Override
public String toString()
{
return String.format("Circle Radius: %.2f ",
super.getLength());
}
}
// ThreeDimensionalShape
package ShapeHierarchy;
public abstract class ThreeDimensionalShape extends Shape
{
private double height;
public ThreeDimensionalShape()
{
height = 0;
}
public ThreeDimensionalShape(double len)
{
super(len);
}
public ThreeDimensionalShape(double len, double wid)
{
super(len, wid);
}
public ThreeDimensionalShape(double len, double wid,
double hgt)
{
super(len, wid);
setHeight(hgt);
}
public void setHeight(double hgt)
{
height = hgt > 0? hgt : 0;
}
public double getHeight()
{
return height;
}
public abstract double getArea();
public abstract double getVolume();
}
// Sphere
package ShapeHierarchy;
public class Sphere extends ThreeDimensionalShape
{
private final double PI = 3.14159;
public Sphere(double len)
{
super(len);
}
@Override
public double getArea()
{
return 4 * PI * super.getLength() * super.getLength();
}
@Override
public double getVolume()
{
return 4.0/3.0 * PI * super.getLength()
* super.getLength() * super.getLength();
}
@Override
public String toString()
{
return String.format("Sphere Radius: %.2f ", super.getLength());
}
}
//Cube
package ShapeHierarchy;
public class Cube extends ThreeDimensionalShape
{
public Cube(double len)
{
super(len);
}
@Override
public double getArea()
{
return super.getLength() * super.getLength();
}
@Override
public double getVolume()
{
return getArea() * super.getLength();
}
@Override
public String toString()
{
return String.format("Cube Side: %.2f ",
super.getLength());
}
}
//Shape
package ShapeHierarchy;
public abstract class Shape
{
private double length;
private double width;
public Shape()
{
length = 0;
width = 0;
}
public Shape(double len)
{
setLength(len);
}
public Shape(double len, double wid)
{
setLength(len);
setWidth(wid);
}
public void setLength(double len)
{
length = len > 0? len : 0;
}
public double getLength()
{
return length;
}
public void setWidth(double wid)
{
width = wid > 0? wid : 0;
}
public double getWidth()
{
return width;
}
public abstract double getArea();
}
// TwoDimensionalShape
package ShapeHierarchy;
public abstract class TwoDimensionalShape extends Shape
{
public TwoDimensionalShape()
{}
public TwoDimensionalShape(double len)
{
super(len);
}
public TwoDimensionalShape(double len, double wid)
{
super(len, wid);
}
public abstract double getArea();
}
// Square
package ShapeHierarchy;
public class Square extends TwoDimensionalShape
{
public Square(double len)
{
super(len);
}
@Override
public double getArea()
{
return super.getLength() * super.getLength();
}
@Override
public String toString()
{
return String.format("Square Side: %.2f ",
super.getLength());
}
}
// Circle
package ShapeHierarchy;
public class Circle extends TwoDimensionalShape
{
private final double PI = 3.14159;
public Circle(double len)
{
super(len);
}
@Override
public double getArea()
{
return PI * super.getLength() * super.getLength();
}
@Override
public String toString()
{
return String.format("Circle Radius: %.2f ",
super.getLength());
}
}
// ThreeDimensionalShape
package ShapeHierarchy;
public abstract class ThreeDimensionalShape extends Shape
{
private double height;
public ThreeDimensionalShape()
{
height = 0;
}
public ThreeDimensionalShape(double len)
{
super(len);
}
public ThreeDimensionalShape(double len, double wid)
{
super(len, wid);
}
public ThreeDimensionalShape(double len, double wid,
double hgt)
{
super(len, wid);
setHeight(hgt);
}
public void setHeight(double hgt)
{
height = hgt > 0? hgt : 0;
}
public double getHeight()
{
return height;
}
public abstract double getArea();
public abstract double getVolume();
}
// Sphere
package ShapeHierarchy;
public class Sphere extends ThreeDimensionalShape
{
private final double PI = 3.14159;
public Sphere(double len)
{
super(len);
}
@Override
public double getArea()
{
return 4 * PI * super.getLength() * super.getLength();
}
@Override
public double getVolume()
{
return 4.0/3.0 * PI * super.getLength()
* super.getLength() * super.getLength();
}
@Override
public String toString()
{
return String.format("Sphere Radius: %.2f ", super.getLength());
}
}
//Cube
package ShapeHierarchy;
public class Cube extends ThreeDimensionalShape
{
public Cube(double len)
{
super(len);
}
@Override
public double getArea()
{
return super.getLength() * super.getLength();
}
@Override
public double getVolume()
{
return getArea() * super.getLength();
}
@Override
public String toString()
{
return String.format("Cube Side: %.2f ",
super.getLength());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.