Add Tests for Heron's Formula (#3035)

This commit is contained in:
RaghavTaneja 2022-04-28 02:40:13 -05:00 committed by GitHub
parent dfdce96c6e
commit 41be089f6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 0 deletions

View File

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

View File

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