Skip to content

Commit 94b1a9c

Browse files
Aymen Al-TahmschiAymen Al-Tahmschi
authored andcommitted
Added surface area calculation for pyramid
1 parent 8930369 commit 94b1a9c

File tree

1 file changed

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

1 file changed

+17
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,21 @@ 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+
* Calculate the surface area of a pyramid with a square base.
197+
* @param sideLength side length of the square base
198+
* @param slantHeight slant height of the pyramid
199+
* @return surface area of the given pyramid
200+
*/
201+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
202+
if (sideLength <= 0) {
203+
throw new IllegalArgumentException("Must be a positive sideLength");
204+
}
205+
if (slantHeight <= 0) {
206+
throw new IllegalArgumentException("Must be a positive slantHeight");
207+
}
208+
double baseArea = sideLength * sideLength;
209+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
210+
return baseArea + lateralSurfaceArea;
211+
}
195212
}

0 commit comments

Comments
 (0)