Can you help me find my logic error
I'm almost done! I am supposed to make a program that allows a user to input the sides of a triangle and print out whether it's an equilateral, obtuse/acute isosceles, or no triangle. Right triangle isosceles doesn't have to be tested for since my assignment requires only ints.
public String triangle(int Side1, int Side2, int Side3)
{
if(Side1 == Side2 && Side1 == Side3 && Side2 == Side3)
{
return "equilateral";
}
if ((Side1 == Side2 || Side1 == Side3 || Side2 == Side3) &&
((Math.pow(Side1,2) + Math.pow(Side2,2)) < Math.pow(Side3,2) ||
(Math.pow(Side3,2) + Math.pow(Side1,2)) < Math.pow(Side2,2) ||
(Math.pow(Side3,2) + Math.pow(Side2,2)) < Math.pow(Side1,2) ))
{
return "obtuse isosceles" ;
}
if ((Side1 == Side2 || Side1 == Side3 || Side2 == Side3) &&
((Math.pow(Side1,2) + Math.pow(Side2,2)) > Math.pow(Side3,2) ||
(Math.pow(Side3,2) + Math.pow(Side1,2)) > Math.pow(Side2,2) ||
(Math.pow(Side3,2) + Math.pow(Side2,2)) > Math.pow(Side1,2)))
{
return "acute isosceles" ;
}
else
{
return "No triangle" ;
}
I'm almost done! I am supposed to make a program that allows a user to input the sides of a triangle and print out whether it's an equilateral, obtuse/acute isosceles, or no triangle. Right triangle isosceles doesn't have to be tested for since my assignment requires only ints.
Basically, If side1^2 + side2^2 < side3^2 then it is an obtuse
If side1^2 +side2^2 is greater than side3^2 then it is acute.
When I input 2, 3, 2 it says i got an obtuse isosceles
however when i input 3,4,3 it says i got an acute isosceles...
Wtf?
2^2 + 3^2 > 2^2 so this should be acute not obtuse....
3^2 + 4^2 > 3^2 this was correct.
But what's wrong? I thought maybe it was multiplying the same lengths first and than comparing them to the other side. For example,
2^2 +2^2 < 3^2 which yields obtuse which is correct
3^2 + 3^2 > 4^2 which yields acute which is correct
but why would it be doing this?
Thanks you so much
Re: Can you help me find my logic error
obtuse : any two side lengths where the sum of their squares is less than the square of the other side length
right:any two side lengths where the sum of their squares is equal to the square of the other side length
acute: a triangle is an acute triangle if it is neither a right triangle nor an obtuse triangle
Re: Can you help me find my logic error
Quote:
Originally Posted by
michael305rodri
I'm almost done!...
That remains to be seen.
First of all: According to mathematicians, the definition of an obtuse triangle is a triangle that has an angle greater than a right angle (that's 90 degrees to us mere humans). If a triangle does not have an angle that is greater than a right angle, it is called an acute triangle. Get it? A triangle is either obtuse or it is acute, depending on whether it has an angle that is greater than 90 degrees.
Separate and independent of obtuseness or acuteness properties, the definition of an isosceles triangle is a triangle that has two equal sides. (The special case where all three sides are equal is called an equilateral triangle. Equilateral triangles, by definition are also isosceles triangles.) If no two sides are equal (that is, if all three sides have different lengths), the triangle is called scalene.
So, here's the deal: An isosceles triangle can be either obtuse or acute. A scalene triangle can be either obtuse or acute.
(An equilateral triangle is necessarily acute since all angles are 60 degrees, and, therefore does not have an angle greater than 90 degrees.)
Now...
If you have some theorem about relationships between squares of sides and obtuseness/acuteness of triangles, I don't know what it is. The fact that you have shown incorrect program output from use of your conditions in triangles with side lengths 2,2,3, means that your conditions are incorrect. At least that's the way that I see it.
So, let's begin at the beginning:
We are given three positive integers. We want to characterize a triangle with side lengths equal to those three integer values.
The order of the integers is unimportant. There is no concept of "position" or "orientation" here. So, as far as the triangle properties that we are interested in, the set of lengths 2,2,3 is the same as 2,3,2 and that's the same as 3,2,2, right?
First of all: If we can take two of the sides and find that the sum of their lengths is less than the length of the other, we can't make a triangle out of it. Period. (Test some integers like 1, 1, 3. Try to make a triangle out of those.)
So...
The condition that we could test is
Code :
((side1 + side2) < side3) OR ((side1 + side3) < side2) OR ((side2 + side3) < side1)
If the result of that expression is true, then we can't make a triangle out of those lengths, no matter how hard we try. We are not interested in this case, but we really should test it.
Secondly, if all of the side lengths are different, we know it is not an isosceles triangle, and we aren't interested in this case either, according to your problem description.
So...
The condition that we could test is
Code :
(side1 != side2) AND (side1 != side3) AND (side2 != side3)
If the result of that expression is true, then no two sides are equal, and it is not isosceles, and we aren't interested in that case.
(Note that we don't actually have to do this test, since it can be covered by some other test that we have to do anyhow, but I put it here for purposes of description.)
Thirdly, if all of the side lengths are the same, we know it is an equilateral triangle. Furthermore, since the angles of an equilateral triangle are all equal to 60 degrees, it is an acute triangle. No need for further testing.
So...
The condition that we could test is
Code :
(side1 == side2) AND (side2 == side3)
If the result of that expression is true, then we don't have to do anything else, right? (Question: why don't we have to test (side1 == side3) also?) We can report the equilateralness of the thingie.
Now, we have reached a place that we are actually interested in and that requires a little calculation
If two of the sides have the same length, let's call that value "a" Let's call the other one's length "b"
So...
The condition that we could test is:
Code :
if (side1 == side2) then let a = side1 and let b = side3
else if (side1 == side3) then let a = side1 and let b = side2
else if (side2 == side3) then let a = side2 and let b = side1
(else it's not isosceles. If we haven't already done a separate test for this, here's where we return from the function.)
I choose "b" to designate the "base" of the triangle that I am going to draw.
I suggest that you get a pencil and paper and try the following:
Draw a triangle with b at the bottom and connect the two "a" sides together above the base. When I was in the tenth grade they taught a course called Plane Geometry where we learned to construct stuff like this on paper using only a straight-edge and a compass. I don't know whether they still teach this, but it's not hard to do and to prove that it can be done under conditions that we have already verified. Anyhow, I'll call the height above the base "h".
Get it?
"b" is the length of the base. "h" is the height above the base where the two equal-length "a" sides intersect.
If we draw a line from the point where the two "a" sides intersect in such a way that it perpendicular to the base, it divides the base exactly in half, and we form two right triangles. Again, this can easily be proved using elementary theorems about congruent triangles from plane geometry, but if you do it (carefully) with pencil and paper, you can observe it.
Got it? Stop and draw a picture. Convince yourself that I haven't made a mistake in my reasoning. (Or point it out to me if I have.)
Now...
Thanks to Pythagoras, we can calculate "h", right? To keep from having to find the square root of a number, we can write
Code :
(h squared) = (a squared) - ((b/2) squared)
Now, let's look at some angles. That's the whole point of the exercise, inspect the angles.
Let theta be the angle between the two "a" sides where they intersect, and set phi equal to theta divided by 2. (We don't actually need variables in our program for these. This is just terminology used in the description and derivation.)
Now, if phi is greater than 45 degrees, we know that theta is greater than 90 degrees, and it is an obtuse triangle. On the other hand, if phi is not greater than 45 degrees, then the triangle does not have an angle greater than 90 degrees, so it is acute.
Again, this can be proved by elementary plane geometry theorems, since we know that the sum of the interior angles of a triangle must add up to what mathematicians call a "straight angle," and we humans call 180 degrees. The two angles formed by the base and the two "a" sides are equal.
OK! Don't lose heart. We are coming into the home stretch:
The tangent of phi is equal to b/2 divided by h. If the tangent of phi is greater than 1, we know that phi is greater than 45 degrees, so theta is greater than 90 degrees, and it is an obtuse triangle. Otherwise it is an acute triangle.
Of course, you don't actually have to divide b/2 by h to know whether the first is greater than the second, you can simply compere ((b/2) squared) with (h squared). If the first is greater than the second, it's obtuse, otherwise it's acute. Bada-bing, bada-boom.
Bottom line:
For given side lengths, it isn't actually necessary to do trigonometry calculations. Just a few comparisons and a couple of multiplications.
Cheers!
Z
Re: Can you help me find my logic error
Thank you :) I forgot to test for a right triangle so I got B on the assignment but It's cool. Thanks for the clarification.