From 0a395f236d76b353303d00a2ad5fd009cc7c97f4 Mon Sep 17 00:00:00 2001 From: Aman Mohamed Ali <69119173+spidervirus@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:48:58 +0400 Subject: [PATCH] fix(maths): validate non-positive dimensions and torus radius in Volume --- .../java/com/thealgorithms/maths/Volume.java | 108 ++++++++++++++++-- .../com/thealgorithms/maths/VolumeTest.java | 58 +++++++++- 2 files changed, 155 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/Volume.java b/src/main/java/com/thealgorithms/maths/Volume.java index 488b921cae83..85c0151f51c2 100644 --- a/src/main/java/com/thealgorithms/maths/Volume.java +++ b/src/main/java/com/thealgorithms/maths/Volume.java @@ -1,17 +1,31 @@ package com.thealgorithms.maths; -/* Calculate the volume of various shapes.*/ +/** + * Calculate the volume of various shapes. + */ public final class Volume { private Volume() { } + private static final String POSITIVE_RADIUS = "Radius must be greater than 0"; + private static final String POSITIVE_HEIGHT = "Height must be greater than 0"; + private static final String POSITIVE_SIDE = "Side length must be greater than 0"; + private static final String POSITIVE_BASE_AREA = "Base area must be greater than 0"; + private static final String POSITIVE_LENGTH = "Length must be greater than 0"; + private static final String POSITIVE_WIDTH = "Width must be greater than 0"; + private static final String POSITIVE_AXIS = "Semi-axis must be greater than 0"; + /** * Calculate the volume of a cube. * * @param sideLength length of the given cube's sides * @return volume of the given cube + * @throws IllegalArgumentException if sideLength is less than or equal to 0 */ public static double volumeCube(double sideLength) { + if (sideLength <= 0) { + throw new IllegalArgumentException(POSITIVE_SIDE); + } return sideLength * sideLength * sideLength; } @@ -22,8 +36,18 @@ public static double volumeCube(double sideLength) { * @param height height of given cuboid * @param length length of given cuboid * @return volume of given cuboid + * @throws IllegalArgumentException if any dimension is less than or equal to 0 */ public static double volumeCuboid(double width, double height, double length) { + if (width <= 0) { + throw new IllegalArgumentException(POSITIVE_WIDTH); + } + if (height <= 0) { + throw new IllegalArgumentException(POSITIVE_HEIGHT); + } + if (length <= 0) { + throw new IllegalArgumentException(POSITIVE_LENGTH); + } return width * height * length; } @@ -32,19 +56,30 @@ public static double volumeCuboid(double width, double height, double length) { * * @param radius radius of given sphere * @return volume of given sphere + * @throws IllegalArgumentException if radius is less than or equal to 0 */ public static double volumeSphere(double radius) { + if (radius <= 0) { + throw new IllegalArgumentException(POSITIVE_RADIUS); + } return (4 * Math.PI * radius * radius * radius) / 3; } /** - * Calculate volume of a cylinder + * Calculate volume of a cylinder. * * @param radius radius of the given cylinder's floor * @param height height of the given cylinder * @return volume of given cylinder + * @throws IllegalArgumentException if radius or height is less than or equal to 0 */ public static double volumeCylinder(double radius, double height) { + if (radius <= 0) { + throw new IllegalArgumentException(POSITIVE_RADIUS); + } + if (height <= 0) { + throw new IllegalArgumentException(POSITIVE_HEIGHT); + } return Math.PI * radius * radius * height; } @@ -53,8 +88,12 @@ public static double volumeCylinder(double radius, double height) { * * @param radius radius of given hemisphere * @return volume of given hemisphere + * @throws IllegalArgumentException if radius is less than or equal to 0 */ public static double volumeHemisphere(double radius) { + if (radius <= 0) { + throw new IllegalArgumentException(POSITIVE_RADIUS); + } return (2 * Math.PI * radius * radius * radius) / 3; } @@ -62,44 +101,72 @@ public static double volumeHemisphere(double radius) { * Calculate the volume of a cone. * * @param radius radius of given cone - * @param height of given cone + * @param height height of given cone * @return volume of given cone + * @throws IllegalArgumentException if radius or height is less than or equal to 0 */ public static double volumeCone(double radius, double height) { + if (radius <= 0) { + throw new IllegalArgumentException(POSITIVE_RADIUS); + } + if (height <= 0) { + throw new IllegalArgumentException(POSITIVE_HEIGHT); + } return (Math.PI * radius * radius * height) / 3; } /** * Calculate the volume of a prism. * - * @param baseArea area of the given prism's base - * @param height of given prism + * @param baseArea area of the given prism's base + * @param height height of given prism * @return volume of given prism + * @throws IllegalArgumentException if baseArea or height is less than or equal to 0 */ public static double volumePrism(double baseArea, double height) { + if (baseArea <= 0) { + throw new IllegalArgumentException(POSITIVE_BASE_AREA); + } + if (height <= 0) { + throw new IllegalArgumentException(POSITIVE_HEIGHT); + } return baseArea * height; } /** * Calculate the volume of a pyramid. * - * @param baseArea of the given pyramid's base - * @param height of given pyramid + * @param baseArea area of the given pyramid's base + * @param height height of given pyramid * @return volume of given pyramid + * @throws IllegalArgumentException if baseArea or height is less than or equal to 0 */ public static double volumePyramid(double baseArea, double height) { + if (baseArea <= 0) { + throw new IllegalArgumentException(POSITIVE_BASE_AREA); + } + if (height <= 0) { + throw new IllegalArgumentException(POSITIVE_HEIGHT); + } return (baseArea * height) / 3; } /** * Calculate the volume of a frustum of a cone. * - * @param r1 radius of the top of the frustum - * @param r2 radius of the bottom of the frustum + * @param r1 radius of the top of the frustum + * @param r2 radius of the bottom of the frustum * @param height height of the frustum * @return volume of the frustum + * @throws IllegalArgumentException if radii or height are less than or equal to 0 */ public static double volumeFrustumOfCone(double r1, double r2, double height) { + if (r1 <= 0 || r2 <= 0) { + throw new IllegalArgumentException(POSITIVE_RADIUS); + } + if (height <= 0) { + throw new IllegalArgumentException(POSITIVE_HEIGHT); + } return (Math.PI * height / 3) * (r1 * r1 + r2 * r2 + r1 * r2); } @@ -108,10 +175,17 @@ public static double volumeFrustumOfCone(double r1, double r2, double height) { * * @param upperBaseArea area of the upper base * @param lowerBaseArea area of the lower base - * @param height height of the frustum + * @param height height of the frustum * @return volume of the frustum + * @throws IllegalArgumentException if base areas or height are less than or equal to 0 */ public static double volumeFrustumOfPyramid(double upperBaseArea, double lowerBaseArea, double height) { + if (upperBaseArea <= 0 || lowerBaseArea <= 0) { + throw new IllegalArgumentException(POSITIVE_BASE_AREA); + } + if (height <= 0) { + throw new IllegalArgumentException(POSITIVE_HEIGHT); + } return (upperBaseArea + lowerBaseArea + Math.sqrt(upperBaseArea * lowerBaseArea)) * height / 3; } @@ -121,8 +195,18 @@ public static double volumeFrustumOfPyramid(double upperBaseArea, double lowerBa * @param majorRadius major radius of a torus * @param minorRadius minor radius of a torus * @return volume of the torus + * @throws IllegalArgumentException if radii are non-positive or minor radius exceeds major radius */ public static double volumeTorus(double majorRadius, double minorRadius) { + if (majorRadius <= 0) { + throw new IllegalArgumentException("Major radius must be greater than 0"); + } + if (minorRadius <= 0) { + throw new IllegalArgumentException("Minor radius must be greater than 0"); + } + if (minorRadius > majorRadius) { + throw new IllegalArgumentException("Major radius must be greater than or equal to minor radius"); + } return 2 * Math.PI * Math.PI * majorRadius * minorRadius * minorRadius; } @@ -133,8 +217,12 @@ public static double volumeTorus(double majorRadius, double minorRadius) { * @param b second semi-axis of an ellipsoid * @param c third semi-axis of an ellipsoid * @return volume of the ellipsoid + * @throws IllegalArgumentException if any semi-axis is less than or equal to 0 */ public static double volumeEllipsoid(double a, double b, double c) { + if (a <= 0 || b <= 0 || c <= 0) { + throw new IllegalArgumentException(POSITIVE_AXIS); + } return (4 * Math.PI * a * b * c) / 3; } } diff --git a/src/test/java/com/thealgorithms/maths/VolumeTest.java b/src/test/java/com/thealgorithms/maths/VolumeTest.java index c0b02d6ba28e..34441c3684b7 100644 --- a/src/test/java/com/thealgorithms/maths/VolumeTest.java +++ b/src/test/java/com/thealgorithms/maths/VolumeTest.java @@ -1,6 +1,8 @@ package com.thealgorithms.maths; +import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; @@ -8,7 +10,6 @@ public class VolumeTest { @Test public void volume() { - /* test cube */ assertEquals(343.0, Volume.volumeCube(7)); @@ -45,4 +46,59 @@ public void volume() { /* test ellipsoid */ assertEquals(25.1327412287183459, Volume.volumeEllipsoid(3, 2, 1)); } + + @Test + public void testIllegalInputs() { + assertAll(() + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCube(0)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCube(-1)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCuboid(0, 1, 1)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCuboid(1, 0, 1)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCuboid(1, 1, 0)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeSphere(0)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeSphere(-5)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCylinder(0, 5)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCylinder(5, 0)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeHemisphere(0)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCone(0, 5)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCone(5, 0)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumePrism(0, 5)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumePrism(5, 0)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumePyramid(0, 5)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumePyramid(5, 0)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeFrustumOfCone(0, 5, 5)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeFrustumOfCone(5, 0, 5)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeFrustumOfCone(5, 5, 0)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeFrustumOfPyramid(0, 5, 5)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeFrustumOfPyramid(5, 0, 5)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeFrustumOfPyramid(5, 5, 0)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeTorus(0, 5)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeTorus(5, 0)), + () + -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeTorus(2, 5)), + () -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeEllipsoid(0, 1, 2)), () -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeEllipsoid(1, 0, 2)), () -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeEllipsoid(1, 2, 0))); + } }