1.先创建一个Point类,然后定义Trianglele类。在Trianglele类中定义三个Point的实体来表示一个三角形的三个点,再定义构造方法对这三个点进行初始化,然后定义两个方法求三角形的周长、面积。定义一个测试类,在main()中创建一个对象,求给定三点的三角形的周长、面积。
Point.class
public class Point {
int x;
int y;
Point(){
}
Point(int a,int b){
x=a;
y=b;
}
public static double getInstance(Point p1, Point p2) { return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y- p2.y, 2));
}
}
Triangle.class
public class Triangle {
Point n1,n2,n3;
boolean isTriangle;
public Triangle(Point n1,Point n2,Point n3){
this.n1=n1;
this.n2=n2;
this.n3=n3;
double sideA=Point.getInstance(n1,n2);
double sideB=Point.getInstance(n2,n3);
double sideC=Point.getInstance(n1,n3);
if(sideA+sideB>sideC&&sideB+sideC>sideA){
isTriangle=true;
}
else{
isTriangle=false;
}
}
public void getPerimeter(){
if(isTriangle){
double sideA=Point.getInstance(n1,n2);
double sideB=Point.getInstance(n2,n3);