From 5f1073da2b4f5c3e6eba5e1395c6f9a882547251 Mon Sep 17 00:00:00 2001 From: Farhan Qadri Date: Wed, 28 Oct 2026 14:03:10 -0400 Subject: [PATCH] Improve pyramid area validation message and variable declarations --- src/main/java/com/thealgorithms/maths/Area.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Area.java b/src/main/java/com/thealgorithms/maths/Area.java index 84fc67159379..8d7871e1d57f 100644 --- a/src/main/java/com/thealgorithms/maths/Area.java +++ b/src/main/java/com/thealgorithms/maths/Area.java @@ -76,15 +76,22 @@ public static double surfaceAreaSphere(final double radius) { * @param slantHeight slant height of the pyramid * @return surface area of the given pyramid */ - public static double surfaceAreaPyramid(final double sideLength, final double slantHeight) { + public static double surfaceAreaPyramid( + final double sideLength, final double slantHeight) { + if (sideLength <= 0) { - throw new IllegalArgumentException(""); + throw new IllegalArgumentException( + "side length must be greater than 0"); } + if (slantHeight <= 0) { - throw new IllegalArgumentException("slant height must be greater than 0"); + throw new IllegalArgumentException( + "slant height must be greater than 0"); } - double baseArea = sideLength * sideLength; - double lateralSurfaceArea = 2 * sideLength * slantHeight; + + final double baseArea = sideLength * sideLength; + final double lateralSurfaceArea = 2 * sideLength * slantHeight; + return baseArea + lateralSurfaceArea; }