Results 1 to 8 of 8
- 03-14-2013, 04:10 PM #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:
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.###"); Double new_area = new Double(fmt.format(area)); System.out.println("The area is: " + new_area); } }
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); } }
- 03-14-2013, 04:26 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-14-2013, 04:33 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Which of these two ways to write this program is "better"?
What is the purpose of this?Java Code:DecimalFormat fmt = new DecimalFormat("0.###"); Double new_area = new Double(fmt.format(area)); System.out.println("The area is: " + new_area);
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.Please do not ask for code as refusal often offends.
- 03-15-2013, 05:29 AM #4
Re: Which of these two ways to write this program is "better"?
Uh... what's the difference between the two versions?
Get in the habit of using standard Java naming conventions!
- 03-15-2013, 01:47 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
- 03-15-2013, 03:27 PM #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?
- 03-15-2013, 03:42 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Which of these two ways to write this program is "better"?
Oh right.
But they both do pointless formatting and parsing...so neither?Please do not ask for code as refusal often offends.
- 03-15-2013, 05:53 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: Which of these two ways to write this program is "better"?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
How to write this program in JAVA?? "DVD Collection Application"?
By holmes4946 in forum New To JavaReplies: 1Last Post: 01-09-2013, 08:28 AM -
access denied("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")
By klspepper in forum New To JavaReplies: 0Last Post: 12-07-2012, 08:29 AM -
Program skips "If" code and goes straight to "Else"
By Logik22 in forum New To JavaReplies: 12Last Post: 01-21-2012, 05:40 PM -
how to write a program for " world cup " in java
By sus in forum New To JavaReplies: 24Last Post: 06-21-2010, 04:49 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks