Results 1 to 8 of 8
  1. #1
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default 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);
    		
    		
    	}
    
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,348
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,813
    Rep Power
    19

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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!

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,813
    Rep Power
    19

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

    Quote Originally Posted by kjkrum View Post
    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!
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default 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?

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,813
    Rep Power
    19

    Default 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.

    ** This space for rent **

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,348
    Blog Entries
    7
    Rep Power
    20

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

    Quote Originally Posted by psx2514 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 01-09-2013, 08:28 AM
  2. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  3. Program skips "If" code and goes straight to "Else"
    By Logik22 in forum New To Java
    Replies: 12
    Last Post: 01-21-2012, 05:40 PM
  4. Replies: 24
    Last Post: 06-21-2010, 04:49 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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