一道关于设计Rectangle的矩形类的题目

阿华tian1 2019-04-14 07:03:39
如图所示,这是一道简单题,求大神指教
...全文
762 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿华tian1 2019-04-16
  • 打赏
  • 举报
回复
引用 3 楼 qps2009 的回复:
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
* * * * * * * * 
*             * 
*             * 
* * * * * * * * 
代码清晰明了,受徒儿一拜👍🏻
qps2009 2019-04-15
  • 打赏
  • 举报
回复
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
* * * * * * * * 
*             * 
*             * 
* * * * * * * * 
流泪熊猫头 2019-04-14
  • 打赏
  • 举报
回复
题目已经叙述得很清楚了。按步骤来写。
巴顿.com 2019-04-14
  • 打赏
  • 举报
回复
书上应该是有参考的,建议初学自己码

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧