Skip to content

Commit 6e3eb3c

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

File tree

1 file changed

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

1 file changed

+19
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ private Area() {
2121
* String of IllegalArgumentException for base
2222
*/
2323
private static final String POSITIVE_BASE = "Must be a positive base";
24+
25+
/**
26+
* Calculate the surface area of a pyramid with a square base.
27+
*
28+
* @param sideLength side length of the square base
29+
* @param slantHeight slant height of the pyramid
30+
* @return surface area of the given pyramid
31+
*/
32+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
33+
if (sideLength <= 0) {
34+
throw new IllegalArgumentException("Must be a positive sideLength");
35+
}
36+
if (slantHeight <= 0) {
37+
throw new IllegalArgumentException("Must be a positive slantHeight");
38+
}
39+
double baseArea = sideLength * sideLength;
40+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
41+
return baseArea + lateralSurfaceArea;
42+
}
2443

2544
/**
2645
* Calculate the surface area of a cube.

0 commit comments

Comments
 (0)