Results 1 to 13 of 13
 03272014, 08:31 PM #1Member
 Join Date
 Mar 2014
 Posts
 10
 Rep Power
 0
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 rightangled 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 rightangled triangles should be classified as rightangled 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("rightangled"); } 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(); } } }
Last edited by gevin20; 03272014 at 08:34 PM.
 03272014, 08:44 PM #2
Re: anyone with very good math? need your help
when i give isosceles right angle triangle.it is printing Isosceles instead of Rightangled.If you don't understand my response, don't ignore it, ask a question.
 03272014, 09:16 PM #3Member
 Join Date
 Mar 2014
 Posts
 10
 Rep Power
 0
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
 03272014, 09:25 PM #4
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.
 03272014, 09:58 PM #5Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,425
 Rep Power
 9
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 03282014, 08:44 AM #6Member
 Join Date
 Mar 2014
 Posts
 10
 Rep Power
 0
 03282014, 12:59 PM #7
Re: anyone with very good math? need your help
where should i make changes?
Perhaps something like this: Math.abs(value1  value2) < CloseEnough
where CloseEnough = 0.00001If you don't understand my response, don't ignore it, ask a question.
 03282014, 01:04 PM #8Moderator
 Join Date
 Apr 2009
 Posts
 12,918
 Rep Power
 22
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 **
 03282014, 01:51 PM #9Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 5,114
 Rep Power
 9
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))))
"Syntactic sugar causes cancer of the semicolon."  Alan Perlis
 03282014, 02:00 PM #10Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,425
 Rep Power
 9
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 03282014, 02:06 PM #11Moderator
 Join Date
 Apr 2009
 Posts
 12,918
 Rep Power
 22
 03282014, 02:21 PM #12Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 5,114
 Rep Power
 9
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
 03282014, 04:04 PM #13Moderator
 Join Date
 Apr 2009
 Posts
 12,918
 Rep Power
 22
Similar Threads

Math help
By stuffses in forum New To JavaReplies: 5Last Post: 03092012, 02:00 AM 
Math.cos()
By ravi1 in forum New To JavaReplies: 5Last Post: 03272011, 02:52 PM 
Create Math.sin without math.sin
By vudoo in forum New To JavaReplies: 11Last Post: 12072010, 07:23 AM 
Hello Good Morning, Good afternoon, and Good Evening
By MrFreeweed in forum IntroductionsReplies: 3Last Post: 12112009, 04:32 PM 
Anyone good at math?
By iWonder in forum Forum LobbyReplies: 12Last Post: 01112009, 05:12 PM
Bookmarks