-1

I'm working on a program to classify triangles into various classes (equilateral, scalene, right, etc.), given points of the triangle. I've gotten it 99% done, but there's one Else-if statement that wont work.

For the final test run, we are supposed to enter the points (0,0) (0,5) (5,0), then get the output "equilateral, right". However, my code just seems to skip that statement and proceed right to the next if statement, which classifies the triangle as scalene.

Thanks in advance. I'm a novice coder, so its probably something relatively simple that's evading my eyes.

Here's the entirety of the code.

public class TriangleCalculator {

    @SuppressWarnings("empty-statement")
    public static void main(String[] args) 
    {
      System.out.println("Enter the x- and y- coordinates of the first point");
      Scanner keyBoard = new Scanner (System.in);
        double coordOneX = keyBoard.nextDouble();
        double coordOneY = keyBoard.nextDouble();
      System.out.println("Enter the x- and y- coordinates of the second point");
        double coordTwoX = keyBoard.nextDouble();
        double coordTwoY = keyBoard.nextDouble();
      System.out.println("Enter the x- and y- coordinates of the third point");
        double coordThreeX = keyBoard.nextDouble();
        double coordThreeY = keyBoard.nextDouble(); 

        if((coordOneY - coordTwoY) * (coordTwoX - coordThreeX) == (coordTwoY - coordThreeY) * (coordOneX - coordTwoX))
        {
           System.out.printf ("D1 ={(%.3f,%.3f) , D2 = (%.3f,%.3f) , D3 = (%.3f,%.3f) are colinear and cannot be vertices of a triangle.}%n",
                coordOneX, coordOneY, coordTwoX, coordTwoY, coordThreeX, coordThreeY);
        }
        else if((coordOneY - coordTwoY) * (coordTwoX - coordThreeX) != (coordTwoY - coordThreeY) * (coordOneX - coordTwoX))
        {    
            System.out.printf ("Triangle ={(%.3f,%.3f) , (%.3f,%.3f) , (%.3f,%.3f) }%n",
                coordOneX, coordOneY, coordTwoX, coordTwoY, coordThreeX, coordThreeY);

            double distanceOne = Math.sqrt(Math.pow(coordOneX - coordTwoX,2) + Math.pow(coordOneY - coordTwoY,2));
                System.out.printf("Distance (P1,P2) = %.3f%n", distanceOne);
            double distanceTwo = Math.sqrt(Math.pow(coordTwoX - coordThreeX,2) + Math.pow(coordTwoY - coordThreeY,2));
                System.out.printf("Distance (P2,P3)= %.3f%n", distanceTwo);
            double distanceThree = Math.sqrt(Math.pow(coordOneX - coordThreeX,2) + Math.pow(coordOneY - coordThreeY,2));
                System.out.printf("Distance (P1,P3)= %.3f%n", distanceThree);   

            double perimeter = distanceOne + distanceTwo + distanceThree;
                System.out.printf("Perimeter = %.3f %n", perimeter);

            double s = perimeter / 2;
                double area = Math.sqrt(s * (s - distanceOne) * (s - distanceTwo) *(s - distanceThree));
                System.out.printf("Area = %.3f %n", area);

            double side1 = distanceOne;
            double side2 = distanceTwo;
            double side3 = distanceThree;               
            String classification = ("");

            if(side1 == side2)
                classification = "isosceles";
            else if(side2==side3)
                    classification = "equilateral";
            else if (Math.abs(side1*side1 + side2*side2 - side3*side3) < 1E-9)
                classification = "isoceles, right";
            if(side1 == side3||side2 == side3)
                 classification = "isosceles";
            else classification = "scalene";
            if(side1*side1 + side2*side2 == side3*side3)
                classification = "scalene, right";


        System.out.println("Clasification(s): " +classification);

This is the particular section of code in question:

        if(side1 == side2)
            classification = "isosceles";
        else if(side2==side3)
                classification = "equilateral";
        else if (Math.abs(side1*side1 + side2*side2 - side3*side3) < 1E-9)
            classification = "isoceles, right";
        if(side1 == side3||side2 == side3)
             classification = "isosceles";
        else classification = "scalene";
        if(side1*side1 + side2*side2 == side3*side3)
            classification = "scalene, right";
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Random8k
  • 1
  • 2

1 Answers1

1

Since doubles and floats are floating point numbers, you should not compare doubles with ==, this is because you can't really be 100% sure or guarantee that the number you assign to the float or double is exactly what is mapped in memory...

In your case use instead Double.compare and take a look at this article

Example

// compares the two specified double values
double d1 = 15.45;
double d2 = 11.50;
int retval = Double.compare(d1, d2);

According to the documentation:

JLS 15.21.1. Numerical Equality Operators == and !=:

The result of a floating-point comparison, as determined by the specification of the IEEE 754 standard, is:

Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard:

  • If either operand is NaN, then the result of == is false but the result of != is true. Indeed, the test x!=x is true if and only if the value of x is NaN. The methods Float.isNaN and Double.isNaN may also be used to test whether a value is NaN.

  • Positive zero and negative zero are considered equal. For example, -0.0==0.0 is true.

  • Otherwise, two distinct floating-point values are considered unequal by the equality operators. In particular, there is one value representing positive infinity and one value representing negative infinity; each compares equal only to itself, and each compares unequal to all other values.

Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • That's not going to help. `Double.compare()` just unboxes the parameters into `double` and uses `<`, `>` and `==` anyway. The main thing it adds is support for `NaN`. – azurefrog Mar 02 '16 at 19:18