62,614
社区成员
发帖
与我相关
我的任务
分享
public class Rectangle {
private int length;
private int width;
public Rectangle(int length,int width){
this.length=length;
this.width=width;
}
public int getArea(){
return this.length*this.width;
}
public int getPerimeter(){
return this.length*2+this.width*2;
}
public void Draw(){
for(int i=0;i<this.length;i++){
for(int j=0;j<this.width;j++){
if(i==0||i==this.length-1){
System.out.print("* ");
}else{
if(j==0||j==this.width-1){
System.out.print("* ");
}else{
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
测试类
public class TestRectangle {
public static void main(String[] args){
Rectangle rectangle = new Rectangle(4,8);
System.out.println("面积:"+rectangle.getArea());
System.out.println("周长:"+rectangle.getPerimeter());
rectangle.Draw();
}
}
输出
面积:32
周长:24
* * * * * * * *
* *
* *
* * * * * * * *