public class Main {
public static void main(String[] args) {
}
}
|
public class Triangle {
private final XYPoint p1;
private final XYPoint p2;
private final XYPoint p3;
private final double side12;
private final double side23;
private final double side31;
public Triangle(XYPoint p1, XYPoint p2, XYPoint p3) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
side12 = XYPoint.distance(p1,p2);
side23 = XYPoint.distance(p2,p3);
side31 = XYPoint.distance(p3,p1);
}
private Triangle(Triangle original) {
this(original.p1,original.p2,original.p3);
}
public Triangle copy() {
return new Triangle(this);
}
public double getPerimeter() {
return side12 + side23 + side31;
}
public double getArea() {
double s = .5 * getPerimeter();
return Math.sqrt( s * (s - side12) * (s - side23) * (s - side31) );
}
public boolean isIsoceles() {
return side12 == side23 ||
side23 == side31 ||
side31 == side12;
}
public boolean isEquilateral() {
return side12 == side23 && side23 == side31;
}
public boolean isRight() {
return Math.pow(side12,2) + Math.pow(side23,2) == Math.pow(side31,2) ||
Math.pow(side23,2) + Math.pow(side31,2) == Math.pow(side12,2) ||
Math.pow(side31,2) + Math.pow(side12,2) == Math.pow(side23,2);
}
public String toString() {
return "a Triangle with vertices at " +
p1 + ", " + p2 + ", and " + p3 +
" and sides of lengths " +
side12 + ", " + side23 + ", and " + side31;
}
}
|
public class Main {
public static void main(String[] args) {
XYPoint a = new XYPoint(1,-1);
XYPoint b = new XYPoint(-1,5);
XYPoint c = new XYPoint(-3,1);
Triangle t = new Triangle(a,b,c);
System.out.println(t);
System.out.println("perimeter = " + t.getPerimeter());
System.out.println("area = " + t.getArea());
System.out.println("is this a right triangle? " + t.isRight());
System.out.println("is this an isoceles? " + t.isIsoceles());
System.out.println("is this an equilateral triangle? " + t.isEquilateral());
Triangle s = t.copy();
t = null;
System.out.println(t);
System.out.println(s);
}
}
|
a Triangle with vertices at (1,-1), (-1,5), and (-3,1) and sides of lengths 6.324555320336759, 4.47213595499958, and 4.47213595499958 perimeter = 15.26882723033592 area = 10.000000000000005 is this a right triangle? true is this an isoceles? true is this an equilateral triangle? false null a Triangle with vertices at (1,-1), (-1,5), and (-3,1) and sides of lengths 6.324555320336759, 4.47213595499958, and 4.47213595499958 |