Skip to content

Commit 53a7e18

Browse files
committed
Added surface area calc for pyramid
1 parent 2319794 commit 53a7e18

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

  • src/main/java/com/thealgorithms/maths

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,24 @@ public static double surfaceAreaCone(final double radius, final double height) {
232232
}
233233
return Math.PI * radius * (radius + Math.pow(height * height + radius * radius, 0.5));
234234
}
235+
236+
237+
/**
238+
* Calculate the surface area of a pyramid with a square base.
239+
*
240+
* @param sideLength side length of the square base
241+
* @param slantHeight slant height of the pyramid
242+
* @return surface area of the given pyramid
243+
*/
244+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
245+
if (sideLength <= 0) {
246+
throw new IllegalArgumentException("Must be a positive sideLength");
247+
}
248+
if (slantHeight <= 0) {
249+
throw new IllegalArgumentException("Must be a positive slantHeight");
250+
}
251+
double baseArea = sideLength * sideLength;
252+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
253+
return baseArea + lateralSurfaceArea;
254+
}
235255
}

0 commit comments

Comments
 (0)