//Shape 3D
public interface Shape3D {
public double getArea();
public double getVolume();
public String toString();
public boolean equals(Object obj);
}
//CircularShape
public abstract class CircularShape implements Shape3D {
private double radius;
public CircularShape() {
radius = 0.0;
}
public CircularShape(double r) {
radius = r;
}
public double getDiameter() {
return radius * 2.0;
}
public double getRadius() {
return radius;
}
public double getCrossSectionArea() {
return Math.PI * Math.pow(radius, 2);
}
public double getCrossSectionPerimeter() {
return 2 * Math.PI * radius;
}
}
//CircularShapewithheight
public abstract class CircularShapeWithHeight extends CircularShape {
private double height;
public CircularShapeWithHeight() {
super();
height = 1;
}
public CircularShapeWithHeight(double radius, double h) {
super(radius);
height = h;
}
public double getHeight() {
return height;
}
}
//CircularCone
public class CircularCone extends CircularShapeWithHeight {
public CircularCone() {
}
public CircularCone(double radius, double height) {
super(radius, height);
}
public double getArea() {
double r = getRadius();
double h = getHeight();
return Math.PI * r * Math.sqrt(r * r + h * h);
}
public double getVolume() {
return getCrossSectionArea() * getHeight() / 3.0;
}
public String toString() {
return "CircularCone radius = " + super.getRadius() + " height " + super.getHeight();
}
public boolean equals(Object obj) {
if (obj instanceof CircularCone) {
CircularCone c = (CircularCone)obj;
return super.equals(c) && getHeight() == c.getHeight();
}
return false;
}
}
//Cube
public class Cube extends RectangularPrism {
public Cube() {
super();
}
public Cube(double size) {
super(size, size, size);
}
public String toString() {
return "Cube size = " + getLength();
}
}
//Cylinder
public class Cylinder extends CircularShapeWithHeight {
public Cylinder() {
super();
}
public Cylinder(double radius, double height) {
super(radius, height);
}
public double getArea() {
return getCrossSectionPerimeter() * getHeight() + 2 * getCrossSectionArea();
}
public double getVolume() {
return getCrossSectionArea() * getHeight();
}
public String toString() {
return "Cylinder: radius = " + getRadius() + ", height = " + getHeight();
}
public boolean equals(Object obj) {
if (obj instanceof Cylinder) {
Cylinder other = (Cylinder) obj;
return getRadius() == other.getRadius() && getHeight() == other.getHeight();
} else
return false;
}
}
//RectangularPrism
public class RectangularPrism implements Shape3D {
private double length;
private double width;
private double height;
public RectangularPrism() {
length = 1.0;
width = 1.0;
height = 1.0;
}
public RectangularPrism(double l, double w, double h) {
length = l;
width = w;
height = h;
}
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public double getArea() {
return 2 * (length * width + width * height + length * height);
}
public double getVolume() {
return length * width * height;
}
public String toString() {
return "RectangularPrism [length = " + length + ", width = " + width + ", height = " + height + "]";
}
public boolean equals(Object obj) {
if (obj instanceof RectangularPrism) {
RectangularPrism r = (RectangularPrism)obj;
return (length == r.length) && (width == r.width) && (height == r.height);
}
return false;
}
}
//Sphere
public class Sphere implements Shape3D {
private double radius;
public Sphere() {
radius = 1.0;
}
public Sphere(double r) {
radius = r;
}
public double getRadius() {
return radius;
}
public double getArea() {
return 4 * Math.PI * Math.pow(radius, 2);
}
public double getVolume() {
return 4.0 * Math.PI * Math.pow(radius, 3) / 3.0;
}
public String toString() {
return "Sphere of radius " + radius;
}
public boolean equals(Object obj) {
if (obj instanceof Sphere) {
Sphere s = (Sphere)obj;
return radius == s.radius;
}
return false;
}
}
//SquarePyramid
/*
* Benis Munezero
* COSC 237.001
* Lab 8, Polymorphism, 3D shapes
*/
public class SquarePyramid implements Shape3D {
private double length;
private double height;
public SquarePyramid() {
length = 0;
height = 0;
}
public SquarePyramid(double l, double h) {
length = l;
height = h;
}
public double getLength() {
return length;
}
public double getHeight() {
return height;
}
public double getArea() {
return length * (length + Math.sqrt(length * length + 4 * height * height));
}
public double getVolume() {
return length * length * height / 3.0;
}
public String toString() {
return "Square Pyramid: length = " + length + "width = x" + height;
}
public boolean equals(Object obj) {
if (obj instanceof SquarePyramid) {
SquarePyramid other = (SquarePyramid) obj;
return (length == other.length) && (height == other.height);
} else
return false;
}
}
//Shape3DClient
public class Shape3D_Client {
public static final int MAX = 6;
public static void main(String[] args) {
Shape3D[] shapes = new Shape3D[MAX];
shapes[0] = new SquarePyramid(37, 20);
shapes[1] = new Sphere(20);
shapes[2] = new RectangularPrism(10, 20, 37);
shapes[3] = new Cube(10);
shapes[4] = new Cylinder(10, 20);
shapes[5] = new CircularCone(10, 20);
for (int i = 0; i < shapes.length; i++) {
System.out.print("\nThis is a ");
switch(i) {
case 0:
System.out.print("square pyramid. ");
break;
case 1:
System.out.print("sphere. ");
break;
case 2:
System.out.print("rectangular prism. ");
break;
case 3:
System.out.print("cube. ");
break;
case 4:
System.out.print("cylinder. ");
break;
case 5:
System.out.print("circular cone. ");
}
System.out.printf("Area = %.2f", shapes[i].getArea());
System.out.printf(". Volume = %.2f\n", shapes[i].getVolume());
System.out.println("Output calling the method printInfo - polymorphism at work!");
printInfo(shapes[i]);
System.out.println("------------------------------------------------------");
}
}
public static void printInfo(Shape3D s) {
System.out.println(s);
System.out.printf("Area = %.2f", s.getArea());
System.out.printf(". Volume = %.2f\n", s.getVolume());
}
}