目录
1、实现getArea,getPerimeter
//method 1
public double getArea() {
double cosA = (side1 * side1 + side2 * side2 - side3 * side3) /
(2.0 * side1 * side2);
return 0.5 * side1 * side2 * Math.sin(Math.acos(cosA));
}
//method 2
public double getArea1() {
double s = (side1 + side2 + side3) / 2;
return Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
}
2、UML
//-side1:double
//-side2:double
//-side3:double
//-------------
//#NewTriangle()
//#NewTriangle(double:side1,double:side2,double:side3)
//#NewTriangle(double:side1,double:side2,double:side3,String:color,Boolean:filled)
//+getSide1(double:side1):void
//+getSide2(double:side2):void
//+getSide3(double:side3):void
//不设置set方法,side不进行改动
//+toString():String
//+getArea():double
//+getPerimeter():double
3、示例
public class NewTriangle extends GeometricObject {
private double side1, side2, side3;
protected NewTriangle() {
}
protected NewTriangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
protected NewTriangle(double side1, double side2, double side3, String color, Boolean filled) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
setColor(color);
setFilled(filled);
}
double getSide1() {
return side1;
}
double getSide2() {
return side2;
}
double getSide3() {
return side3;
}
@Override
public String toString() {
return super.toString() + "\nthe side1:" + side1 +
" the side2:" + side2 + " the side3:" + side3;
}
public double getArea() {
double cosA = (side1 * side1 + side2 * side2 - side3 * side3) /
(2.0 * side1 * side2);
return 0.5 * side1 * side2 * Math.sin(Math.acos(cosA));
}
public double getArea1() {
double s = (side1 + side2 + side3) / 2;
return Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
}
public double getPerimeter() {
return side1 + side2 + side3;
}
}