Skip to content

Commit a955db4

Browse files
committed
added surface area calc for pyramid
1 parent 873dd97 commit a955db4

File tree

1 file changed

+22
-1
lines changed
  • src/main/java/com/thealgorithms/maths

1 file changed

+22
-1
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithms.maths;
1+
Dpackage com.thealgorithms.maths;
22

33
/**
44
* Find the area of various geometric shapes
@@ -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+
* Calculate the surface area of a pyramid with a square base.
200+
*
201+
* @param sideLength side length of the square base
202+
* @param slantHeight slant height of the pyramid
203+
* @return surface area of the given pyramid
204+
*/
205+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
206+
if (sideLength <= 0) {
207+
throw new IllegalArgumentException("Must be a positive sideLength");
208+
}
209+
if (slantHeight <= 0) {
210+
throw new IllegalArgumentException("Must be a positive slantHeight");
211+
}
212+
double baseArea = sideLength * sideLength;
213+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
214+
return baseArea + lateralSurfaceArea;
215+
}
216+

0 commit comments

Comments
 (0)