现欲构造一个三角形求周长和面积完整的解决方案,下面是代码,有5个构造方法:
1.正三角形,构造参数1个边长;
2.直角三角形,构造参数2个直角边;
3.已知三边的任意三角形,构造参数3个边;
4.已知两相连两边及其夹角,构造参数2个边和1个夹角;
5.已知两角及其夹边, 构造参数2个角和1个夹边;
问题是:
1.构造方法3,4,5的参数都为3个,我采用把角的参数为float, 不知有什么缺陷。
2.有没有其他构造方法没写?
3.如果要写set方法, 如 setSides, 那么其他变量,角度、周长、面积也会跟着变,那该怎么处理呢?
4.是不是我的这个类根本的思路就不对?
- Java code
/*
* Triangle.java
* @author Wenzee Yann, wenzee@gmail.com
* @version 0.1000
* @since 2009.05.04
*/
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;
}
/* 已知三边的任意三角形
* 余弦定理:a2=b2+c2-2bccosA, b2=a2+c2-2accosB, c2=a2+b2-2abcosC
* 三角形一边的平方等于另两边的平方和再减去这两边与夹角余弦的乘积的2倍
*/
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;
this.area = this.side1 * this.side2 * Math.cos(this.angle3);
}
/* 已知两相连两边及其夹角
* 正弦定理: a/sinA=b/sinB=c/sinC=2R, 其中R是三角形外接圆半径。
* 余弦定理:a2=b2+c2-2bccosA, b2=a2+c2-2accosB, c2=a2+b2-2abcosC
*/
public Triangle(double side1, double side2, float angle3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = Math.sqrt(this.side1 * this.side1
+ this.side2 * this.side2
- 2 * side1 * side2 * Math.cos(angle3)); //余弦定理
this.angle3 = angle3;
this.angle1 = Math.asin(this.side1 * Math.sin(this.angle3)
/ this.side3); //正弦定理
this.angle2 = Math.PI /2 - this.angle1 - this.angle2;
this.perimeter = this.side1 + this.side2 + this.side3;
this.area = this.side1 * this.side2 * Math.cos(this.angle3);
}
/* 已知两角及其夹边
* 正弦定理: a/sinA=b/sinB=c/sinC=2R, 其中R是三角形外接圆半径。
*/
public Triangle(double side1, float angle2, float angle3) {
this.angle1 = Math.PI - angle2 -angle3;
this.angle2 = angle2;
this.angle3 = angle3;
this.side1 = side1;
this.side2 = this.side1 * Math.sin(this.angle2)
/ Math.sin(this.angle1); //正弦定理
this.side3 = this.side1 * Math.sin(this.angle3)
/ Math.sin(this.angle1); //正弦定理
this.perimeter = this.side1 + this.side2 + this.side3;
this.area = this.side1 * this.side2 * Math.cos(this.angle3);
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
public double getAngle1() {
return angle1;
}
public double getAngle2() {
return angle2;
}