# Thread: Which of these two ways to write this program is "better"?

1. Member Join Date
Oct 2012
Posts
50
Rep Power
0

## Which of these two ways to write this program is "better"?

I wrote two versions of a simple program that gives the area of a triangle using Heron's formula. Will someone tell me which version is better, and explain why. Thanks.
This is the first version:
Java Code:
```import java.util.Scanner;
import java.text.DecimalFormat;
public class AreaOfTriangle
{
public static void main(String[] args)
{
double sideA, sideB, sideC, s, area;
Scanner scan = new Scanner(System.in);

System.out.println("This program finds the area of a triangle" +
" using Heron's formula.\n");
System.out.print("Enter the length of the first side: ");
sideA = scan.nextDouble();
System.out.print("Enter the length of the second side: ");
sideB = scan.nextDouble();
System.out.print("Enter the length of the third side: ");
sideC = scan.nextDouble();
s = (sideA+sideB+sideC)/2;
area = Math.sqrt(s*(s-sideA)*(s-sideB)*(s-sideC));
DecimalFormat fmt = new DecimalFormat("0.###");
Double new_area = new Double(fmt.format(area));
System.out.println("The area is: " + new_area);

}

}```
and this is the second version:
Java Code:
```import java.util.Scanner;
import java.text.DecimalFormat;
public class AreaOfTriangle
{
public static void main(String[] args)
{
double sideA, sideB, sideC, s, area;
Scanner scan = new Scanner(System.in);

System.out.println("This program finds the area of a triangle" +
" using Heron's formula.\n");
System.out.print("Enter the length of the first side: ");
sideA = scan.nextDouble();
System.out.print("Enter the length of the second side: ");
sideB = scan.nextDouble();
System.out.print("Enter the length of the third side: ");
sideC = scan.nextDouble();
s = (sideA+sideB+sideC)/2;
area = Math.sqrt(s*(s-sideA)*(s-sideB)*(s-sideC));
DecimalFormat fmt = new DecimalFormat("0.###");
area = (Double.parseDouble(fmt.format(area)));
System.out.println("The area is: " + area);

}

}```  Reply With Quote

2. ## Re: Which of these two ways to write this program is "better"?

You probably don't want to read this but Heron's formula as used in both version is numerically unstable for triangles with one small angle; I suggest rewriting it as a more stable version (Google is your friend here).

kind regards,

Jos  Reply With Quote

3. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Which of these two ways to write this program is "better"?

Java Code:
```DecimalFormat fmt = new DecimalFormat("0.###");
Double new_area = new Double(fmt.format(area));
System.out.println("The area is: " + new_area);```
What is the purpose of this?
If it's to format the output then do that...don't format, reassign to a double, then print the double, which will in all likelihood not be the format you want.  Reply With Quote

4. ## Re: Which of these two ways to write this program is "better"?

Uh... what's the difference between the two versions?  Reply With Quote

5. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Which of these two ways to write this program is "better"? Originally Posted by kjkrum Uh... what's the difference between the two versions?
Oh good.
I didn't want to say anything yesterday in case I was missing something obvious!
:)  Reply With Quote

6. Member Join Date
Oct 2012
Posts
50
Rep Power
0

## Re: Which of these two ways to write this program is "better"?

The assignment asked to use Heron's formula.
Look at lines 20-24. You'll see the difference between the two programs there.
Now, which one is better?  Reply With Quote

7. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Which of these two ways to write this program is "better"?

Oh right.
But they both do pointless formatting and parsing...so neither?  Reply With Quote

8. ## Re: Which of these two ways to write this program is "better"? Originally Posted by psx2514 The assignment asked to use Heron's formula.
Look at lines 20-24. You'll see the difference between the two programs there.
Now, which one is better?
But there is a stable version for Heron's formula; Google for it and use that one and formatting a double and parsing the result afterwards again is just a no no.

kind regards,

Jos  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•