diff --git a/src/main/java/com/thealgorithms/maths/HeronsFormula.java b/src/main/java/com/thealgorithms/maths/HeronsFormula.java new file mode 100644 index 00000000..b3da8701 --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/HeronsFormula.java @@ -0,0 +1,19 @@ +package com.thealgorithms.maths; + +/** + * Find the area of a triangle using only side lengths + */ + +public 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; + } +} diff --git a/src/test/java/com/thealgorithms/maths/HeronsFormulaTest.java b/src/test/java/com/thealgorithms/maths/HeronsFormulaTest.java new file mode 100644 index 00000000..44328399 --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/HeronsFormulaTest.java @@ -0,0 +1,27 @@ +package com.thealgorithms.maths; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class HeronsFormulaTest { + @Test + void test1() + { + Assertions.assertEquals(HeronsFormula.Herons(3,4,5), 6.0); + } + @Test + void test2() + { + Assertions.assertEquals(HeronsFormula.Herons(24,30,18), 216.0); + } + @Test + void test3() + { + Assertions.assertEquals(HeronsFormula.Herons(1,1,1), 0.4330127018922193); + } + @Test + void test4() + { + Assertions.assertEquals(HeronsFormula.Herons(4,5,8), 8.181534085976786); + } +}