Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Heron's Formula

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Heron's Formula

    Heyy friendss!!
    Please help me with my program...below!
    nd tell me what is the correction.. I'm always getting the area as 0 ... so help me!
    Thanxx!





    class heron
    {
    public static void main(String args[])
    {
    int a=6,b=4,c=10;
    int s= (a+b+c) / 2;
    double area = Math.sqrt(s*(s-a) * (s-b) * (s-c));
    System.out.println("Program to calculate the Area of a Triangle using Heron's Formula");
    System.out.println(" ");
    System.out.println("The First side of the Triangle is " +a);
    System.out.println("The Second side of the Triangle is "+b);
    System.out.println("The Third side of the Triangle is " +c);
    System.out.println("The Area of the Triangle is " + area);
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Heron's Formula

    What does the program print out when it executes? Please copy the full output and paste it here.
    What is the correct answer? Have you used a calculator to verify the code has the right equation?


    ON windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2009
    Posts
    2
    My Mood
    Where
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Heron's Formula

    What are the values you are seeing in each step of the area calculation? If you were doing this by hand, what would you get?

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Heron's Formula

    Well...

    Try to draw a triangle with sides 6, 4, 10. (Graph paper and a compass will help here.)

    The area is zero, as the formula shows.

    Change the values of the sides to 6, 4, 5. What do you get? Is this correct?

    The correct answer is 9.921567416492215, as obtained from Online Conversion - Area of a scalene triangle. There are about a million other places on line where you can check your work (maybe more).

    Now if you don't get this value, go back and calculate each step by hand (and calculator). Put print statements in your program to see whether the values are the same as your hand calculations.

     
            int a = 6, b = 4, c = 5;
            int s = (a + b + c) / 2;
     
            System.out.println("s = " + s);
            System.out.println("s*(s-a)*(s-b)*(s-c) = " + s*(s-a)*(s-b)*(s-c));
     
            double area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
    .
    .


    Bottom line: When properly implemented, Heron's rule can find the area of a triangle.

    However...

    Not every set of numbers a, b, and c can represent lengths of sides of a triangle. Maybe the program should include a check for validity before trying to compute the area.

    What conditions on a, b, c must be met so that they can be the sides of a triangle?



    Cheers!

    Z

Similar Threads

  1. [SOLVED] is there any good formula's for 2d java game - to make ur player move right or left?
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 23rd, 2012, 01:42 AM
  2. Distance formula with arrays, and loops. help?
    By smith999 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 22nd, 2012, 06:27 PM
  3. i am having a problem with the distance formula,help needed!
    By Bentino in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2012, 07:07 PM
  4. [SOLVED] Putting Trajectory of a Projectile formula in Java form.
    By mwebb in forum Java Theory & Questions
    Replies: 2
    Last Post: March 2nd, 2012, 07:23 PM
  5. Method is not initiating formula
    By CrimsonFlash in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 22nd, 2011, 08:19 PM