Skip to content

Commit 1e4a1e6

Browse files
committed
Added surface area calculation for pyramid
1 parent d5289b9 commit 1e4a1e6

File tree

1 file changed

+18
-0
lines changed
  • src/main/java/com/thealgorithms/maths

1 file changed

+18
-0
lines changed

src/main/java/com/thealgorithms/maths/Area.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,22 @@ public static double surfaceAreaCone(final double radius, final double height) {
192192
}
193193
return Math.PI * radius * (radius + Math.pow(height * height + radius * radius, 0.5));
194194
}
195+
/**
196+
* Calculate the surface area of a pyramid with a square base.
197+
*
198+
* @param sideLength side length of the square base
199+
* @param slantHeight slant height of the pyramid
200+
* @return surface area of the given pyramid
201+
*/
202+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
203+
if (sideLength <= 0) {
204+
throw new IllegalArgumentException("Must be a positive sideLength");
205+
}
206+
if (slantHeight <= 0) {
207+
throw new IllegalArgumentException("Must be a positive slantHeight");
208+
}
209+
double baseArea = sideLength * sideLength;
210+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
211+
return baseArea + lateralSurfaceArea;
212+
}
195213
}

0 commit comments

Comments
 (0)