Skip to content

Commit 1c59f7a

Browse files
Update Area.java
1 parent 8930369 commit 1c59f7a

File tree

1 file changed

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

1 file changed

+18
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ public static double surfaceAreaCube(final double sideLength) {
3434
}
3535
return 6 * sideLength * sideLength;
3636
}
37+
/**
38+
* Calculate the surface area of a pyramid with a square base.
39+
*
40+
* @param sideLength side length of the square base
41+
* @param slantHeight slant height of the pyramid
42+
* @return surface area of the given pyramid
43+
*/
44+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
45+
if (sideLength <= 0) {
46+
throw new IllegalArgumentException("Must be a positive sideLength");
47+
}
48+
if (slantHeight <= 0) {
49+
throw new IllegalArgumentException("Must be a positive slantHeight");
50+
}
51+
double baseArea = sideLength * sideLength;
52+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
53+
return baseArea + lateralSurfaceArea;
54+
}
3755

3856
/**
3957
* Calculate the surface area of a sphere.

0 commit comments

Comments
 (0)