Skip to content
Open
Changes from 1 commit
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
27 changes: 27 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,31 @@ 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 surface area of a pyramid with a square base.
* This includes both the base and the four triangular faces.
*
* @param sideLength the length of one side of the square base
* @param slantHeight the slant height of the pyramid
* @return the total surface area of the pyramid
* @throws IllegalArgumentException if any parameter is non-positive
*/
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
// Validation checks to ensure inputs are physically valid
if (sideLength <= 0) {
throw new IllegalArgumentException("Side length must be a positive number");
}
if (slantHeight <= 0) {
throw new IllegalArgumentException("Slant height must be a positive number");
}

// Base area (square) = side²
double baseArea = sideLength * sideLength;
Copy link
Author

Choose a reason for hiding this comment

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

Simple and efficient calculation of the square base area.
Variable naming (baseArea) is clear and self-explanatory.


// Lateral area = 2 × side × slant height
double lateralSurfaceArea = 2 * sideLength * slantHeight;
Copy link
Author

Choose a reason for hiding this comment

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

Accurate computation for the combined area of four triangular sides.
The inline comment clarifies why the factor of 2 is used instead of 4 × ½ × base × height.


// Combine base and lateral areas for total surface area
return baseArea + lateralSurfaceArea;
}
}