英文:
I have problem with class that creates rectangle based on a test
问题
好的,以下是您提供的代码的中文翻译:
// 测试矩形的方法,基于给定的点、高度和宽度创建矩形
public void testRectangle1() {
Point center = new Point(20, 30);
Rectangle rect = new Rectangle(center, 20, 20);
assertAll(
() -> assertEquals(10, rect.getTopLeft().getX()),
() -> assertEquals(20, rect.getTopLeft().getY()),
() -> assertEquals(30, rect.getBottomRight().getX()),
() -> assertEquals(40, rect.getBottomRight().getY()),
() -> assertEquals(20, rect.getWidth()),
() -> assertEquals(20, rect.getHeight())
);
}
// Point 类定义
package net.thumbtack.school.figures.v1;
public class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point() {
this(0, 0);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void moveTo(int newX, int newY) {
x = newX;
y = newY;
}
public void moveRel(int dx, int dy) {
x += dx;
y += dy;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
// 创建矩形的类定义
package net.thumbtack.school.figures.v1;
public class Rectangle {
public int width;
public int height;
public Point center;
public int xCenter;
public int yCenter;
private Point point;
// 矩形构造方法
public Rectangle(Point center, int width, int height) {
this.width = width;
this.height = height;
this.center = center;
}
// 获取矩形左上角的点
public Point getTopLeft() {
Point point = getCenter();
point.moveRel(- width / 2, - height / 2);
return point;
}
// 获取矩形右下角的点
public Point getBottomRight() {
Point point = getCenter();
point.moveRel(width / 2, height / 2);
return point;
}
// 获取矩形宽度
public int getWidth() {
return width;
}
// 获取矩形高度
public int getHeight() {
return height;
}
// 获取矩形中心点
public Point getCenter() {
Point center = new Point(xCenter, yCenter);
return center;
}
}
关于您提到的问题,您的构造函数 public Rectangle(Point center, int width, int height)
中似乎存在问题,导致测试返回错误的值。您可能需要仔细检查该构造函数内部的逻辑,确保正确计算矩形的位置和属性。
英文:
Ok so i have rectangle that should be created based on: method point that will crete coordinates of it center and values of hight and width, all value are based on this test
public void testRectangle1() {
Point center = new Point(20, 30);
Rectangle rect = new Rectangle(center, 20, 20);
assertAll(
() -> assertEquals(10, rect.getTopLeft().getX()),
() -> assertEquals(20, rect.getTopLeft().getY()),
() -> assertEquals(30, rect.getBottomRight().getX()),
() -> assertEquals(40, rect.getBottomRight().getY()),
() -> assertEquals(20, rect.getWidth()),
() -> assertEquals(20, rect.getHeight())
);
}
I have prewritten class point that i will add here for overall clarity
package net.thumbtack.school.figures.v1;
public class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point() {
this(0, 0);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void moveTo(int newX, int newY) {
x = newX;
y = newY;
}
public void moveRel(int dx, int dy) {
x += dx;
y += dy;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
And this is class for creating rectangle
package net.thumbtack.school.figures.v1;
public class Rectangle {
public int width;
public int height;
public Point center;
public int xCenter;
public int yCenter;
private Point point;
public Rectangle(Point center, int width, int height) {
this.width=width;
this.height=height;
this.center=center;
}
public Point getTopLeft() {
Point point = getCenter();
point.moveRel(- width / 2, - height / 2);
return point;
}
public Point getBottomRight() {
Point point = getCenter();
point.moveRel(width / 2, height / 2);
return point;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Point getCenter() {
Point center = new Point(xCenter, yCenter);
return center;
}
So problem is in constructor public Rectangle(Point center, int width, int height)
when i run the test it returns wirng values
expected: <10> but was: <-10>
Comparison Failure:
Expected :10
Actual :-10
expected: <20> but was: <-10>
Comparison Failure:
Expected :20
Actual :-10
expected: <30> but was: <10>
Comparison Failure:
Expected :30
Actual :10
expected: <40> but was: <10>
Comparison Failure:
Expected :40
Actual :10
I dont this that the problem is into other methods becouse when i use them in different but simmilar constrictors everything works.
答案1
得分: 0
如maloomeister所提到的,您必须在使用之前初始化xCenter
或yCenter
,否则xCenter
和yCenter
的值将为0。
修改getCenter()
方法:
public Point getCenter() {
Point point = new Point(center.getX(), center.getY());
return point;
}
英文:
As maloomeister mentioned you must init xCenter
or yCenter
before used,or the value of xCenter
and yCenter
is 0.
modify getCenter()
method:
public Point getCenter() {
Point point = new Point(center.getX(), center.getY());
return point;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论