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 <vil02@o2.pl>
This commit is contained in:
Appari Satya Barghav 2023-10-24 02:39:42 +05:30 committed by GitHub
parent e87036d886
commit 9dae389faa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 13 deletions

View File

@ -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));
}
}

View File

@ -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); });
}
}