EDIT// I might think that the code Programmr.com uses to check the answer output vs expected output is wrong. Because all of the answers here have almost the same formula, and also the formula on the wiki page about hero's formula is the same as the answers here.
In this exercise, complete the function that "returns a value". When you call this function, it should calculate the area of the triangle using Heron's formula and return it.
Heron's formula: Area = (s*(s-a)(s-b)(s-c))0.5 where s = (a+b+c)/2
I wrote this, but it seems not correct and I can't figure out what's wrong. The output of this gives wrong values:
public class Challenge
{
public static void main( String[] args )
{
double a;
a = triangleArea(3, 3, 3);
System.out.println("A triangle with sides 3,3,3 has an area of:" + a);
a = triangleArea(3, 4, 5);
System.out.println("A triangle with sides 3,4,5 has an area of:" + a);
a = triangleArea(9, 9, 9); // ! also realize the 9,9,9 is not even the same as the comment bellow. This was generated by the Programmr.com exercise.
System.out.println("A triangle with sides 7,8,9 has an area of:" + a );
}
public static double triangleArea( int a, int b, int c )
{
double s = (a + b + c)/2;
double x = ((s) * (s-a) * (s-b) * (s-c));
double Area = Math.sqrt(x);
return Area;
}
}
Expected Output
3.897114317029974
6.0
35.074028853269766
Your code's output
2.0
6.0
28.844410203711913