aspose file tools*
The moose likes Beginning Java and the fly likes Using heron's formula to calculate the area of a triangle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using heron Watch "Using heron New topic
Author

Using heron's formula to calculate the area of a triangle

russell lloyd
Greenhorn

Joined: Feb 09, 2005
Posts: 18
For some reason which passes understanding my teacher has asked that we Create a program that will calculate the area of a formula using Heron's formula which is
area = squareroot(S * (S - A) * (S - B) * (S - C))
Where S = the perimeter of the triangle divided by 2.
and Where A,B,C are the lengths of the three sides.
Looking through my textbook and the material that we have covered in class the only way i can figure out to write that line is





but this always gives me an area of zero so i'm pretty sure that this isn't the best way to go about writing the algorithm. I personally think that it's getting messed up in the squareroot part but if someone could please help me with this algorithm that would be great thanks a lot.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Other than using Math.sqrt(x) instead of Math.pow(x, 0.5), my only suggestion is to post more of your code. I just coded up a solution using your formula and it worked. Well, I didn't verify the areas, but it prints zero only when I expect it (a + b == c, which is an empty triangle).

So to recap.
  • Use Math.sqrt()
  • Post more of your code
  • Post output from a run that demonstrates failure, preferably with printlns of your sides and permeter (any intermediate calculations)

  • Jeanne Boyarsky
    internet detective
    Marshal

    Joined: May 26, 2003
    Posts: 30294
        
    150

    Russell,
    Welcome to JavaRanch!

    This looks correct to me. I suggest breaking up the expression so you can see where things are going wrong. For example:

    That way you can print each of the variables (or step through them if using a debugger) and see where things aren't what you expect.


    [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
    Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24183
        
      34

    My guess is that Russell is using int variables instead of doubles, and the resulting integer math is causing fractions to be rounded to 0.


    [Jess in Action][AskingGoodQuestions]
    russell lloyd
    Greenhorn

    Joined: Feb 09, 2005
    Posts: 18
    thank you guys for all of this as it turns out my code is right but i wasn't thinking beacause my test that i was using was sides = 1,2,3 which isn't a triangle(boy i felt stupid) but thank all of you for your suggestions. Sorry it took so long to thank you guys I fell asleep last night
    David Harkness
    Ranch Hand

    Joined: Aug 07, 2003
    Posts: 1646
    Yes, I did the same thing as well: 1 1 2 == 0 ... "What?! Mine doesn't work either."
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Using heron's formula to calculate the area of a triangle