2

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
MOTIVECODEX
  • 2,624
  • 14
  • 43
  • 78

7 Answers7

6

Use this .. Heron's formual

enter image description here

enter image description here

double s = (a + b + c)/2.0d;
double x = (s * (s-a) * (s-b) * (s-c));
double Area= Math.sqrt(x);
return Area;
MOTIVECODEX
  • 2,624
  • 14
  • 43
  • 78
Bharath R
  • 1,491
  • 1
  • 11
  • 23
  • Tried this, also not working.. Expected Output `3.897114317029974`- `6.0`- `35.074028853269766` Your code's output `2.0` - `6.0` - `28.844410203711913` – MOTIVECODEX Nov 01 '13 at 08:58
  • what are the sides of triangle you are trying – Bharath R Nov 01 '13 at 09:04
  • @F4LLCON This does work. You seem to be checking the old output. I tried the above code and it gives the correct outpu – Pratik Shelar Nov 01 '13 at 09:11
  • Awesome! It works, the 2.0d and math.sqrt were the solution. Thanks – MOTIVECODEX Nov 01 '13 at 09:13
  • When using floating-point values, writing the formula as `Math.sqrt((a+b+c)*(a-b+c)*(b-a+c)*(a-c+b)) * 0.5` will yield more accurate results than precomputing the semiperimeter. Not apt to be a huge difference with `double` in realistic scenarios, but when using `float`, a triangle with sides (16777215.0f, 16777215f, 4.0f) will often yield an answer which too big or too small by about 50%. – supercat Feb 10 '15 at 23:46
0
double s = (a + b + c)/2;

You are getting loss of precision. Read this thread for details.

For your formula, it should be:

double Area = Math.sqrt(s * (s - a) * (s - b) * (s - c));

Since you didn't understand when I said about precision loss, here how your method should look like-

public static double triangleArea( double a, double b, double c ) {
    double s = (a + b + c)/2;
    double Area = Math.sqrt(s * (s - a) * (s - b) * (s - c));

    return Area;
}
Community
  • 1
  • 1
Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
0

Herons formula used is incorrect.You do not have to multiply with 0.5. You can find correct one here : http://en.wikipedia.org/wiki/Heron%27s_formula

double s = (a + b + c)/2.0d;
double x = ((s) * (s-a) * (s-B)* (s-c));
return Math.sqrt(x);
Pratik Shelar
  • 3,154
  • 7
  • 31
  • 51
  • Is it just me, or are you copying @Bharath Rallapalli? – MOTIVECODEX Nov 01 '13 at 09:15
  • Yes I updated my answer using his. But I had commented that his answer works cos I tested it and it does. I dont expect you to select my answer. I am just correcting my mistake, so that next time I have a look I will know. – Pratik Shelar Nov 01 '13 at 09:18
0

From the Wikipedia article, you are missing a squared root in your formula. Correct solution may be:

public static double triangleArea( int a, int b, int c )
{
    double s = (a + b + c)/2;
    double Area = Math.sqrt((s* (s-a) *(s-b) * (s-c)) * 0.5);
    return Area;
}

EDIT: I forgot to remove the *0.5 in the second line. It is wrong.

LeeNeverGup
  • 1,096
  • 8
  • 23
0
double s = (a+b+c)/2.0d;
return Math.pow((s*(s-a)*(s-b)*(s-c)),0.5);
Chaulagai
  • 752
  • 8
  • 12
0

I had the same problem and searched Google for the same. I ran into your question and I am using the same site FYI. The answer is pretty simple. Instead of double s = (a+b+c)/2;

You use : double s = (a+b+c)/2.0;

This solved the problem.

0

Heron's formula Area = (s*(s-a)(s-b)(s-c))0.5 where s = (a+b+c)/2

double s = (a+b+c)/2.0;

double area =  (s*(s-a)*(s-b)*(s-c));

area = Math.pow(area, 0.5);

return area;
vishwaraj
  • 487
  • 5
  • 5