Repository: TheAlgorithms/Java
Pull request #3358 (2022-10-02 11:03:53)

refactor: Added test for Area.java and refactor
### **Describe your change:**
- [x] Test + Refactoring

### **Checklist:**
- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] All new Java files are placed inside an existing directory.
- [x] All filenames are in all uppercase characters with no spaces or dashes.
- [x] All functions and variable names follow Java naming conventions.

amarildoaliaj: Done

Patch:
diff --git a/src/main/java/com/thealgorithms/maths/Area.java b/src/main/java/com/thealgorithms/maths/Area.java
index c87bd634..262669fe 100644
--- a/src/main/java/com/thealgorithms/maths/Area.java
+++ b/src/main/java/com/thealgorithms/maths/Area.java
@@ -5,45 +5,20 @@ package com.thealgorithms.maths;
  */
 public class Area {
 
-    public static void main(String[] args) {
-        /* test cube */
-        assert Double.compare(surfaceAreaCube(1), 6.0) == 0;
-
-        /* test sphere */
-        assert Double.compare(surfaceAreaSphere(5), 314.1592653589793) == 0;
-        assert Double.compare(surfaceAreaSphere(1), 12.566370614359172) == 0;
-
-        /* test rectangle */
-        assert Double.compare(surfaceAreaRectangle(10, 20), 200.0) == 0;
-
-        /* test square */
-        assert Double.compare(surfaceAreaSquare(10), 100.0) == 0;
-
-        /* test triangle */
-        assert Double.compare(surfaceAreaTriangle(10, 10), 50.0) == 0;
-
-        /* test parallelogram */
-        assert Double.compare(surfaceAreaParallelogram(10, 20), 200.0) == 0;
-
-        /* test trapezium */
-        assert Double.compare(surfaceAreaTrapezium(10, 20, 30), 450.0) == 0;
-
-        /* test circle */
-        assert Double.compare(surfaceAreaCircle(20), 1256.6370614359173) == 0;
-
-        /* test cylinder */
-        assert Double.compare(surfaceAreaCylinder(1, 2), 18.84955592153876) ==
-        0;
+    /**
+     * String of IllegalArgumentException for radius
+     */
+    private static final String POSITIVE_RADIUS = "Must be a positive radius";
 
-        /* test hemisphere */
-        assert Double.compare(surfaceAreaHemisphere(5), 235.61944901923448) ==
-        0;
-        assert Double.compare(surfaceAreaHemisphere(1), 9.42477796076938) == 0;
+    /**
+     * String of IllegalArgumentException for height
+     */
+    private static final String POSITIVE_HEIGHT = "Must be a positive height";
 
-        /* test cone */
-        assert Double.compare(surfaceAreaCone(6, 8), 301.59289474462014) == 0;
-        assert Double.compare(surfaceAreaCone(10, 24), 1130.9733552923256) == 0;
-    }
+    /**
+     * String of IllegalArgumentException for base
+     */
+    private static final String POSITIVE_BASE = "Must be a positive base";
 
     /**
      * Calculate the surface area of a cube.
@@ -51,7 +26,10 @@ public class Area {
      * @param sideLength side length of cube
      * @return surface area of given cube
      */
-    private static double surfaceAreaCube(double sideLength) {
+    public static double surfaceAreaCube(final double sideLength) {
+        if (sideLength <= 0) {
+            throw new IllegalArgumentException("Must be a positive sideLength");
+        }
         return 6 * sideLength * sideLength;
     }
 
@@ -61,87 +39,125 @@ public class Area {
      * @param radius radius of sphere
      * @return surface area of given sphere
      */
-    private static double surfaceAreaSphere(double radius) {
+    public static double surfaceAreaSphere(final double radius) {
+        if (radius <= 0) {
+            throw new IllegalArgumentException(POSITIVE_RADIUS);
+        }
         return 4 * Math.PI * radius * radius;
     }
 
     /**
-     * Calculate the area of a rectangle
+     * Calculate the area of a rectangle.
      *
-     * @param length length of rectangle
-     * @param width width of rectangle
+     * @param length length of a rectangle
+     * @param width  width of a rectangle
      * @return area of given rectangle
      */
-    private static double surfaceAreaRectangle(double length, double width) {
+    public static double surfaceAreaRectangle(final double length, final double width) {
+        if (length <= 0) {
+            throw new IllegalArgumentException("Must be a positive length");
+        }
+        if (width <= 0) {
+            throw new IllegalArgumentException("Must be a positive width");
+        }
         return length * width;
     }
 
     /**
-     * Calculate surface area of a cylinder
+     * Calculate surface area of a cylinder.
      *
      * @param radius radius of the floor
      * @param height height of the cylinder.
      * @return volume of given cylinder
      */
-    private static double surfaceAreaCylinder(double radius, double height) {
+    public static double surfaceAreaCylinder(final double radius, final double height) {
+        if (radius <= 0) {
+            throw new IllegalArgumentException(POSITIVE_RADIUS);
+        }
+        if (height <= 0) {
+            throw new IllegalArgumentException(POSITIVE_RADIUS);
+        }
         return 2 * (Math.PI * radius * radius + Math.PI * radius * height);
     }
 
     /**
-     * Calculate the area of a square
+     * Calculate the area of a square.
      *
      * @param sideLength side length of square
      * @return area of given square
      */
-    private static double surfaceAreaSquare(double sideLength) {
+    public static double surfaceAreaSquare(final double sideLength) {
+        if (sideLength <= 0) {
+            throw new IllegalArgumentException("Must be a positive sideLength");
+        }
         return sideLength * sideLength;
     }
 
     /**
-     * Calculate the area of a triangle
+     * Calculate the area of a triangle.
      *
-     * @param base base of triangle
+     * @param base   base of triangle
      * @param height height of triangle
      * @return area of given triangle
      */
-    private static double surfaceAreaTriangle(double base, double height) {
+    public static double surfaceAreaTriangleRectangle(final double base, final double height) {
+        if (base <= 0) {
+            throw new IllegalArgumentException(POSITIVE_BASE);
+        }
+        if (height <= 0) {
+            throw new IllegalArgumentException(POSITIVE_HEIGHT);
+        }
         return base * height / 2;
     }
 
     /**
-     * Calculate the area of a parallelogram
+     * Calculate the area of a parallelogram.
      *
-     * @param base base of parallelogram
-     * @param height height of parallelogram
+     * @param base   base of a parallelogram
+     * @param height height of a parallelogram
      * @return area of given parallelogram
      */
-    private static double surfaceAreaParallelogram(double base, double height) {
+    public static double surfaceAreaParallelogram(final double base, final double height) {
+        if (base <= 0) {
+            throw new IllegalArgumentException(POSITIVE_BASE);
+        }
+        if (height <= 0) {
+            throw new IllegalArgumentException(POSITIVE_HEIGHT);
+        }
         return base * height;
     }
 
     /**
-     * Calculate the area of a trapezium
+     * Calculate the area of a trapezium.
      *
-     * @param base1 upper base of trapezium
-     * @param base2 bottom base of trapezium
+     * @param base1  upper base of trapezium
+     * @param base2  bottom base of trapezium
      * @param height height of trapezium
      * @return area of given trapezium
      */
-    private static double surfaceAreaTrapezium(
-        double base1,
-        double base2,
-        double height
-    ) {
+    public static double surfaceAreaTrapezium(final double base1, final double base2, final double height) {
+        if (base1 <= 0) {
+            throw new IllegalArgumentException(POSITIVE_BASE + 1);
+        }
+        if (base2 <= 0) {
+            throw new IllegalArgumentException(POSITIVE_BASE + 2);
+        }
+        if (height <= 0) {
+            throw new IllegalArgumentException(POSITIVE_HEIGHT);
+        }
         return (base1 + base2) * height / 2;
     }
 
     /**
-     * Calculate the area of a circle
+     * Calculate the area of a circle.
      *
      * @param radius radius of circle
      * @return area of given circle
      */
-    private static double surfaceAreaCircle(double radius) {
+    public static double surfaceAreaCircle(final double radius) {
+        if (radius <= 0) {
+            throw new IllegalArgumentException(POSITIVE_RADIUS);
+        }
         return Math.PI * radius * radius;
     }
 
@@ -151,7 +167,10 @@ public class Area {
      * @param radius radius of hemisphere
      * @return surface area of given hemisphere
      */
-    private static double surfaceAreaHemisphere(double radius) {
+    public static double surfaceAreaHemisphere(final double radius) {
+        if (radius <= 0) {
+            throw new IllegalArgumentException(POSITIVE_RADIUS);
+        }
         return 3 * Math.PI * radius * radius;
     }
 
@@ -162,11 +181,13 @@ public class Area {
      * @param height of cone.
      * @return surface area of given cone.
      */
-    private static double surfaceAreaCone(double radius, double height) {
-        return (
-            Math.PI *
-            radius *
-            (radius + Math.pow((height * height + radius * radius), 0.5))
-        );
+    public static double surfaceAreaCone(final double radius, final double height) {
+        if (radius <= 0) {
+            throw new IllegalArgumentException(POSITIVE_RADIUS);
+        }
+        if (height <= 0) {
+            throw new IllegalArgumentException(POSITIVE_HEIGHT);
+        }
+        return Math.PI * radius * (radius + Math.pow(height * height + radius * radius, 0.5));
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/AreaTest.java b/src/test/java/com/thealgorithms/maths/AreaTest.java
new file mode 100644
index 00000000..6020054c
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/AreaTest.java
@@ -0,0 +1,90 @@
+package com.thealgorithms.maths;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+/**
+ * @author Amarildo Aliaj
+ */
+class AreaTest {
+
+    @Test
+    void testSurfaceAreaCube() {
+        assertEquals(6.0, Area.surfaceAreaCube(1));
+    }
+
+    @Test
+    void testSurfaceAreaSphere() {
+        assertEquals(12.566370614359172, Area.surfaceAreaSphere(1));
+    }
+
+    @Test
+    void testSurfaceAreaRectangle() {
+        assertEquals(200.0, Area.surfaceAreaRectangle(10, 20));
+    }
+
+    @Test
+    void testSurfaceAreaCylinder() {
+        assertEquals(18.84955592153876, Area.surfaceAreaCylinder(1, 2));
+    }
+
+    @Test
+    void testSurfaceAreaSquare() {
+        assertEquals(100.0, Area.surfaceAreaSquare(10));
+    }
+
+    @Test
+    void testSurfaceAreaTriangleRectangle() {
+        assertEquals(50.0, Area.surfaceAreaTriangleRectangle(10, 10));
+    }
+
+    @Test
+    void testSurfaceAreaParallelogram() {
+        assertEquals(200.0, Area.surfaceAreaParallelogram(10, 20));
+    }
+
+    @Test
+    void testSurfaceAreaTrapezium() {
+        assertEquals(450.0, Area.surfaceAreaTrapezium(10, 20, 30));
+    }
+
+    @Test
+    void testSurfaceAreaCircle() {
+        assertEquals(1256.6370614359173, Area.surfaceAreaCircle(20));
+    }
+
+    @Test
+    void surfaceAreaHemisphere() {
+        assertEquals(235.61944901923448, Area.surfaceAreaHemisphere(5));
+    }
+
+    @Test
+    void surfaceAreaCone() {
+        assertEquals(301.59289474462014, Area.surfaceAreaCone(6, 8));
+    }
+
+    @Test
+    void testAllIllegalInput() {
+        assertAll(
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCube(0)),
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaSphere(0)),
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaRectangle(0, 10)),
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaRectangle(10, 0)),
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCylinder(0, 1)),
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCylinder(1, 0)),
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaSquare(0)),
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaTriangleRectangle(0, 1)),
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaTriangleRectangle(1, 0)),
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaParallelogram(0, 1)),
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaParallelogram(1, 0)),
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaTrapezium(0, 1, 1)),
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaTrapezium(1, 0, 1)),
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaTrapezium(1, 1, 0)),
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCircle(0)),
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaHemisphere(0)),
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCone(1, 0)),
+                () -> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCone(0, 1))
+        );
+    }
+}
\ No newline at end of file