Skip to content

Commit c704611

Browse files
committed
Added surface area calculation for pyramid
1 parent a7a2441 commit c704611

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

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

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,23 @@ 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+
* Calculate the surface area of a pyramid with a square base.
238+
*
239+
* @param sideLength side length of the square base
240+
* @param slantHeight slant height of the pyramid
241+
* @return surface area of the given pyramid
242+
*/
243+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
244+
if (sideLength <= 0) {
245+
throw new IllegalArgumentException("Must be a positive sideLength");
246+
}
247+
if (slantHeight <= 0) {
248+
throw new IllegalArgumentException("Must be a positive slantHeight");
249+
}
250+
double baseArea = sideLength * sideLength;
251+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
252+
return baseArea + lateralSurfaceArea;
253+
}
235254
}

0 commit comments

Comments
 (0)