Skip to content

Herons : Changed the signature of the function #4686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions src/main/java/com/thealgorithms/maths/HeronsFormula.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
20 changes: 16 additions & 4 deletions src/test/java/com/thealgorithms/maths/HeronsFormulaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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); });
}
}