From 8afead813675b0da2720256ba67daa75ff96a9a7 Mon Sep 17 00:00:00 2001 From: Makrus12345 Date: Tue, 21 Oct 2025 21:51:18 -0400 Subject: [PATCH 1/4] Added surface area calculation for pyramid --- Java | 1 + .../java/com/thealgorithms/maths/Area.java | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 160000 Java diff --git a/Java b/Java new file mode 160000 index 000000000000..6866c58dc8b0 --- /dev/null +++ b/Java @@ -0,0 +1 @@ +Subproject commit 6866c58dc8b03b84e5dda7848184082aefe525fa diff --git a/src/main/java/com/thealgorithms/maths/Area.java b/src/main/java/com/thealgorithms/maths/Area.java index 7a06fd5e5fa0..613fb6cdfe03 100644 --- a/src/main/java/com/thealgorithms/maths/Area.java +++ b/src/main/java/com/thealgorithms/maths/Area.java @@ -192,4 +192,24 @@ 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; + } } From 501dac28d8c9873123ddc43bf8bbca4a804c18f7 Mon Sep 17 00:00:00 2001 From: Makrus12345 Date: Tue, 21 Oct 2025 21:52:34 -0400 Subject: [PATCH 2/4] Cleaned nested repo and added surface area calculation for pyramid --- Java | 1 - 1 file changed, 1 deletion(-) delete mode 160000 Java diff --git a/Java b/Java deleted file mode 160000 index 6866c58dc8b0..000000000000 --- a/Java +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6866c58dc8b03b84e5dda7848184082aefe525fa From 1874b098fa11926e5204caf8a61e2d8b4798844b Mon Sep 17 00:00:00 2001 From: Makrus12345 Date: Tue, 21 Oct 2025 22:00:14 -0400 Subject: [PATCH 3/4] Revised pyramid surface area method with comments and formatting updates --- .../java/com/thealgorithms/maths/Area.java | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Area.java b/src/main/java/com/thealgorithms/maths/Area.java index 613fb6cdfe03..2adde7f18db6 100644 --- a/src/main/java/com/thealgorithms/maths/Area.java +++ b/src/main/java/com/thealgorithms/maths/Area.java @@ -193,23 +193,35 @@ 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. + /** + * Calculates the total surface area of a pyramid with a square base. + * Includes both the base area and the slanted triangular sides. * - * @param sideLength side length of the square base - * @param slantHeight slant height of the pyramid - * @return surface area of the given pyramid + * @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 sideLength, final double slantHeight) { - if (sideLength <= 0) { - throw new IllegalArgumentException("Must be a positive sideLength"); + public static double surfaceAreaPyramid(final double side, final double slant) { + // Validation: both side and slant height must be positive + if (side <= 0) { + throw new IllegalArgumentException("Side length must be greater than zero"); } - if (slantHeight <= 0) { - throw new IllegalArgumentException("Must be a positive slantHeight"); + if (slant <= 0) { + throw new IllegalArgumentException("Slant height must be greater than zero"); } - double baseArea = sideLength * sideLength; - double lateralSurfaceArea = 2 * sideLength * slantHeight; - return baseArea + lateralSurfaceArea; + // Base area (square) = side^2 + double base = side * side; + + // 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; } } From 4d3487621dcc8bd012e738ac9dbca221c81b90ae Mon Sep 17 00:00:00 2001 From: Makrus12345 Date: Tue, 21 Oct 2025 22:10:48 -0400 Subject: [PATCH 4/4] Added surfaceAreaPyramid method with validation and comments --- .../java/com/thealgorithms/maths/Area.java | 38 +++++++------------ 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Area.java b/src/main/java/com/thealgorithms/maths/Area.java index 2adde7f18db6..ca8406ee131c 100644 --- a/src/main/java/com/thealgorithms/maths/Area.java +++ b/src/main/java/com/thealgorithms/maths/Area.java @@ -193,35 +193,23 @@ 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. + /** + * Calculate the surface area of a pyramid with a square base. * - * @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 + * @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 side, final double slant) { - // Validation: both side and slant height must be positive - if (side <= 0) { - throw new IllegalArgumentException("Side length must be greater than zero"); + public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) { + if (sideLength <= 0) { + throw new IllegalArgumentException("Must be a positive sideLength"); } - if (slant <= 0) { - throw new IllegalArgumentException("Slant height must be greater than zero"); + if (slantHeight <= 0) { + throw new IllegalArgumentException("Must be a positive slantHeight"); } - // Base area (square) = side^2 - double base = side * side; - - // 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; + double baseArea = sideLength * sideLength; + double lateralSurfaceArea = 2 * sideLength * slantHeight; + return baseArea + lateralSurfaceArea; } }