mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Added surface area calculation for pyramid (#6853)
Co-authored-by: JonathanButterworth <JonathanButterworth>
This commit is contained in:
committed by
GitHub
parent
bb6385e756
commit
dfd8d6993f
@@ -48,6 +48,25 @@ public final class Area {
|
||||
return 4 * Math.PI * radius * radius;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the surface area of a pyramid with a square base.
|
||||
*
|
||||
* @param sideLength side length of the square base
|
||||
* @param slantHeight slant height of the pyramid
|
||||
* @return surface area of the given pyramid
|
||||
*/
|
||||
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
|
||||
if (sideLength <= 0) {
|
||||
throw new IllegalArgumentException("Must be a positive sideLength");
|
||||
}
|
||||
if (slantHeight <= 0) {
|
||||
throw new IllegalArgumentException("Must be a positive slantHeight");
|
||||
}
|
||||
double baseArea = sideLength * sideLength;
|
||||
double lateralSurfaceArea = 2 * sideLength * slantHeight;
|
||||
return baseArea + lateralSurfaceArea;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the area of a rectangle.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user