Skip to content

Commit 6408120

Browse files
committed
Added surface area calculation for pyramid
1 parent d5289b9 commit 6408120

File tree

1 file changed

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

1 file changed

+20
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,26 @@ public static double surfaceAreaHemisphere(final double radius) {
175175
}
176176
return 3 * Math.PI * radius * radius;
177177
}
178+
179+
/**
180+
* Calculate the surface area of a pyramid with a square base.
181+
*
182+
* @param sideLength side length of the square base
183+
* @param slantHeight slant height of the pyramid
184+
* @return surface area of the given pyramid
185+
*/
186+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
187+
if (sideLength <= 0) {
188+
throw new IllegalArgumentException("Must be a positive sideLength");
189+
}
190+
if (slantHeight <= 0) {
191+
throw new IllegalArgumentException("Must be a positive slantHeight");
192+
}
193+
double baseArea = sideLength * sideLength;
194+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
195+
return baseArea + lateralSurfaceArea;
196+
}
197+
178198

179199
/**
180200
* Calculate the surface area of a cone.

0 commit comments

Comments
 (0)