class Trangle
{
double sideA,sideB,sideC,area,length;
boolean boo;
public Trangle(double a,double b,double c)
{
//參數a,b,c分別賦值給sideA,sideB,sideC
this.sideA=a;
this.sideB=b;
this.sideC=c;
if(sideA+sideB>sideC&&sideB+sideC>sideA&&sideA+sideC>sideB) //a,b,c構成三角形的條件表達式
{
boo=true; //給boo賦值。
}
else
{
boo=false;//給boo賦值。
}
}
double getLength()
{
return (this.sideA+this.sideB+this.sideC); //方法體,要求計算出length的值並返回
}
public double getArea()
{
if(boo)
{
double p=(sideA+sideB+sideC)/2.0;
area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC)) ;
return area;
}
else
{
System.out.println("不是一個三角形,不能計算面積");
return 0;
}
}
public void setABC(double a,double b,double c)
{
this.sideA=a;
this.sideB=b;
this.sideC=c;//參數a,b,c分別賦值給sideA,sideB,sideC
if(sideA+sideB>sideC&&sideB+sideC>sideA&&sideA+sideC>sideB) //a,b,c構成三角形的條件表達式
{
boo=true;//給boo賦值。
}
else
{
boo=false; //給boo賦值。
}
}
}
class Lader
{
double above,bottom,height,area;
Lader(double a,double b,double h)
{
//方法體,將參數a,b,c分別賦值給above,bottom,height
this.above=a;
this.bottom=b;
this.height=h;
}
double getArea()
{
//方法體,,要求計算出area返回
area=((this.above+this.bottom)*this.height)/2;
return area;
}
}
class Circle
{
double radius,area;
Circle(double r)
{
this.radius=r; //方法體
}
double getArea()
{
this.area=3.14*radius*radius;
return area;//方法體,要求計算出area返回
}
double getLength()
{
return (2*3.14*radius); //getArea方法體的代碼,要求計算出length返回
}
void setRadius(double newRadius)
{
radius=newRadius;
}
double getRadius()
{
return radius;
}
}
public class AreaAndLength
{
public static void main(String args[])
{
double length,area;
Circle circle=null;
Trangle trangle;
Lader lader;
circle=new Circle(2);//創建對象circle
trangle=new Trangle(3,4,5); //創建對象trangle。
lader=new Lader(1,2,3);//創建對象lader
length=circle.getLength();// circle調用方法返回周長並賦值給length
System.out.println("圓的周長:"+length);
area=circle.getArea();// circle調用方法返回面積並賦值給area
System.out.println("圓的面積:"+area);
length=trangle.getLength();// trangle調用方法返回周長並賦值給length
System.out.println("三角形的周長:"+length);
area=trangle.getArea();// trangle調用方法返回面積並賦值給area
System.out.println("三角形的面積:"+area);
area=lader.getArea();// lader調用方法返回面積並賦值給area
System.out.println("梯形的面積:"+area);
trangle.setABC(12, 34, 1);// trangle調用方法設置三個邊,要求將三個邊修改爲12,34,1。
area=trangle.getArea();// trangle調用方法返回面積並賦值給area
System.out.println("三角形的面積:"+area);
length=trangle.getLength(); // trangle調用方法返回周長並賦值給length
System.out.println("三角形的周長:"+length);
}
}