Skip to content

Commit f075515

Browse files
Added surfaceAreaPyramid_v4 with improved comments and validation
1 parent 8930369 commit f075515

File tree

1 file changed

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

1 file changed

+27
-0
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,31 @@ 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+
* Calculates the surface area of a pyramid with a square base.
197+
* This includes both the base and the four triangular faces.
198+
*
199+
* @param sideLength the length of one side of the square base
200+
* @param slantHeight the slant height of the pyramid
201+
* @return the total surface area of the pyramid
202+
* @throws IllegalArgumentException if any parameter is non-positive
203+
*/
204+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
205+
// Validation checks to ensure inputs are physically valid
206+
if (sideLength <= 0) {
207+
throw new IllegalArgumentException("Side length must be a positive number");
208+
}
209+
if (slantHeight <= 0) {
210+
throw new IllegalArgumentException("Slant height must be a positive number");
211+
}
212+
213+
// Base area (square) = side²
214+
double baseArea = sideLength * sideLength;
215+
216+
// Lateral area = 2 × side × slant height
217+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
218+
219+
// Combine base and lateral areas for total surface area
220+
return baseArea + lateralSurfaceArea;
221+
}
195222
}

0 commit comments

Comments
 (0)