Skip to content

Commit 0195577

Browse files
committed
Added surface area calculation for pyramid
1 parent 21eff8a commit 0195577

File tree

1 file changed

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

1 file changed

+18
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ public static double surfaceAreaSphere(final double radius) {
4747
}
4848
return 4 * Math.PI * radius * radius;
4949
}
50+
/**
51+
* Calculate the surface area of a pyramid with a square base.
52+
*
53+
* @param sideLength side length of the square base
54+
* @param slantHeight slant height of the pyramid
55+
* @return surface area of the given pyramid
56+
*/
57+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
58+
if (sideLength <= 0) {
59+
throw new IllegalArgumentException("Must be a positive sideLength");
60+
}
61+
if (slantHeight <= 0) {
62+
throw new IllegalArgumentException("Must be a positive slantHeight");
63+
}
64+
double baseArea = sideLength * sideLength;
65+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
66+
return baseArea + lateralSurfaceArea;
67+
}
5068

5169
/**
5270
* Calculate the area of a rectangle.

0 commit comments

Comments
 (0)