From 41be089f6a918aece04e6c951359c699e755206e Mon Sep 17 00:00:00 2001 From: RaghavTaneja <97575679+RaghavTaneja@users.noreply.github.com> Date: Thu, 28 Apr 2022 02:40:13 -0500 Subject: [PATCH] Add Tests for Heron's Formula (#3035) --- .../thealgorithms/maths/HeronsFormula.java | 19 +++++++++++++ .../maths/HeronsFormulaTest.java | 27 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/main/java/com/thealgorithms/maths/HeronsFormula.java create mode 100644 src/test/java/com/thealgorithms/maths/HeronsFormulaTest.java 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); + } +}