设计一个表示三角形的类sjx(成员包含三条边),在主程序中创建对象t,按如下格式输出三角形的周长和面积
T(3,4,5),Area=6
T(3,4,5),Perimeter
------解决思路----------------------
public class Triangle {
private double a;
private double b;
private double c;
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
/**
* 计算三角形的周长
*
* @return
*/
public double getLength() {
return (a + b + c);
}
/**
* 计算三角形的面积
*
* @return
*/
public double getSpace() {
double temp = getLength() / 2;
return java.lang.Math.sqrt(temp * (temp - a) * (temp - b) * (temp - c));
}
public static void main(String[] args) {
Triangle t = new Triangle(3, 4, 5);
System.out.println("周长: " + t.getLength());
System.out.println("面积: " + t.getSpace());
}
}