Hey guys, I'm making a program that takes 3 sets of coordinates from a user to make a triangle. It then calculates the length of each side, the perimeter of the triangle, the area of the triangle, and the 3 angles. I have it all coded, except for the area and angles, I get a return value of NaN. can anyone explain why this is happening?

public class Triangle
{
   public Triangle(double x, double y, double xa, double ya, double xb, double yb)
   {
       x1 = x;
       x2 = xa;
       x3 = xb;
       y1 = y;
       y2 = ya;
       y3 = yb;
    }
    

    private double x1;
    private double x2;
    private double x3;
    private double y1;
    private double y2;
    private double y3;
    private double A;
    private double B;
    private double C;
    private double perimeter;
    private double area;
    private double angle1;
    private double angle2;
    private double angle3;
    private double S;

    public double getSide1()
    {
        A = Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2));
        return A;
    }
    
    public double getSide2()
    {
        double B = Math.sqrt(Math.pow((x2-x3),2)+Math.pow((y2-y3),2));
        return B;
    }
    
    public double getSide3()
    {
        double C = Math.sqrt(Math.pow((x3-x1),2)+Math.pow((y3-y1),2));
        return C;
    }
    
        double AA = Math.pow(A, 2);
        double BB = Math.pow(B, 2);
        double CC = Math.pow(C, 2);
    
    public double getPerimeter()
    {
        perimeter = (A + B + C);
        return perimeter;
    }
    
    public double getS()
    {
        S = (perimeter * 0.5);
        return S;
    }
    
    
    public double getArea()
    {
        area = (Math.sqrt(S*(S-A)*(S-B)*(S-C)));
        return area;
    }

    public double getAngle1()
    {
        angle1 = Math.acos((BB + CC - AA)/(2*B*C))*(180/Math.PI); //gets the angle in radians and converts to degrees
        return angle1;
    }
    
    public double getAngle2()
    {
        angle2 = Math.acos((AA + CC - BB)/(2*A*C))*(180/Math.PI);
        return angle2;
    }
    
    public double getAngle3()
    {
        angle3 = Math.acos((AA + BB - CC)/(2*B*A))*(180/Math.PI);
        return angle3;
    }
    
}
import java.util.Scanner;
public class TriangleSimulator
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        

        System.out.println("Please enter the first x coordinate:");
        double x1 = in.nextDouble();
        
        System.out.println("Please enter the first y coordinate:");
        double y1 = in.nextDouble();
        
        System.out.println("Please enter the second x coordinate:");
        double x2 = in.nextDouble();
        
        System.out.println("Please enter the second y coordinate:");
        double y2 = in.nextDouble();
        
        System.out.println("Please enter the third x coordinate:");
        double x3 = in.nextDouble();
        
        System.out.println("Please enter the third y coordinate:");
        double y3 = in.nextDouble();
        
        Triangle tri = new Triangle(x1, y1, x2, y2, x3,  y3);
         
        System.out.println("Side 1 is: " + tri.getSide1());
        System.out.println("Side 2 is: " + tri.getSide2());
        System.out.println("Side 3 is: " + tri.getSide3());   
        System.out.println("The perimeter is: " + tri.getPerimeter());    
        System.out.println("The area is: " + tri.getArea());  
        System.out.println("Angle 1 is: " + tri.getAngle1());  
        System.out.println("Angle 2 is: " + tri.getAngle2());   
        System.out.println("Angle 3 is: " + tri.getAngle3());   
    }
}

NaN. Read the section on "How NaN is created", step through your code a single method at a time and it should be pretty easy. Use a debugger; a slick one which comes with Eclipse or a primitive command line debugger like "jdb" should do the job.

commented: NaN - more than just an elderly female relative :) +29

solved, thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.