java矩形类坐标_根据 矩形坐标求宽度、高度

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

//初学者,当练习做

// Test.java

//点

class Spot

{

private int x,y;

Spot()

{

x = 0;

y = 0;

}

Spot(int x0,int y0)

{

x = x0;

y = y0;

}

public int deltaX(Spot s)

{

return (this.x - s.x);

}

public int deltaY(Spot s)

{

return (this.y - s.y);

}

}

//矩形

//upLeft表示矩形左上角的点,downRight表示矩形右下角的点

//坐标系是数学里的,x正方向向右,y正方向向上

class MyRectangle

{

private Spot upLeft,downRight;

MyRectangle()

{

upLeft = new Spot();

downRight = new Spot();

}

MyRectangle(int x1,int y1,int x2,int y2)

{

upLeft = new Spot(x1,y1);

downRight = new Spot(x2,y2);

}

public int getHeight()

{

return upLeft.deltaY(downRight);

}

public int getWidth()

{

return downRight.deltaX(upLeft);

}

}

//主类,测试

public class Test

{

public static void main(String[] args)

{

System.out.println("^_^");

MyRectangle myRec = new MyRectangle(1,5,4,1);

System.out.println("width:" + myRec.getWidth());

System.out.println("height:" + myRec.getHeight());

}

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值