From 9dae389faa03b8400b6f0796383f47495887151a Mon Sep 17 00:00:00 2001 From: Appari Satya Barghav <36763910+satyabarghav@users.noreply.github.com> Date: Tue, 24 Oct 2023 02:39:42 +0530 Subject: [PATCH] Herons : Changed the signature of the function (#4686) * Made changes to the code to correct the Logic of Armstrong Number * Resolved the issues * Trying to resolve the Linter error by changing Variable name * Changed Variable Names : trying to resolve Clang error * Chnged the signature of the function * Added the Function documentation * Added exception for parameters * Resolved with suggested changes * Resolved with Suggested changes * fix: use proper logic --------- Co-authored-by: vil02 --- .../thealgorithms/maths/HeronsFormula.java | 35 ++++++++++++++----- .../maths/HeronsFormulaTest.java | 20 ++++++++--- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/HeronsFormula.java b/src/main/java/com/thealgorithms/maths/HeronsFormula.java index 72052a1b8..5baee715d 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 32feeacdb..22cecf4dc 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); }); } }