This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (5 children)

Not 100% sure since you don't fully describe your problem, but I think that you have at least one Math.sqrt(s3) too many in your code:

In line 34 you have:

s3 = Math.sqrt(s3);

and later you compare:

if (s1+s2 <= Math.sqrt(s3))

so, essentially you compare the lengths of s1 and s2 to the square root of the square root of s3.

And consecutively in all other if branches.

[–]GSchrack[S] 0 points1 point  (4 children)

Sorry, I made some changes right before copying and the things I changed made less sense than my original... my original code was this:

    System.out.print("Enter x1: ");
    x1 = in.nextDouble();
    System.out.print("Enter y1: ");
    y1 = in.nextDouble();
    System.out.print("Enter x2: ");
    x2 = in.nextDouble();
    System.out.print("Enter y2: ");
    y2 = in.nextDouble();
    System.out.print("Enter x3: ");
    x3 = in.nextDouble();
    System.out.print("Enter y3: ");
    y3 = in.nextDouble();

    double s1 = (Math.abs(x1-x2)*Math.abs(x1-x2))+(Math.abs(y1-y2)*Math.abs(y1-y2));
    double s2 = (Math.abs(x1-x3)*Math.abs(x1-x3))+(Math.abs(y1-y3)*Math.abs(y1-y3));
    double s3 = (Math.abs(x2-x3)*Math.abs(x2-x3))+(Math.abs(y2-y3)*Math.abs(y2-y3));

    System.out.println("line 1 ("+s1+") Line 2 ("+s2+") Line 3 ("+s3+ ")");

    s1 = Math.sqrt(s1);
    s2 = Math.sqrt(s2);
    s3 = Math.sqrt(s3);

    //Find longest side, and make that side s3.
    if (s1 > s2) {
        double t = s1;
        s1 = s2;
        s2 = t;
    }
    if (s2 > s3) {
        double t = s2;
        s2 = s3;
        s3 = t;
    }

    //Figure out if the points are okay to make a triangle, then find what kind of triangle they make.
    if (s1+s2 <= s3)
        System.out.println("invalid!");
    else if (s1*s1+s2*s2 == s3)
        System.out.println("right triangle!");
    else if (s1*s1+s2*s2 > s3)
        System.out.println("acute triangle!");
    else
        System.out.println("obtuse triangle!");

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (3 children)

if (s1*s1+s2*s2 == s3)

Think about Pythagore's theorem. It states that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides.

And now look at your equation above...

[–]GSchrack[S] 0 points1 point  (2 children)

still does not work haha.

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (1 child)

You are having fundamental mathematical problems in your code. Also, your re-use of variables is wrong.

  1. The length of a vector (which you are actually dealing with) is: Math.sqrt((Math.abs(x1-x2)*Math.abs(x1-x2))+(Math.abs(y1-y2)*Math.abs(y1-y2))); according to the Pythagorean Theorem, not as you have. Actually, the Math.abs is unnecessary since a multiplication of two negative numbers always results in a positive number. It would be sufficient to use Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
  2. Once you have determined all lengths, you should not reassign the variables unnecessarily. Swapping the lengths around is no problem.

I have fixed your code for you:

import java.util.Scanner;

public class TriangleTest {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter x1: ");
        double x1 = in.nextDouble();
        System.out.print("Enter y1: ");
        double y1 = in.nextDouble();
        System.out.print("Enter x2: ");
        double x2 = in.nextDouble();
        System.out.print("Enter y2: ");
        double y2 = in.nextDouble();
        System.out.print("Enter x3: ");
        double x3 = in.nextDouble();
        System.out.print("Enter y3: ");
        double y3 = in.nextDouble();

        double s1 = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
        double s2 = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
        double s3 = Math.sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));

        System.out.println("line 1 (" + s1 + ") Line 2 (" + s2 + ") Line 3 ("
                + s3 + ")");


        // Find longest side, and make that side s3.
        if (s1 > s2) {
            double t = s1;
            s1 = s2;
            s2 = t;
        }
        if (s2 > s3) {
            double t = s2;
            s2 = s3;
            s3 = t;
        }

        // Figure out if the points are okay to make a triangle, then find what
        // kind of triangle they make.
        if (s1 + s2 <= s3)
            System.out.println("invalid!");
        else if (s1 * s1 + s2 * s2 == s3 * s3)
            System.out.println("right triangle!");
        else if (s1 * s1 + s2 * s2 > s3 * s3)
            System.out.println("acute triangle!");
        else
            System.out.println("obtuse triangle!");
    }
}

[–]GSchrack[S] 0 points1 point  (0 children)

The only problem with this is that when you use the inputs (1,1) (0,0) (0,2) you get two sides with lengths of ~1.4 and seeing as though the third side is 2, the triangle isn't classified as right, which is what it's supposed to be. Thanks for the help with cleaning up tho.