|
| 1 | +package Maths; |
| 2 | + |
| 3 | +/** |
| 4 | + * Find the area of various geometric shapes |
| 5 | + */ |
| 6 | +public class Area { |
| 7 | + public static void main(String[] args) { |
| 8 | + /* test cube */ |
| 9 | + assert surfaceAreaCube(1) == 6; |
| 10 | + |
| 11 | + /* test sphere */ |
| 12 | + assert surfaceAreaSphere(5) == 314.1592653589793; |
| 13 | + assert surfaceAreaSphere(1) == 12.566370614359172; |
| 14 | + |
| 15 | + /* test rectangle */ |
| 16 | + assert surfaceAreaRectangle(10, 20) == 200; |
| 17 | + |
| 18 | + /* test square */ |
| 19 | + assert surfaceAreaSquare(10) == 100; |
| 20 | + |
| 21 | + /* test triangle */ |
| 22 | + assert surfaceAreaTriangle(10, 10) == 50; |
| 23 | + |
| 24 | + /* test parallelogram */ |
| 25 | + assert surfaceAreaParallelogram(10, 20) == 200; |
| 26 | + |
| 27 | + /* test trapezium */ |
| 28 | + assert surfaceAreaTrapezium(10, 20, 30) == 450; |
| 29 | + |
| 30 | + /* test circle */ |
| 31 | + assert surfaceAreaCircle(20) == 1256.6370614359173; |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Calculate the surface area of a cube. |
| 37 | + * |
| 38 | + * @param sideLength side length of cube |
| 39 | + * @return surface area of given cube |
| 40 | + */ |
| 41 | + public static double surfaceAreaCube(double sideLength) { |
| 42 | + return 6 * sideLength * sideLength; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Calculate the surface area of a sphere. |
| 47 | + * |
| 48 | + * @param radius radius of sphere |
| 49 | + * @return surface area of given sphere |
| 50 | + */ |
| 51 | + public static double surfaceAreaSphere(double radius) { |
| 52 | + return 4 * Math.PI * radius * radius; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Calculate the area of a rectangle |
| 57 | + * |
| 58 | + * @param length length of rectangle |
| 59 | + * @param width width of rectangle |
| 60 | + * @return area of given rectangle |
| 61 | + */ |
| 62 | + public static double surfaceAreaRectangle(double length, double width) { |
| 63 | + return length * width; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Calculate the area of a square |
| 68 | + * |
| 69 | + * @param sideLength side length of square |
| 70 | + * @return area of given square |
| 71 | + */ |
| 72 | + public static double surfaceAreaSquare(double sideLength) { |
| 73 | + return sideLength * sideLength; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Calculate the area of a triangle |
| 78 | + * |
| 79 | + * @param base base of triangle |
| 80 | + * @param height height of triangle |
| 81 | + * @return area of given triangle |
| 82 | + */ |
| 83 | + public static double surfaceAreaTriangle(double base, double height) { |
| 84 | + return base * height / 2; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Calculate the area of a parallelogram |
| 89 | + * |
| 90 | + * @param base base of parallelogram |
| 91 | + * @param height height of parallelogram |
| 92 | + * @return area of given parallelogram |
| 93 | + */ |
| 94 | + public static double surfaceAreaParallelogram(double base, double height) { |
| 95 | + return base * height; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Calculate the area of a trapezium |
| 100 | + * |
| 101 | + * @param base1 upper base of trapezium |
| 102 | + * @param base2 bottom base of trapezium |
| 103 | + * @param height height of trapezium |
| 104 | + * @return area of given trapezium |
| 105 | + */ |
| 106 | + public static double surfaceAreaTrapezium(double base1, double base2, double height) { |
| 107 | + return (base1 + base2) * height / 2; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Calculate the area of a circle |
| 112 | + * |
| 113 | + * @param radius radius of circle |
| 114 | + * @return area of given circle |
| 115 | + */ |
| 116 | + public static double surfaceAreaCircle(double radius) { |
| 117 | + return Math.PI * radius * radius; |
| 118 | + } |
| 119 | +} |
0 commit comments