试定义Triangle类,使其具有以下软件接口:
class Triangle {
   public Triangle();   //将三边长设定为0
   public Triangle(double a, double b, double c);
   public Triangle(Triangle tri);//用tri的状态设置当前三角形的状态
   public double getPerimeter(); //返回当前三角形的周长
   public double getArea();  //返回当前三角形的面积
   public String toString();//以格式"三角形(a,b,c)"返回当前三角形的字符串表示
}
并在该类的main函数中编写测试以上各成员函数的代码。

解决方案 »

  1.   

    沙发么,顺便写写。
    package Test;public class Triangle {
        private double a;
        private double b;
        private double c;
        private Triangle tri;    public Triangle() {
    super();
    this.a = 0D;
    this.b = 0D;
    this.c = 0D;
        }    public Triangle(double a, double b, double c) {
    super();
    this.a = a;
    this.b = b;
    this.c = c;
        }    public Triangle(Triangle tri) {
    this.tri = tri;
        }    public double getPerimeter() {
    return this.a + this.b + this.c;
        }    public double getArea() {
    double s = (this.a + this.b + this.c) / 2.0D;
    return Math.sqrt(s * (s - this.a) * (s - this.b) * (s - this.c));
        }    public String toString() {
    return "a: " + String.valueOf(this.a) + "b: " + String.valueOf(this.b)
    + "c: " + String.valueOf(this.b);
        }
    }
      

  2.   

    谢谢,通过编译和测试了。但是我想知道
    public Triangle(Triangle tri) {
        this.tri = tri;}
    这个构造函数是干嘛用的,好像没有什么意义啊?thanks~~~