Skip to content

Commit 9f35bb7

Browse files
committed
Added surface area calculation for pyramid
1 parent 410f087 commit 9f35bb7

1 file changed

Lines changed: 21 additions & 20 deletions

File tree

  • src/main/java/com/thealgorithms/maths

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
package com.thealgorithms.maths;
23

34
/**
@@ -69,30 +70,11 @@ public static double surfaceAreaSphere(final double radius) {
6970
return 4 * Math.PI * radius * radius;
7071
}
7172

72-
/**
73-
* Calculate the surface area of a pyramid with a square base.
74-
*
75-
* @param sideLength side length of the square base
76-
* @param slantHeight slant height of the pyramid
77-
* @return surface area of the given pyramid
78-
*/
79-
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
80-
if (sideLength <= 0) {
81-
throw new IllegalArgumentException("");
82-
}
83-
if (slantHeight <= 0) {
84-
throw new IllegalArgumentException("slant height must be greater than 0");
85-
}
86-
double baseArea = sideLength * sideLength;
87-
double lateralSurfaceArea = 2 * sideLength * slantHeight;
88-
return baseArea + lateralSurfaceArea;
89-
}
90-
9173
/**
9274
* Calculate the area of a rectangle.
9375
*
9476
* @param length length of a rectangle
95-
* @param width width of a rectangle
77+
* @param width width of a rectangle
9678
* @return area of given rectangle
9779
*/
9880
public static double surfaceAreaRectangle(final double length, final double width) {
@@ -232,4 +214,23 @@ public static double surfaceAreaCone(final double radius, final double height) {
232214
}
233215
return Math.PI * radius * (radius + Math.pow(height * height + radius * radius, 0.5));
234216
}
217+
218+
/**
219+
* Calculate the surface area of a pyramid with a square base.
220+
*
221+
* @param sideLength side length of the square base
222+
* @param slantHeight slant height of the pyramid
223+
* @return surface area of the given pyramid
224+
*/
225+
public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) {
226+
if (sideLength <= 0) {
227+
throw new IllegalArgumentException("Must be a positive sideLength");
228+
}
229+
if (slantHeight <= 0) {
230+
throw new IllegalArgumentException("Must be a positive slantHeight");
231+
}
232+
double baseArea = sideLength * sideLength;
233+
double lateralSurfaceArea = 2 * sideLength * slantHeight;
234+
return baseArea + lateralSurfaceArea;
235+
}
235236
}

0 commit comments

Comments
 (0)