1.封裝一類三角形對象Triangle,該類對象具有三條邊的屬性,具有初始化三角形的功能、修改邊長的功能、判斷三條邊能否構成三角形的功能、求周長的功能、求面積的功能。
public class TestTriangle {
private double a,b,c;
TestTriangle(double a,double b,double c){
this.a = a;
this.b = b;
this.c = c;
}
TestTriangle(){
this.a = 6;
this.b = 8;
this.c = 10;
}
public void set_edge(double a,double b,double c){
this.a = a;
this.b = b;
this.c = c;
}
public double get_Area(double a,double b,double c){
return (this.a+this.b+this.c);
}
public double get_Perimer(double a,double b,double c){
double s;
s=(this.a+this.b+this.c)/2;
return(Math.sqrt(s*(s-this.a)*(s-this.b)*(s-this.c)));
}
public boolean is_Triangle(double a,double b,double c)
{
if(this.a+this.b>this.c&&this.a+this.c>this.b&&this.c+this.b>this.a)
{
System.out.println("是三角形");
return true;
}
else{
System.out.println("不是三角形");
return false;
}
}
public static void main(String[] args) {
TestTriangle t = new TestTriangle();
t.get_Area(3,4,5);
t.is_Triangle(3,4,5);
t.get_Perimer(3,4,5);
double c,x;
c=t.get_Area(3, 4, 5);
x=t.get_Perimer(3,4,5);
System.out.println("面積爲 "+c+" 周長爲 "+x);
}
}