Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Can you help me find my logic error

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Can you help me find my logic error

    Quote Originally Posted by michael305rodri View Post
    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
    ((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
    (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
    (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:
    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
    (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
    Last edited by Zaphod_b; October 15th, 2012 at 11:58 AM.

  4. The Following User Says Thank You to Zaphod_b For This Useful Post:

    michael305rodri (November 5th, 2012)

  5. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

Similar Threads

  1. Logic Error in noughts and crosses game
    By eyesackery in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 15th, 2012, 08:40 AM
  2. Lotto Problem logic error
    By ippo in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 10th, 2012, 11:18 AM
  3. Serial Programming Logic Error???
    By bczm8703 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 27th, 2011, 04:28 AM
  4. logic error somewhere.. working with try & catch, simple array.. please help
    By basketball8533 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 9th, 2010, 12:40 PM
  5. [SOLVED] logic error: cpu assigning incorrect values in for loop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 25th, 2010, 09:13 PM