Skip to content

Commit 71c5a4c

Browse files
committed
Added surface area calculation for pyramid
1 parent 410f087 commit 71c5a4c

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

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

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,24 @@ public static double surfaceAreaCircle(final double radius) {
202202
}
203203
return Math.PI * radius * radius;
204204
}
205+
/**
206+
* Calculate the surface area of a pyramid with a square base.
207+
*
208+
* @param sideLength side length of the square base
209+
* @param slantHeight slant height of the pyramid
210+
* @return surface area of the given pyramid
211+
*/
212+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
213+
if (sideLength <= 0) {
214+
throw new IllegalArgumentException("Must be a positive sideLength");
215+
}
216+
if (slantHeight <= 0) {
217+
throw new IllegalArgumentException("Must be a positive slantHeight");
218+
}
219+
double baseArea = sideLength * sideLength;
220+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
221+
return baseArea + lateralSurfaceArea;
222+
}
205223

206224
/**
207225
* Calculate the surface area of a hemisphere.

0 commit comments

Comments
 (0)