COP3337 Section U05 Spring 2017 THE SOLID INTERFACE =================== I. The interface package fall2011cop3337solids; // describes a solid public interface Solid { double area(); double volume(); } II. The Cube class package fall2011cop3337solids; // describes a cube public class Cube implements Solid { // the fields private double side; // s is the side public Cube(double s) { side = s; } // get the side public double getSide() { return side; } // return the area public double area() { return 6 * side *side; } // return the volume public double volume() { return side * side * side; } } III. The Sphere class package fall2011cop3337solids; // public class Sphere implements Solid { // the field private double rad; // the radius // the constructor // r is the radius public Sphere(double r) { rad = r; } // return the radius public double getRadius() { return rad; } // return the area public double area() { return 4.0 * Math.PI * rad * rad ; } // return the volume public double volume() { return 4 * Math.PI * rad * rad * rad / 3; } } IV. The Cylinder class package fall2011cop3337solids; // the Cylinder solid public class Cylinder implements Solid { // the fields private double rad; private double height; // th height // the constructor // r is the radius, h the height public Cylinder(double r, double h) { rad = r; height = h; } // get the radius public double getRadius() { return rad; } // return the height public double getHeight() { return height; } // return the area public double area() { return 2 * Math.PI * rad * ( height + rad); } // return the volume public double volume() { return Math.PI * rad * rad * height; } } V. The driver package fall2011cop3337solids; // test solids public class Fall2011cop3337solids { // adriver for solids public static void main(String[] args) { // print the titles System.out.println("Testing Solids"); System.out.println("==============\n\n"); System.out.println("We form 6 solids."); Solid[] shapes = new Solid[6]; shapes[0] = new Sphere(10); shapes[1] = new Cube(10); shapes[2] = new Cylinder(5,10); shapes[3] = new Cube(15); shapes[4] = new Cylinder(10,5); shapes[5] = new Sphere(12); System.out.println("The solids are:"); print(shapes); } // print an array of solids public static void print(Solid[] arr) { // check if the array is empty if (arr == null || arr.length == 0) { System.out.println("The array is empty."); return; } // print the array for (int i = 0; i < arr.length; i++ ) { // print arr[i] if (arr[i] instanceof Sphere) { Sphere s = (Sphere) arr[i]; System.out.println("\nThe solid is a sphere with radius " + s.getRadius() + "."); System.out.println("The area is " + arr[i].area() + ", the volume " + arr[i].volume() + "."); } else if (arr[i] instanceof Cylinder) { Cylinder c = (Cylinder) arr[i]; System.out.println("\nThe object is a cylinder with radius " + c.getRadius() + " and height " + c.getHeight() + "."); System.out.println("The area is " + arr[i].area() + ", the volume " + arr[i].volume() + "."); } else if (arr[i] instanceof Cube) { Cube c = (Cube) arr[i]; System.out.println("\nThe solid is a cube with side " + c.getSide() + "."); System.out.println("The area is " + arr[i].area() + ", the volume " + arr[i].volume() + "."); } else System.out.println("\nUnknown solid."); } } } VI. The output run: Testing Solids ============== We form 6 solids. The solids are: The solid is a sphere with radius 10.0. The area is 1256.6370614359173, the volume 4188.790204786391. The solid is a cube with side 10.0. The area is 600.0, the volume 1000.0. The object is a cylinder with radius 5.0 and height 10.0. The area is 471.23889803846896, the volume 785.3981633974483. The solid is a cube with side 15.0. The area is 1350.0, the volume 3375.0. The object is a cylinder with radius 10.0 and height 5.0. The area is 942.4777960769379, the volume 1570.7963267948967. The solid is a sphere with radius 12.0. The area is 1809.5573684677208, the volume 7238.229473870883. BUILD SUCCESSFUL (total time: 0 seconds)