1. Member
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 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 08:34 PM.

2. 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?

3. Member
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

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

5. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

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

6. Member
Join Date
Mar 2014
Posts
10
Rep Power
0

Re: anyone with very good math? need your help

Originally Posted by Norm
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. 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

8. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

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

9. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

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?

10. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

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

11. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

Re: anyone with very good math? need your help

Originally Posted by gimbal2
... 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...:))

12. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

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.

13. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

Re: anyone with very good math? need your help

It's Friday...:)

Posting Permissions

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