Skip to content

Commit 4d34876

Browse files
Added surfaceAreaPyramid method with validation and comments
1 parent 1874b09 commit 4d34876

File tree

1 file changed

+13
-25
lines changed
  • src/main/java/com/thealgorithms/maths

1 file changed

+13
-25
lines changed

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

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -193,35 +193,23 @@ 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-
* Calculates the total surface area of a pyramid with a square base.
198-
* Includes both the base area and the slanted triangular sides.
196+
/**
197+
* Calculate the surface area of a pyramid with a square base.
199198
*
200-
* @param side the length of one side of the square base
201-
* @param slant the slant height of the pyramid
202-
* @return the total surface area of the pyramid in square units
203-
* @throws IllegalArgumentException if any dimension is zero or negative
199+
* @param sideLength side length of the square base
200+
* @param slantHeight slant height of the pyramid
201+
* @return surface area of the given pyramid
204202
*/
205-
public static double surfaceAreaPyramid(final double side, final double slant) {
206-
// Validation: both side and slant height must be positive
207-
if (side <= 0) {
208-
throw new IllegalArgumentException("Side length must be greater than zero");
203+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
204+
if (sideLength <= 0) {
205+
throw new IllegalArgumentException("Must be a positive sideLength");
209206
}
210-
if (slant <= 0) {
211-
throw new IllegalArgumentException("Slant height must be greater than zero");
207+
if (slantHeight <= 0) {
208+
throw new IllegalArgumentException("Must be a positive slantHeight");
212209
}
213210

214-
// Base area (square) = side^2
215-
double base = side * side;
216-
217-
// Each triangular face has area = (side * slant) / 2
218-
// A square pyramid has 4 faces → multiply by 4 / 2 = 2
219-
double lateral = 2 * side * slant;
220-
221-
// Total surface area = base + lateral area
222-
double totalArea = base + lateral;
223-
224-
// Return the final computed surface area
225-
return totalArea;
211+
double baseArea = sideLength * sideLength;
212+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
213+
return baseArea + lateralSurfaceArea;
226214
}
227215
}

0 commit comments

Comments
 (0)