From 9f35bb79185d24c01ba925eea50291c238a1be05 Mon Sep 17 00:00:00 2001 From: yrazvi32 Date: Thu, 29 Oct 2026 21:31:01 -0400 Subject: [PATCH] Added surface area calculation for pyramid --- .../java/com/thealgorithms/maths/Area.java | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Area.java b/src/main/java/com/thealgorithms/maths/Area.java index 84fc67159379..8d7ba33478be 100644 --- a/src/main/java/com/thealgorithms/maths/Area.java +++ b/src/main/java/com/thealgorithms/maths/Area.java @@ -1,3 +1,4 @@ + package com.thealgorithms.maths; /** @@ -69,30 +70,11 @@ public static double surfaceAreaSphere(final double radius) { 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(""); - } - if (slantHeight <= 0) { - throw new IllegalArgumentException("slant height must be greater than 0"); - } - double baseArea = sideLength * sideLength; - double lateralSurfaceArea = 2 * sideLength * slantHeight; - return baseArea + lateralSurfaceArea; - } - /** * Calculate the area of a rectangle. * * @param length length of a rectangle - * @param width width of a rectangle + * @param width width of a rectangle * @return area of given rectangle */ public static double surfaceAreaRectangle(final double length, final double width) { @@ -232,4 +214,23 @@ public static double surfaceAreaCone(final double radius, final double height) { } return Math.PI * radius * (radius + Math.pow(height * height + radius * radius, 0.5)); } + + /** + * 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; + } }