Skip to content

Commit d3444e8

Browse files
committed
Added surface area calculations for pyramid
1 parent d5289b9 commit d3444e8

File tree

1 file changed

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

1 file changed

+21
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,24 @@ public static double surfaceAreaCone(final double radius, final double height) {
193193
return Math.PI * radius * (radius + Math.pow(height * height + radius * radius, 0.5));
194194
}
195195
}
196+
197+
198+
199+
/**
200+
* Calculate the surface area of a pyramid with a square base.
201+
*
202+
* @param sideLength side length of the square base
203+
* @param slantHeight slant height of the pyramid
204+
* @return surface area of the given pyramid
205+
*/
206+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
207+
if (sideLength <= 0) {
208+
throw new IllegalArgumentException("Must be a positive sideLength");
209+
}
210+
if (slantHeight <= 0) {
211+
throw new IllegalArgumentException("Must be a positive slantHeight");
212+
}
213+
double baseArea = sideLength * sideLength;
214+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
215+
return baseArea + lateralSurfaceArea;
216+
}

0 commit comments

Comments
 (0)