Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/main/java/com/thealgorithms/maths/Area.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,36 @@ public static double surfaceAreaCone(final double radius, final double height) {
}
return Math.PI * radius * (radius + Math.pow(height * height + radius * radius, 0.5));
}

/**
* Calculates the total surface area of a pyramid with a square base.
* Includes both the base area and the slanted triangular sides.
*
* @param side the length of one side of the square base
* @param slant the slant height of the pyramid
* @return the total surface area of the pyramid in square units
* @throws IllegalArgumentException if any dimension is zero or negative
*/
public static double surfaceAreaPyramid(final double side, final double slant) {
// Validation: both side and slant height must be positive
if (side <= 0) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good validation step. It’s always smart to prevent invalid geometric dimensions early.

throw new IllegalArgumentException("Side length must be greater than zero");
}
if (slant <= 0) {
throw new IllegalArgumentException("Slant height must be greater than zero");
}

// Base area (square) = side^2
double base = side * side;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Base area calculation is correct and easy to follow. Consider adding a quick comment that this represents the square base.


// Each triangular face has area = (side * slant) / 2
// A square pyramid has 4 faces → multiply by 4 / 2 = 2
double lateral = 2 * side * slant;

// Total surface area = base + lateral area
double totalArea = base + lateral;

// Return the final computed surface area
return totalArea;
}
}