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.

# Thread: Heron's Formula

1. ## 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. ## 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.

3. ## 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. ## 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