public class Triangle {
private double a;
private double b;
private double c;
public double getA() {
return a;
}
public double getB() {
return b;
}
public double getC() {
return c;
}
public void setBases(double a, double b, double c) {
if (a < 0 || b < 0 || c < 0) {
System.out.println("三角形的边长必须大于等于零!");
return;
}
if (a + b < c || a + c < b || b + c < a) {
System.out.println("您输入的三角形边长有误!");
return;
}
this.a = a;
this.b = b;
this.c = c;
}
//三角形面积
public double area() {
//半周长
double s = perimeter() / 2.0;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
//三角形周长
public double perimeter() {
return a + b + c;
}
//三角形信息
public void getInfo() {
System.out.println("三角形的三边分别是 " + "a=" + a + " b=" + b + " c=" + c + ", 面积是:" + area() + ", 周长是:" + perimeter());
}
}
public class TestTriangle {
public static void main(String[] args) {
Triangle tri = new Triangle();
tri.setBases(4, 3, 5);
tri.getInfo();
}
}