weixin_43484870 2018-10-22 02:53 采纳率: 100%
浏览 7124
已采纳

定义一个矩形(Rectangle)类,该类代表了一个矩形。可以定义不同的矩形,并对矩形 进行如下运算:

1、移动矩形
2、判断一个点是否在矩形内部
3、求两个矩形合并后的矩形,通过函数返回值返回代表合并后矩形的一个新建立的矩形
对象
4、求两个矩形交集,通过函数返回值返回代表两个矩形交集的一个新建立的矩形对象

同时创建一个包含 main 函数的测试类(TestRectangle)去测试矩形类,通过创建不同的 矩形对象,进行运算并在屏幕上打印出结果

  • 写回答

2条回答

  • threenewbee 2018-10-22 06:31
    关注
    class Untitled {
        public static void main(String[] args) {
            Rectangle rect1 = new Rectangle(10, 10, 20, 10);
            System.out.println(rect1);
            rect1.move(15, 30);
            System.out.println(rect1);
            Rectangle rect2 = new Rectangle(5, 10, 15, 10);
            System.out.println(rect2);
            Rectangle rect3 = rect1.combine(rect2);
            System.out.println(rect3);
            System.out.println(rect3.isInside(30, 10));
            System.out.println(rect3.isInside(100, 100));
            System.out.println(rect1.intersectsWith(rect2.move(20, 30)));
        }
    }
    
    class Rectangle
    {
        public int top;
        public int left;
        public int width;
        public int height;
        public Rectangle(int t, int l, int w, int h)
        {
            this.top = t;
            this.left = l;
            this.width = w;
            this.height = h;
        }
    
        public Rectangle move(int x, int y)
        {
            top = x;
            left = y;
            return this;
        }
    
        public Boolean isInside(int x, int y)
        {
            return x > left && x < (left + width) && y > top && y < (top + height);
        }
    
        public Rectangle combine(Rectangle rect)
        {
            int x = left > rect.left ? rect.left : left;
            int y = top > rect.top ? rect.top : top;
            int x2 = left + width > rect.left + rect.width ? left + width : rect.left + rect.width;
            int y2 = top + height > rect.top + rect.height ? top + height : rect.top + rect.height;
            return new Rectangle(x, y, x2 - x, y2 - y);
        }
    
        public Rectangle intersectsWith(Rectangle rect)
        {
            int x = left > rect.left ? left : rect.left;
            int y = top > rect.top ? top : rect.top;
            int x2 = left + width > rect.left + rect.width ? rect.left + rect.width : left + width;
            int y2 = top + height > rect.top + rect.height ? rect.top + rect.height : top + height;
            if (x2 - x <= 0 || y2 - y <= 0) return null;
            return new Rectangle(x, y, x2 - x, y2 - y);
        }
    
        @Override
        public String toString()
        {
            return "top = " + top +
                " left = " + left +
                " width = " + width +
                " height = " + height + "\n";
        }   
    
    }
    

    top = 10 left = 10 width = 20 height = 10
    top = 15 left = 30 width = 20 height = 10
    top = 5 left = 10 width = 15 height = 10
    top = 10 left = 5 width = 40 height = 20
    false
    false
    top = 30 left = 20 width = 15 height = 5

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题