|
4 | 4 | * Find the area of various geometric shapes |
5 | 5 | */ |
6 | 6 | public final class Area { |
7 | | - private Area() { |
8 | | - } |
9 | | - |
10 | | - /** |
11 | | - * String of IllegalArgumentException for radius |
12 | | - */ |
13 | | - private static final String POSITIVE_RADIUS = "Must be a positive radius"; |
14 | | - |
15 | | - /** |
16 | | - * String of IllegalArgumentException for height |
17 | | - */ |
18 | | - private static final String POSITIVE_HEIGHT = "Must be a positive height"; |
19 | | - |
20 | | - /** |
21 | | - * String of IllegalArgumentException for base |
22 | | - */ |
23 | | - private static final String POSITIVE_BASE = "Must be a positive base"; |
24 | | - |
25 | | - /** |
26 | | - * Calculate the surface area of a cube. |
27 | | - * |
28 | | - * @param sideLength side length of cube |
29 | | - * @return surface area of given cube |
30 | | - */ |
31 | | - public static double surfaceAreaCube(final double sideLength) { |
32 | | - if (sideLength <= 0) { |
33 | | - throw new IllegalArgumentException("Must be a positive sideLength"); |
34 | | - } |
35 | | - return 6 * sideLength * sideLength; |
36 | | - } |
37 | | - |
38 | | - /** |
39 | | - * Calculate the surface area of a sphere. |
40 | | - * |
41 | | - * @param radius radius of sphere |
42 | | - * @return surface area of given sphere |
43 | | - */ |
44 | | - public static double surfaceAreaSphere(final double radius) { |
45 | | - if (radius <= 0) { |
46 | | - throw new IllegalArgumentException(POSITIVE_RADIUS); |
47 | | - } |
48 | | - return 4 * Math.PI * radius * radius; |
49 | | - } |
50 | | - |
51 | | - /** |
52 | | - * Calculate the area of a rectangle. |
53 | | - * |
54 | | - * @param length length of a rectangle |
55 | | - * @param width width of a rectangle |
56 | | - * @return area of given rectangle |
57 | | - */ |
58 | | - public static double surfaceAreaRectangle(final double length, final double width) { |
59 | | - if (length <= 0) { |
60 | | - throw new IllegalArgumentException("Must be a positive length"); |
61 | | - } |
62 | | - if (width <= 0) { |
63 | | - throw new IllegalArgumentException("Must be a positive width"); |
64 | | - } |
65 | | - return length * width; |
66 | | - } |
67 | | - |
68 | | - /** |
69 | | - * Calculate surface area of a cylinder. |
70 | | - * |
71 | | - * @param radius radius of the floor |
72 | | - * @param height height of the cylinder. |
73 | | - * @return volume of given cylinder |
74 | | - */ |
75 | | - public static double surfaceAreaCylinder(final double radius, final double height) { |
76 | | - if (radius <= 0) { |
77 | | - throw new IllegalArgumentException(POSITIVE_RADIUS); |
78 | | - } |
79 | | - if (height <= 0) { |
80 | | - throw new IllegalArgumentException(POSITIVE_RADIUS); |
81 | | - } |
82 | | - return 2 * (Math.PI * radius * radius + Math.PI * radius * height); |
83 | | - } |
84 | | - |
85 | | - /** |
86 | | - * Calculate the area of a square. |
87 | | - * |
88 | | - * @param sideLength side length of square |
89 | | - * @return area of given square |
90 | | - */ |
91 | | - public static double surfaceAreaSquare(final double sideLength) { |
92 | | - if (sideLength <= 0) { |
93 | | - throw new IllegalArgumentException("Must be a positive sideLength"); |
94 | | - } |
95 | | - return sideLength * sideLength; |
96 | | - } |
97 | | - |
98 | | - /** |
99 | | - * Calculate the area of a triangle. |
100 | | - * |
101 | | - * @param base base of triangle |
102 | | - * @param height height of triangle |
103 | | - * @return area of given triangle |
104 | | - */ |
105 | | - public static double surfaceAreaTriangle(final double base, final double height) { |
106 | | - if (base <= 0) { |
107 | | - throw new IllegalArgumentException(POSITIVE_BASE); |
108 | | - } |
109 | | - if (height <= 0) { |
110 | | - throw new IllegalArgumentException(POSITIVE_HEIGHT); |
111 | | - } |
112 | | - return base * height / 2; |
113 | | - } |
114 | | - |
115 | | - /** |
116 | | - * Calculate the area of a parallelogram. |
117 | | - * |
118 | | - * @param base base of a parallelogram |
119 | | - * @param height height of a parallelogram |
120 | | - * @return area of given parallelogram |
121 | | - */ |
122 | | - public static double surfaceAreaParallelogram(final double base, final double height) { |
123 | | - if (base <= 0) { |
124 | | - throw new IllegalArgumentException(POSITIVE_BASE); |
125 | | - } |
126 | | - if (height <= 0) { |
127 | | - throw new IllegalArgumentException(POSITIVE_HEIGHT); |
128 | | - } |
129 | | - return base * height; |
130 | | - } |
131 | | - |
132 | | - /** |
133 | | - * Calculate the area of a trapezium. |
134 | | - * |
135 | | - * @param base1 upper base of trapezium |
136 | | - * @param base2 bottom base of trapezium |
137 | | - * @param height height of trapezium |
138 | | - * @return area of given trapezium |
139 | | - */ |
140 | | - public static double surfaceAreaTrapezium(final double base1, final double base2, final double height) { |
141 | | - if (base1 <= 0) { |
142 | | - throw new IllegalArgumentException(POSITIVE_BASE + 1); |
143 | | - } |
144 | | - if (base2 <= 0) { |
145 | | - throw new IllegalArgumentException(POSITIVE_BASE + 2); |
146 | | - } |
147 | | - if (height <= 0) { |
148 | | - throw new IllegalArgumentException(POSITIVE_HEIGHT); |
149 | | - } |
150 | | - return (base1 + base2) * height / 2; |
151 | | - } |
152 | | - |
153 | | - /** |
154 | | - * Calculate the area of a circle. |
155 | | - * |
156 | | - * @param radius radius of circle |
157 | | - * @return area of given circle |
158 | | - */ |
159 | | - public static double surfaceAreaCircle(final double radius) { |
160 | | - if (radius <= 0) { |
161 | | - throw new IllegalArgumentException(POSITIVE_RADIUS); |
162 | | - } |
163 | | - return Math.PI * radius * radius; |
164 | | - } |
165 | | - |
166 | | - /** |
167 | | - * Calculate the surface area of a hemisphere. |
168 | | - * |
169 | | - * @param radius radius of hemisphere |
170 | | - * @return surface area of given hemisphere |
171 | | - */ |
172 | | - public static double surfaceAreaHemisphere(final double radius) { |
173 | | - if (radius <= 0) { |
174 | | - throw new IllegalArgumentException(POSITIVE_RADIUS); |
175 | | - } |
176 | | - return 3 * Math.PI * radius * radius; |
177 | | - } |
178 | | - |
179 | | - /** |
180 | | - * Calculate the surface area of a cone. |
181 | | - * |
182 | | - * @param radius radius of cone. |
183 | | - * @param height of cone. |
184 | | - * @return surface area of given cone. |
185 | | - */ |
186 | | - public static double surfaceAreaCone(final double radius, final double height) { |
187 | | - if (radius <= 0) { |
188 | | - throw new IllegalArgumentException(POSITIVE_RADIUS); |
189 | | - } |
190 | | - if (height <= 0) { |
191 | | - throw new IllegalArgumentException(POSITIVE_HEIGHT); |
192 | | - } |
193 | | - return Math.PI * radius * (radius + Math.pow(height * height + radius * radius, 0.5)); |
194 | | - } |
| 7 | + private Area() { |
| 8 | + } |
| 9 | + |
| 10 | + /** |
| 11 | + * String of IllegalArgumentException for radius |
| 12 | + */ |
| 13 | + private static final String POSITIVE_RADIUS = "Must be a positive radius"; |
| 14 | + |
| 15 | + /** |
| 16 | + * String of IllegalArgumentException for height |
| 17 | + */ |
| 18 | + private static final String POSITIVE_HEIGHT = "Must be a positive height"; |
| 19 | + |
| 20 | + /** |
| 21 | + * String of IllegalArgumentException for base |
| 22 | + */ |
| 23 | + private static final String POSITIVE_BASE = "Must be a positive base"; |
| 24 | + |
| 25 | + /** |
| 26 | + * Calculate the surface area of a cube. |
| 27 | + * |
| 28 | + * @param sideLength side length of cube |
| 29 | + * @return surface area of given cube |
| 30 | + */ |
| 31 | + public static double surfaceAreaCube(final double sideLength) { |
| 32 | + if (sideLength <= 0) { |
| 33 | + throw new IllegalArgumentException("Must be a positive sideLength"); |
| 34 | + } |
| 35 | + return 6 * sideLength * sideLength; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Calculate the surface area of a sphere. |
| 40 | + * |
| 41 | + * @param radius radius of sphere |
| 42 | + * @return surface area of given sphere |
| 43 | + */ |
| 44 | + public static double surfaceAreaSphere(final double radius) { |
| 45 | + if (radius <= 0) { |
| 46 | + throw new IllegalArgumentException(POSITIVE_RADIUS); |
| 47 | + } |
| 48 | + return 4 * Math.PI * radius * radius; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Calculate the area of a rectangle. |
| 53 | + * |
| 54 | + * @param length length of a rectangle |
| 55 | + * @param width width of a rectangle |
| 56 | + * @return area of given rectangle |
| 57 | + */ |
| 58 | + public static double surfaceAreaRectangle(final double length, final double width) { |
| 59 | + if (length <= 0) { |
| 60 | + throw new IllegalArgumentException("Must be a positive length"); |
| 61 | + } |
| 62 | + if (width <= 0) { |
| 63 | + throw new IllegalArgumentException("Must be a positive width"); |
| 64 | + } |
| 65 | + return length * width; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Calculate surface area of a cylinder. |
| 70 | + * |
| 71 | + * @param radius radius of the floor |
| 72 | + * @param height height of the cylinder. |
| 73 | + * @return volume of given cylinder |
| 74 | + */ |
| 75 | + public static double surfaceAreaCylinder(final double radius, final double height) { |
| 76 | + if (radius <= 0) { |
| 77 | + throw new IllegalArgumentException(POSITIVE_RADIUS); |
| 78 | + } |
| 79 | + if (height <= 0) { |
| 80 | + throw new IllegalArgumentException(POSITIVE_RADIUS); |
| 81 | + } |
| 82 | + return 2 * (Math.PI * radius * radius + Math.PI * radius * height); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Calculate the area of a square. |
| 87 | + * |
| 88 | + * @param sideLength side length of square |
| 89 | + * @return area of given square |
| 90 | + */ |
| 91 | + public static double surfaceAreaSquare(final double sideLength) { |
| 92 | + if (sideLength <= 0) { |
| 93 | + throw new IllegalArgumentException("Must be a positive sideLength"); |
| 94 | + } |
| 95 | + return sideLength * sideLength; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Calculate the area of a triangle. |
| 100 | + * |
| 101 | + * @param base base of triangle |
| 102 | + * @param height height of triangle |
| 103 | + * @return area of given triangle |
| 104 | + */ |
| 105 | + public static double surfaceAreaTriangle(final double base, final double height) { |
| 106 | + if (base <= 0) { |
| 107 | + throw new IllegalArgumentException(POSITIVE_BASE); |
| 108 | + } |
| 109 | + if (height <= 0) { |
| 110 | + throw new IllegalArgumentException(POSITIVE_HEIGHT); |
| 111 | + } |
| 112 | + return base * height / 2; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Calculate the area of a parallelogram. |
| 117 | + * |
| 118 | + * @param base base of a parallelogram |
| 119 | + * @param height height of a parallelogram |
| 120 | + * @return area of given parallelogram |
| 121 | + */ |
| 122 | + public static double surfaceAreaParallelogram(final double base, final double height) { |
| 123 | + if (base <= 0) { |
| 124 | + throw new IllegalArgumentException(POSITIVE_BASE); |
| 125 | + } |
| 126 | + if (height <= 0) { |
| 127 | + throw new IllegalArgumentException(POSITIVE_HEIGHT); |
| 128 | + } |
| 129 | + return base * height; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Calculate the area of a trapezium. |
| 134 | + * |
| 135 | + * @param base1 upper base of trapezium |
| 136 | + * @param base2 bottom base of trapezium |
| 137 | + * @param height height of trapezium |
| 138 | + * @return area of given trapezium |
| 139 | + */ |
| 140 | + public static double surfaceAreaTrapezium(final double base1, final double base2, final double height) { |
| 141 | + if (base1 <= 0) { |
| 142 | + throw new IllegalArgumentException(POSITIVE_BASE + 1); |
| 143 | + } |
| 144 | + if (base2 <= 0) { |
| 145 | + throw new IllegalArgumentException(POSITIVE_BASE + 2); |
| 146 | + } |
| 147 | + if (height <= 0) { |
| 148 | + throw new IllegalArgumentException(POSITIVE_HEIGHT); |
| 149 | + } |
| 150 | + return (base1 + base2) * height / 2; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Calculate the area of a circle. |
| 155 | + * |
| 156 | + * @param radius radius of circle |
| 157 | + * @return area of given circle |
| 158 | + */ |
| 159 | + public static double surfaceAreaCircle(final double radius) { |
| 160 | + if (radius <= 0) { |
| 161 | + throw new IllegalArgumentException(POSITIVE_RADIUS); |
| 162 | + } |
| 163 | + return Math.PI * radius * radius; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Calculate the surface area of a hemisphere. |
| 168 | + * |
| 169 | + * @param radius radius of hemisphere |
| 170 | + * @return surface area of given hemisphere |
| 171 | + */ |
| 172 | + public static double surfaceAreaHemisphere(final double radius) { |
| 173 | + if (radius <= 0) { |
| 174 | + throw new IllegalArgumentException(POSITIVE_RADIUS); |
| 175 | + } |
| 176 | + return 3 * Math.PI * radius * radius; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Calculate the surface area of a cone. |
| 181 | + * |
| 182 | + * @param radius radius of cone. |
| 183 | + * @param height of cone. |
| 184 | + * @return surface area of given cone. |
| 185 | + */ |
| 186 | + public static double surfaceAreaCone(final double radius, final double height) { |
| 187 | + if (radius <= 0) { |
| 188 | + throw new IllegalArgumentException(POSITIVE_RADIUS); |
| 189 | + } |
| 190 | + if (height <= 0) { |
| 191 | + throw new IllegalArgumentException(POSITIVE_HEIGHT); |
| 192 | + } |
| 193 | + return Math.PI * radius * (radius + Math.pow(height * height + radius * radius, 0.5)); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Calculate the surface area of a pyramid with a square base. |
| 198 | + * |
| 199 | + * @param sideLength side length of the square base |
| 200 | + * @param slantHeight slant height of the pyramid |
| 201 | + * @return surface area of the given pyramid |
| 202 | + */ |
| 203 | + public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) { |
| 204 | + if (sideLength <= 0) { |
| 205 | + throw new IllegalArgumentException("Must be a positive sideLength"); |
| 206 | + } |
| 207 | + if (slantHeight <= 0) { |
| 208 | + throw new IllegalArgumentException("Must be a positive slantHeight"); |
| 209 | + } |
| 210 | + double baseArea = sideLength * sideLength; |
| 211 | + double lateralSurfaceArea = 2 * sideLength * slantHeight; |
| 212 | + return baseArea + lateralSurfaceArea; |
| 213 | + } |
195 | 214 | } |
0 commit comments