Skip to content

Commit 1874b09

Browse files
Revised pyramid surface area method with comments and formatting updates
1 parent 501dac2 commit 1874b09

File tree

1 file changed

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

1 file changed

+25
-13
lines changed

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,23 +193,35 @@ 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-
* Calculate the surface area of a pyramid with a square base.
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.
198199
*
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
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
202204
*/
203-
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
204-
if (sideLength <= 0) {
205-
throw new IllegalArgumentException("Must be a positive sideLength");
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");
206209
}
207-
if (slantHeight <= 0) {
208-
throw new IllegalArgumentException("Must be a positive slantHeight");
210+
if (slant <= 0) {
211+
throw new IllegalArgumentException("Slant height must be greater than zero");
209212
}
210213

211-
double baseArea = sideLength * sideLength;
212-
double lateralSurfaceArea = 2 * sideLength * slantHeight;
213-
return baseArea + lateralSurfaceArea;
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;
214226
}
215227
}

0 commit comments

Comments
 (0)