Skip to content

Commit cbd0853

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

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

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

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ public static double surfaceAreaCuboid(final double length, double width, double
5656
return 2 * (length * width + length * height + width * height);
5757
}
5858

59+
/**
60+
* Calculate the surface area of a pyramid with a square base.
61+
*
62+
* @param sideLength side length of the square base
63+
* @param slantHeight slant height of the pyramid
64+
* @return surface area of the given pyramid
65+
*/
66+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
67+
if (sideLength <= 0) {
68+
throw new IllegalArgumentException("Must be a positive sideLength");
69+
}
70+
if (slantHeight <= 0) {
71+
throw new IllegalArgumentException("Must be a positive slantHeight");
72+
}
73+
double baseArea = sideLength * sideLength;
74+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
75+
return baseArea + lateralSurfaceArea;
76+
}
77+
5978
/**
6079
* Calculate the surface area of a sphere.
6180
*

0 commit comments

Comments
 (0)