2.封裝一類梯形對象Ladder,該類對象具有上底、下底和高的屬性,具有初始化梯形的功能、修改上底、下底和高的功能、求周長的功能、求面積的功能。
public class TestLadder {
private double top;
private double bottom;
private double height;
TestLadder(double top,double bottom,double height) {
this.top=top;
this.bottom=bottom;
this.height=height;
}
TestLadder(){
this.top =1;
this.bottom=2;
this.height=3;
}
public void set_Ladder(double top,double bottom,double height){
this.top=top;
this.bottom=bottom;
this.height=height;
}
public double get_Perimeter()
{
return (this.bottom+this.height+this.top);
}
public double get_Area(){
double s;
s=(this.top+this.bottom)*height/2;
return s;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestLadder t = new TestLadder(2,4,3);
t.get_Area();
t.get_Perimeter();
double x1,x2;
x1=t.get_Area();
x2=t.get_Perimeter();
System.out.println(x1+" "+x2);
}
}