设计一个类Triangle,它有三个double型属性;

1个回答

  • class Triangle{

    public double a;

    public double b;

    public double c;

    public Triangle(){

    a=0.0;

    b=0.0;

    c=0.0;

    }

    public Triangle(double a,double b,double c){

    this.a=a;

    this.b=b;

    this.c=c;

    }

    private boolean isTrue(){

    if(a+b>c&&a+c>b&&b+c>a)return true;

    else return false;

    }

    public double getPerimeter(){

    if(this.isTrue())return a+b+c;

    else return -1;

    }

    public double getArea(){

    double s=(a+b+c)/2;

    if(this.isTrue())return Math.sqrt(s*(s-a)*(s-b)*(s-c));

    else return -1;

    }

    public static void main(String[] args){

    Trangle t=new Trangle();

    System.out.println(t.getPerimeter());

    System.out.println(t.getArea());

    Trangle q=new Trangle(1.2,1.2,1.2);

    System.out.println(q.getPerimeter());

    System.out.println(q.getArea());

    }

    }