|
| 1 | +packageMaths; |
| 2 | + |
| 3 | + |
| 4 | +/* Find volume of various shapes.*/ |
| 5 | +publicclassVolume { |
| 6 | +publicstaticvoidmain(String[]args) { |
| 7 | + |
| 8 | +/* test cube */ |
| 9 | +assertDouble.compare(volumeCube(7),343.0) ==0; |
| 10 | + |
| 11 | +/* test cuboid */ |
| 12 | +assertDouble.compare(volumeCuboid(2,5,7),70.0) ==0; |
| 13 | + |
| 14 | +/* test sphere */ |
| 15 | +assertDouble.compare(volumeSphere(5),523.5987755982989) ==0; |
| 16 | + |
| 17 | +/* test cylinder */ |
| 18 | +assertDouble.compare(volumeCylinder(1,2),12.566370614359172) ==0; |
| 19 | + |
| 20 | +/* test hemisphere */ |
| 21 | +assertDouble.compare(volumeHemisphere(5),261.79938779914943) ==0; |
| 22 | + |
| 23 | +/* test cone */ |
| 24 | +assertDouble.compare(volumeCone(5,7),916.297857297023) ==0; |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | + * Calculate the volume of a cube. |
| 31 | + * |
| 32 | + * @param sideLength side length of cube |
| 33 | + * @return volume of given cube |
| 34 | + */ |
| 35 | +privatestaticdoublevolumeCube(doublesidelength) { |
| 36 | +returnsidelength *sidelength *sidelength; |
| 37 | + } |
| 38 | + |
| 39 | +/** |
| 40 | + * Calculate the volume of a cuboid. |
| 41 | + * |
| 42 | + * @param width of cuboid |
| 43 | + * @param height of cuboid |
| 44 | + * @param length of cuboid |
| 45 | + * @return volume of given cuboid |
| 46 | + */ |
| 47 | +privatestaticdoublevolumeCuboid(doublewidth,doubleheight,doublelength) { |
| 48 | +returnwidth *height *length; |
| 49 | + } |
| 50 | + |
| 51 | +/** |
| 52 | + * Calculate the volume of a sphere. |
| 53 | + * |
| 54 | + * @param radius radius of sphere |
| 55 | + * @return volume of given sphere |
| 56 | + */ |
| 57 | +privatestaticdoublevolumeSphere(doubleradius) { |
| 58 | +return4 /3 *Math.PI *radius *radius *radius; |
| 59 | + } |
| 60 | + |
| 61 | +/** |
| 62 | + * Calculate volume of a cylinder |
| 63 | + * |
| 64 | + * @param radius radius of the floor |
| 65 | + * @param height height of the cylinder. |
| 66 | + * @return volume of given cylinder |
| 67 | + */ |
| 68 | +privatestaticdoublevolumeCylinder(doubleradius,doubleheight) { |
| 69 | +returnMath.PI *radius *radius *height; |
| 70 | + } |
| 71 | + |
| 72 | +/** |
| 73 | + * Calculate the volume of a hemisphere. |
| 74 | + * |
| 75 | + * @param radius radius of hemisphere |
| 76 | + * @return volume of given hemisphere |
| 77 | + */ |
| 78 | +privatestaticdoublevolumeHemisphere(doubleradius) { |
| 79 | +return2 /3 *Math.PI *radius *radius *radius; |
| 80 | + } |
| 81 | + |
| 82 | +/** |
| 83 | + * Calculate the volume of a cone. |
| 84 | + * |
| 85 | + * @param radius radius of cone. |
| 86 | + * @param height of cone. |
| 87 | + * @return volume of given cone. |
| 88 | + */ |
| 89 | +privatestaticdoublevolumeCone(doubleradius,doubleheight) { |
| 90 | +returnMath.PI *radius *radius *height /3; |
| 91 | + } |
| 92 | +} |