public class Triangle

/*-the lengths of all sides
-the perimeter
-the angles at all corners
-the area */

public Triangle(double xa, double ya, double xb, double yb, double xc, double yc)
x1 = xa;
y1 = ya;
x2 = xb;
y2 = yb;
x3 = xc;
y3 = yc;

}//instance variables
private double x1;
private double y1;
private double x2;
private double y2;
private double x3;
private double y3;
private double lengthAB;
private double lengthAC;
private double lengthBC;
private double perimeter;
private double area;
private double angle1;
private double angle2;
private double angle3;
private double S;
private double area1;

public double getLengthAB()
lengthAB = Math.sqrt(Math.pow(x2-x1, 2)+Math.pow(y2-y1, 2));//calculates length of AB
return lengthAB;
public double getLengthAC()
lengthAC = Math.sqrt(Math.pow(x3-x1, 2)+Math.pow(y3-y1, 2));//calculates length of AC
return lengthAC;
public double getLengthBC()
lengthBC = Math.sqrt(Math.pow(x3-x2, 2)+Math.pow(y3-y2, 2));//calculates length of BC
return lengthBC;
public double getPerimeter()//calculates perimeter
perimeter = lengthAB + lengthAC + lengthBC;
return perimeter;
public double getS()
S = (1/2) * perimeter;
return S;
public double getArea()//calulates area
area1 = (S*(S-lengthAB)*(S-lengthAC)*(S-lengthBC));

area = Math.sqrt(area1);
return area;
double AB2 = Math.pow(lengthAB, 2);
double AC2 = Math.pow(lengthAC, 2);
double BC2 = Math.pow(lengthBC, 2);

public double getAngle1()//calulates angle1
angle1 = Math.acos((AC2 + BC2 - AB2)/(2*lengthAC*lengthBC))*(180/Math.PI);
return angle1;
public double getAngle2()//calulates angle2
angle2 = Math.acos((AC2 + BC2 - AB2)/(2*lengthAC*lengthBC))*(180/Math.PI);
return angle2;
public double getAngle3()//calulates angle3
angle3 = Math.acos((AB2 + AC2 - BC2)/(2*lengthAB*lengthAC))*(180/Math.PI);
return angle3;
import java.util.Scanner;

public class TriangleTester
public static void main(String[] args)

Scanner in = new Scanner(;
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("Length AB is: " + tri.getLengthAB());
System.out.println("Length AC is: " + tri.getLengthAC());
System.out.println("Length BC is: " + tri.getLengthBC());
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());


it gives me an area of -0.0 and gives me 90degrees for each angle
idk whats wrong :S

Edited 6 Years Ago by wowz: n/a


S = (1/2) * perimeter;

should be

S = (1/2.0) * perimeter;

(or something similar), otherwise (1/2) is integer math and produces 0, which makes S 0.
(or simply S = 0.5 * perimeter; or S = perimeter/2;)

I would also "predetermine" the value of (180/Math.PI) rather than actually doing that operation in each method. Also, angle1 and angle2 are both using the same formula (i.e. both determining angle1). One of those should be ab2 + bc2 (with the other approriate changes) as that is the one that is not calculated. Other than that the formula is correctly implemented.

Edited 4 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.