Skip to content

Commit dfd8d69

Browse files
Added surface area calculation for pyramid (#6853)
Co-authored-by: JonathanButterworth <JonathanButterworth>
1 parent bb6385e commit dfd8d69

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
@@ -48,6 +48,25 @@ public static double surfaceAreaSphere(final double radius) {
4848
return 4 * Math.PI * radius * radius;
4949
}
5050

51+
/**
52+
* Calculate the surface area of a pyramid with a square base.
53+
*
54+
* @param sideLength side length of the square base
55+
* @param slantHeight slant height of the pyramid
56+
* @return surface area of the given pyramid
57+
*/
58+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
59+
if (sideLength <= 0) {
60+
throw new IllegalArgumentException("Must be a positive sideLength");
61+
}
62+
if (slantHeight <= 0) {
63+
throw new IllegalArgumentException("Must be a positive slantHeight");
64+
}
65+
double baseArea = sideLength * sideLength;
66+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
67+
return baseArea + lateralSurfaceArea;
68+
}
69+
5170
/**
5271
* Calculate the area of a rectangle.
5372
*

0 commit comments

Comments
 (0)