# 三角形求周长和面积完整的解决方案,该怎么处理(4)

www.MyException.Cn  网友分享于：2013-03-31  浏览：39次

------解决方案--------------------

------解决方案--------------------

------解决方案--------------------

------解决方案--------------------

------解决方案--------------------

------解决方案--------------------

------解决方案--------------------
1.构造方法3，4，5的参数都为3个，我采用把角的参数为float， 不知有什么缺陷。

Java code
```
class Triangle {
public static Triangle sideSideSide(double s, double s, double s) {..}
public static Triangle sideAngleSide(double s, double a, double s) {...}
public static Triangle angleSideAngle(double a, double s, double a) {...}
}

//example
Triangle one = Triangle.sideSideSide(3,4,5);
------解决方案--------------------探讨1.构造方法3，4，5的参数都为3个，我采用把角的参数为float， 不知有什么缺陷。缺陷大大的。建议用静态方法取代构造函数，参见《effective java》第一条。比如，可以这样写：Java codeclassTriangle {publicstaticTriangle sideSideSide(doubles,doubles,doubles) {..}publicstaticTriangle sideAngleSide(doubles,doublea,doubles) {...}publicstaticTriangle angleSideAngle(doublea,doubles,doublea) {...}}//exam…
------解决方案--------------------*/    public class Triangle {      // 正三角形      public Triangle(double side) {          this.side1 = side;          this.side2 = side;          this.side3 = side;          this.angle1 = Math.PI / 3;          this.angle2 = Math.PI / 3;          this.angle3 = Math.PI / 3;          this.perimeter = 3 * side;    // L = 3*a          this.area = Math.pow(side1, 2) * Math.sin(Math.PI / 3);    //s = a*a*sin(π/3)      }      // 已知两直角边的直角三角形      public Triangle(double side1, double side2) {          this.side1 = side1;          this.side2 = side2;          this.side3 = Math.sqrt(side1 * side1 + side2 + side2);          this.angle1 = Math.atan(side1 / side2);          this.angle2 = Math.PI / 2 - this.angle1;          this.angle3 = Math.PI / 2;          this.perimeter = this.side1 + this.side2 + this.side3;          this.area = this.side1 * this.side2;      }        public Triangle(double side1, double side2, double side3) {          this.side1 = side1;          this.side2 = side2;          this.side3 = side3;          this.angle1 = Math.acos((this.side2 * this.side2                  + this.side3 + this.side3 - this.side1 * this.side1)                  / 2*  this.side2 * this.side3);    //余弦定理          this.angle2 = Math.acos((this.side1 * this.side1                  + this.side3 + this.side3 - this.side2 * this.side1)                  / 2*  this.side1 * this.side3);    //余弦定理          this.angle3 = Math.PI - this.side1 - this.side2;          this.perimeter = this.side1 + this.side2 + this.side3; ```