定义一个Triangle类表示三角形,其中三个double型变量a,b,c表示三边长。为该类定义两个构造方法,默认构造方法设置三角形的三边长都为0.0;带三个参数的构造方法通过传递三个参数创建三角形对象。定义三角形的面积求法area(),面积计算公式为area=Math.sqrt(s*(s-a)*(s-b)*(s-c)),其中s=(a+b+c)/2,编写另一个程序测试这个三角形的方法
public class Triangle {
double a;
double b;
double c;
public Triangle(){}
//Triangle triangle1 = new Triangle();
public Triangle(double a,double b, double c){
this.a = a;
this.b = b;
this.c = c;
}
public static void area(){
//默认构造器下:
Triangle triangle1 = new Triangle();
System.out.println("无参构造器a,b,c分别为" + triangle1.a + triangle1.b + triangle1.c);
//带参数构造器下:
Triangle triangle2 = new Triangle(3.0,4.0,5.0);
System.out.println("无参构造器a,b,c分别为" + triangle2.a + triangle2.b + triangle2.c);
double s =( triangle2.a+triangle2.b+triangle2.c)/2;
double area;
area = Math.sqrt(s*(s-triangle2.a)*(s-triangle2.b)*(s-triangle2.c));
System.out.println("三角形2的面积是"+area);
}
public static void main(String[] args){
//System.out.println("默认构造器下");
area();
}
}