用Java编写XYRectangle类
我有以下课程要写: 编写一个名为XYU LastName的类,其中LastName替换为姓氏。。XYU LastName类应该有以下字段:
一个名为TopLeft的XYPoint。它存储矩形左上角的位置
双重命名长度。这将存储矩形的长度
双代号宽度。这将存储矩形的宽度
XYRectangle类应该有以下方法:
随机确定矩形左上角的无参数构造函数。x和y的值应介于-10和10之间。此外,它还会为矩形选择一个随机的宽度和长度,其值介于5和10之间
一个三参数构造函数,左上角有一个XYPoint、一个长度和一个宽度
获取长度、宽度、左上角、右上角、左下角和右下角的方法
长度、宽度和左上角的设置方法
一个名为isInside的布尔方法,它获取一个XYPoint并确定它是否在该矩形内
一个名为reflectX的方法,它返回一个已在x轴上反射的矩形
一个名为reflectY的方法,该方法返回已在y轴上反射的矩形
这是我目前掌握的代码:
public class XYRectangle {
private XYPoint topLeft;
private double length;
private double width;
public XYRectangle() {
Random rnd = new Random();
int x = (rnd.nextInt(21) - 10);
int y = (rnd.nextInt(21) -10);
XYPoint topLeft = new XYPoint(x, y);
int width = (rnd.nextInt(5) + 5);
int height = (rnd.nextInt(5) + 5);
}
public XYRectangle(XYPoint topLeft, double length, double width) {
this.topLeft = topLeft;
this.length = length;
this.width = width;
}
public double getLength() { return this.length; }
public void setLength(double length) { this.length = length; }
public double getWidth() { return this.width; }
public void setWidth(double width) { this.width = width; }
public XYPoint getTopLeft() { return this.topLeft; }
public void setTopLeft(XYPoint topLeft) { this.topLeft = topLeft; }
我在使用topRight、bottomLeft和bottomRight get方法和reflect方法时遇到了问题。我甚至不确定到目前为止我写的代码是否是write。谁能帮我一下,告诉我该怎么做,如果我做错了什么
# 1 楼答案
你没有关于右上角、左下角和右下角的信息,但有左上角和宽度、长度,它完全定义了其他点:
您可以决定在构造对象时存储这些信息,或者在每次调用get方法时计算这些信息
关于空构造函数,当应该调用它时,您将其称为“角落”:
通常,当我们重写构造函数时,我们会这样调用基本构造函数:
我希望你能自己想出思考的方法