Question

Create class definitions using the following: Classes: Circle, Cube, Shape, Sphere, Square, ThreeDimensionalShape, TwoDimensionalShape Functions: Draw,...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//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\nSide: %.2f\n",
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\nRadius: %.2f\n",
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\nRadius: %.2f\n", 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\nSide: %.2f\n",
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\nSide: %.2f\n",
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\nRadius: %.2f\n",
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\nRadius: %.2f\n", 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\nSide: %.2f\n",
super.getLength());
}
}

Add a comment
Know the answer?
Add Answer to:
Create class definitions using the following: Classes: Circle, Cube, Shape, Sphere, Square, ThreeDimensionalShape, TwoDimensionalShape Functions: Draw,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT