Repository: TheAlgorithms/Java
Pull request #4686 (2023-10-06 10:22:18)

Herons : Changed the signature of the function
- [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 it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.

I have already raised this issue [here](https://github.com/TheAlgorithms/Java/issues/4685#issue-1929808965) 
The required changes were made to make it work with even larger set of data types like double and float and further documentation was also added ro ease the readability and understandability of the code.


satyabarghav: fix #4685 
satyabarghav: #4685 
vil02: Fixes #4685.
satyabarghav: Made the requested changes, I request the Reviewer(s) to please tag this as hacktoberfest-accepted.

vil02: > Made the requested changes, I request the Reviewer(s) to please tag this as hacktoberfest-accepted.

This is not needed, since this repository has `hacktoberfest` _tag_. The [website](https://hacktoberfest.com) will recognize your contribution.
vil02: @satyabarghav there was a subtle error in the check if all of the provided sidelights are positive, namely, the tests:
```java
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); });
```
did not pass.

Always add tests.

To speed up the whole process I allowed myself to push a commit into your branch - I hope you do not mind.

Patch:
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));
     }
 }


Test patch:
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); });
     }
 }