Skip to content

Commit c7fe8ee

Browse files
committed
Added surface area calculation for pyramid
1 parent 33dee07 commit c7fe8ee

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
@@ -192,4 +192,23 @@ public static double surfaceAreaCone(final double radius, final double height) {
192192
}
193193
return Math.PI * radius * (radius + Math.pow(height * height + radius * radius, 0.5));
194194
}
195+
196+
/**
197+
* Calculate the surface area of a pyramid with a square base.
198+
*
199+
* @param sideLength side length of the square base
200+
* @param slantHeight slant height of the pyramid
201+
* @return surface area of the given pyramid
202+
*/
203+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
204+
if (sideLength <= 0) {
205+
throw new IllegalArgumentException("Must be a positive sideLength");
206+
}
207+
if (slantHeight <= 0) {
208+
throw new IllegalArgumentException("Must be a positive slantHeight");
209+
}
210+
double baseArea = sideLength * sideLength;
211+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
212+
return baseArea + lateralSurfaceArea;
213+
}
195214
}

0 commit comments

Comments
 (0)