|
| 1 | + |
1 | 2 | package com.thealgorithms.maths; |
2 | 3 |
|
3 | 4 | /** |
@@ -69,30 +70,11 @@ public static double surfaceAreaSphere(final double radius) { |
69 | 70 | return 4 * Math.PI * radius * radius; |
70 | 71 | } |
71 | 72 |
|
72 | | - /** |
73 | | - * Calculate the surface area of a pyramid with a square base. |
74 | | - * |
75 | | - * @param sideLength side length of the square base |
76 | | - * @param slantHeight slant height of the pyramid |
77 | | - * @return surface area of the given pyramid |
78 | | - */ |
79 | | - public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) { |
80 | | - if (sideLength <= 0) { |
81 | | - throw new IllegalArgumentException(""); |
82 | | - } |
83 | | - if (slantHeight <= 0) { |
84 | | - throw new IllegalArgumentException("slant height must be greater than 0"); |
85 | | - } |
86 | | - double baseArea = sideLength * sideLength; |
87 | | - double lateralSurfaceArea = 2 * sideLength * slantHeight; |
88 | | - return baseArea + lateralSurfaceArea; |
89 | | - } |
90 | | - |
91 | 73 | /** |
92 | 74 | * Calculate the area of a rectangle. |
93 | 75 | * |
94 | 76 | * @param length length of a rectangle |
95 | | - * @param width width of a rectangle |
| 77 | + * @param width width of a rectangle |
96 | 78 | * @return area of given rectangle |
97 | 79 | */ |
98 | 80 | public static double surfaceAreaRectangle(final double length, final double width) { |
@@ -232,4 +214,23 @@ public static double surfaceAreaCone(final double radius, final double height) { |
232 | 214 | } |
233 | 215 | return Math.PI * radius * (radius + Math.pow(height * height + radius * radius, 0.5)); |
234 | 216 | } |
| 217 | + |
| 218 | + /** |
| 219 | + * Calculate the surface area of a pyramid with a square base. |
| 220 | + * |
| 221 | + * @param sideLength side length of the square base |
| 222 | + * @param slantHeight slant height of the pyramid |
| 223 | + * @return surface area of the given pyramid |
| 224 | + */ |
| 225 | + public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) { |
| 226 | + if (sideLength <= 0) { |
| 227 | + throw new IllegalArgumentException("Must be a positive sideLength"); |
| 228 | + } |
| 229 | + if (slantHeight <= 0) { |
| 230 | + throw new IllegalArgumentException("Must be a positive slantHeight"); |
| 231 | + } |
| 232 | + double baseArea = sideLength * sideLength; |
| 233 | + double lateralSurfaceArea = 2 * sideLength * slantHeight; |
| 234 | + return baseArea + lateralSurfaceArea; |
| 235 | + } |
235 | 236 | } |
0 commit comments