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