diff --git a/src/main/java/com/thealgorithms/maths/HeronsFormula.java b/src/main/java/com/thealgorithms/maths/HeronsFormula.java index 72052a1b..5baee715 100644 --- a/src/main/java/com/thealgorithms/maths/HeronsFormula.java +++ b/src/main/java/com/thealgorithms/maths/HeronsFormula.java @@ -1,18 +1,35 @@ package com.thealgorithms.maths; /** + * Wikipedia for HeronsFormula => https://en.wikipedia.org/wiki/Heron%27s_formula * Find the area of a triangle using only side lengths */ -public class HeronsFormula { +public final class HeronsFormula { - public static double Herons(int s1, int s2, int s3) { - double a = s1; - double b = s2; - double c = s3; - double s = (a + b + c) / 2.0; - double area = 0; - area = Math.sqrt((s) * (s - a) * (s - b) * (s - c)); - return area; + /* + * A function to get the Area of a Triangle using Heron's Formula + * @param s1,s2,s3 => the three sides of the Triangle + * @return area using the formula (√(s(s – s1)(s – s2)(s – s3))) + * here s is called semi-perimeter and it is the half of the perimeter (i.e; s = (s1+s2+s3)/2) + * @author satyabarghav + */ + private HeronsFormula() { + } + + private static boolean areAllSidesPositive(final double a, final double b, final double c) { + return a > 0 && b > 0 && c > 0; + } + + private static boolean canFormTriangle(final double a, final double b, final double c) { + return a + b > c && b + c > a && c + a > b; + } + + public static double herons(final double a, final double b, final double c) { + if (!areAllSidesPositive(a, b, c) || !canFormTriangle(a, b, c)) { + throw new IllegalArgumentException("Triangle can't be formed with the given side lengths"); + } + final double s = (a + b + c) / 2.0; + return Math.sqrt((s) * (s - a) * (s - b) * (s - c)); } } diff --git a/src/test/java/com/thealgorithms/maths/HeronsFormulaTest.java b/src/test/java/com/thealgorithms/maths/HeronsFormulaTest.java index 32feeacd..22cecf4d 100644 --- a/src/test/java/com/thealgorithms/maths/HeronsFormulaTest.java +++ b/src/test/java/com/thealgorithms/maths/HeronsFormulaTest.java @@ -7,21 +7,33 @@ public class HeronsFormulaTest { @Test void test1() { - Assertions.assertEquals(HeronsFormula.Herons(3, 4, 5), 6.0); + Assertions.assertEquals(HeronsFormula.herons(3, 4, 5), 6.0); } @Test void test2() { - Assertions.assertEquals(HeronsFormula.Herons(24, 30, 18), 216.0); + Assertions.assertEquals(HeronsFormula.herons(24, 30, 18), 216.0); } @Test void test3() { - Assertions.assertEquals(HeronsFormula.Herons(1, 1, 1), 0.4330127018922193); + Assertions.assertEquals(HeronsFormula.herons(1, 1, 1), 0.4330127018922193); } @Test void test4() { - Assertions.assertEquals(HeronsFormula.Herons(4, 5, 8), 8.181534085976786); + Assertions.assertEquals(HeronsFormula.herons(4, 5, 8), 8.181534085976786); + } + + @Test + public void testCalculateAreaWithInvalidInput() { + Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(1, 2, 3); }); + Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(2, 1, 3); }); + Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(3, 2, 1); }); + Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(1, 3, 2); }); + + Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(1, 1, 0); }); + Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(1, 0, 1); }); + Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(0, 1, 1); }); } }