Add Tests for Heron's Formula (#3035)
This commit is contained in:
parent
dfdce96c6e
commit
41be089f6a
19
src/main/java/com/thealgorithms/maths/HeronsFormula.java
Normal file
19
src/main/java/com/thealgorithms/maths/HeronsFormula.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
27
src/test/java/com/thealgorithms/maths/HeronsFormulaTest.java
Normal file
27
src/test/java/com/thealgorithms/maths/HeronsFormulaTest.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user