Skip to content

Commit 1af8bba

Browse files
committed
Added surface area calculation for pyramid
1 parent 03bb8ee commit 1af8bba

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
@@ -35,6 +35,25 @@ public static double surfaceAreaCube(final double sideLength) {
3535
return 6 * sideLength * sideLength;
3636
}
3737

38+
/**
39+
* Calculate the surface area of a pyramid with a square base.
40+
*
41+
* @param sideLength side length of the square base
42+
* @param slantHeight slant height of the pyramid
43+
* @return surface area of the given pyramid
44+
*/
45+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
46+
if (sideLength <= 0) {
47+
throw new IllegalArgumentException("Must be a positive sideLength");
48+
}
49+
if (slantHeight <= 0) {
50+
throw new IllegalArgumentException("Must be a positive slantHeight");
51+
}
52+
double baseArea = sideLength * sideLength;
53+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
54+
return baseArea + lateralSurfaceArea;
55+
}
56+
3857
/**
3958
* Calculate the surface area of a sphere.
4059
*

0 commit comments

Comments
 (0)