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

• 03-14-2013, 05:10 PM
psx2514
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:
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:
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);                                         } }```
• 03-14-2013, 05:26 PM
JosAH
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
• 03-14-2013, 05:33 PM
Tolls
Re: Which of these two ways to write this program is "better"?
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.
• 03-15-2013, 06:29 AM
kjkrum
Re: Which of these two ways to write this program is "better"?
Uh... what's the difference between the two versions?
• 03-15-2013, 02:47 PM
Tolls
Re: Which of these two ways to write this program is "better"?
Quote:

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!
:)
• 03-15-2013, 04:27 PM
psx2514
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?
• 03-15-2013, 04:42 PM
Tolls
Re: Which of these two ways to write this program is "better"?
Oh right.
But they both do pointless formatting and parsing...so neither?
• 03-15-2013, 06:53 PM
JosAH
Re: Which of these two ways to write this program is "better"?
Quote:

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