Results 1 to 13 of 13
  1. #1
    gevin20 is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default anyone with very good math? need your help

    problem statement--

    Consider a triangle with three sides measuring a, b, and c units. A triangle is a right-angled triangle if
    (a*a)+ (b*b) = (c*c)
    Allow a tolerance of 0.000001 in the comparison in the above case i.e
    (a*a) + (b*b) = (c*c) +/- 0.000001
    A triangle is an isosceles triangle if any two of its sides are equal. A triangle is an equilateral triangle if all the three sides are equal. Three values can be the dimensions of a triangle if and only if the sum of every pair of values is greater than the third value. Otherwise, it is an invalid triangle.
    Write a program that reads three real numbers and finds out whether they can be the sides of the triangle and if they do, prints what type of triangle it is. Even though all equilateral triangles are isosceles, your program should classify an equilateral triangle to be an equilateral only. Similarly isosceles right-angled triangles should be classified as right-angled and not isosceles.

    Java Code:
     public class First {
    	
    	public static void main(String[] args)
    	{
    		double a,b,c,d,e,f;
    		try
    		{
    			Scanner sc=new Scanner(System.in);
    			a=sc.nextDouble();
    			b=sc.nextDouble();
    			c=sc.nextDouble();
    	
    		d=(a*a)+(b*b);
    		e=(a*a)+(c*c);
    		f=(b*b)+(c*c);
    		if((a>(b+c))||(b>(a+c))||(c>(a+b)))
    		{
    			System.out.println("invalid");
    		}
    		else if( ((d==(c*c))||(d==((c*c)+0.000001))||(d==((c*c)-0.000001))) || ((e==(b*b))||(e==((b*b)+0.000001))
                    ||(e==((b*b)-0.000001))) || ((f==(a*a))||(f==((a*a)+0.000001))||(f==((a*a)-0.000001)))) 
    		{
    			System.out.println("right-angled");
    		}
    		else if(((a==c)&&(a!=b))||((a==b)&&(a!=c))||((b==c)&&(b!=a)))
    		{
    			System.out.println("isosceles");
    		}
    		else if((a==b)&&(b==c)&&(a==c))
    		{
    			System.out.println("equilateral");
    		}
    		}
    		catch(Exception ex)
    		{
    			ex.printStackTrace();
    		}			
    	}
    	
    
    }
    the code is failing when i give isosceles right angle triangle.it is printing Isosceles instead of Right-angled. What changes should i do??
    Last edited by gevin20; 03-27-2014 at 07:34 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: anyone with very good math? need your help

    when i give isosceles right angle triangle.it is printing Isosceles instead of Right-angled.
    That would say that all the boolean expressions in the else if statement are false. Which one should be true for the if to be true?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gevin20 is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: anyone with very good math? need your help

    consider a=8.00000 b=8.000000 and c=11.3137085
    then
    (8*8)+(8*8)=(11.3137085*11.3137085)
    i.e right angle triangle but it is showing isosceles

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: anyone with very good math? need your help

    What are the values of those two expressions?
    It would be unusual for the product of int values to be equal to the product of floating point numbers.

    How close would those two values need to be for you to consider them to be "equal"?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: anyone with very good math? need your help

    A couple of things to think about.

    1. Would it be easier to figure all this out if you knew which side was the largest, the smallest, and the one in the middle (regardless of whether any
    of them are equal or not)?
    2. There are only five possibilities. Invalid, Right, Isoceles, Equilateral, and Scalene. If any four of those five fail the test, guess what's left. This could help you structure your code (even though you aren't asked to determine if the tirangle is scalene or not).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    gevin20 is offline Member
    Join Date
    Mar 2014
    Posts
    10
    Rep Power
    0

    Default Re: anyone with very good math? need your help

    Quote Originally Posted by Norm View Post
    What are the values of those two expressions?
    It would be unusual for the product of int values to be equal to the product of floating point numbers.

    How close would those two values need to be for you to consider them to be "equal"?
    there is not a single int in program all variable's are of double type .in your view where should i make changes?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: anyone with very good math? need your help

    where should i make changes?
    How close in value should the values be to consider them equal? Did you print out the the two values in post#3 and compare them?
    Perhaps something like this: Math.abs(value1 - value2) < CloseEnough
    where CloseEnough = 0.00001
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: anyone with very good math? need your help

    This is the bit of the requirements you posted that Norm is suggesting you are missing:
    "Allow a tolerance of 0.000001 in the comparison in the above case."
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,994
    Rep Power
    5

    Default Re: anyone with very good math? need your help

    ... but it is not missing is it? I mean I see this in the posted code which seems to match exactly what the requirements state:

    Java Code:
    else if( ((d==(c*c))||(d==((c*c)+0.000001))||(d==((c*c)-0.000001))) || ((e==(b*b))||(e==((b*b)+0.000001))
                    ||(e==((b*b)-0.000001))) || ((f==(a*a))||(f==((a*a)+0.000001))||(f==((a*a)-0.000001))))
    I must be really stupid, what am I not seeing that you guys are?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: anyone with very good math? need your help

    The OP is checking tolerances with == and not < or >. That is wrong. Use Norm's suggestion.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: anyone with very good math? need your help

    Quote Originally Posted by gimbal2 View Post
    ... but it is not missing is it? I mean I see this in the posted code which seems to match exactly what the requirements state:

    Java Code:
    else if( ((d==(c*c))||(d==((c*c)+0.000001))||(d==((c*c)-0.000001))) || ((e==(b*b))||(e==((b*b)+0.000001))
                    ||(e==((b*b)-0.000001))) || ((f==(a*a))||(f==((a*a)+0.000001))||(f==((a*a)-0.000001))))
    I must be really stupid, what am I not seeing that you guys are?
    What if c*c equals d + 0.0000005 (that's half of 0.000001, so you don't have to count the zeros...:))
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,994
    Rep Power
    5

    Default Re: anyone with very good math? need your help

    DOH! Yes, I'm stupid. Oh man I'm going to feel ashamed for this one for a loooooooong time.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: anyone with very good math? need your help

    It's Friday...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Math help
    By stuffses in forum New To Java
    Replies: 5
    Last Post: 03-09-2012, 01:00 AM
  2. Math.cos()
    By ravi1 in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 02:52 PM
  3. Create Math.sin without math.sin
    By vudoo in forum New To Java
    Replies: 11
    Last Post: 12-07-2010, 06:23 AM
  4. Hello Good Morning, Good afternoon, and Good Evening
    By MrFreeweed in forum Introductions
    Replies: 3
    Last Post: 12-11-2009, 03:32 PM
  5. Anyone good at math?
    By iWonder in forum Forum Lobby
    Replies: 12
    Last Post: 01-11-2009, 04:12 PM

Posting Permissions

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