I need constructor that will create rectangle using coordinates of the centers, high (X axis) and width (y axis)

huangapple go评论63阅读模式
英文:

I need constructor that will create rectangle using coordinates of the centers, high (X axis) and width (y axis)

问题

以下是已翻译的内容:

所以我的代码由3个部分组成,包括2个类和一个测试。

这是测试的代码:

@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 类运行正常,我只是为了清晰起见添加了它。

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;
    }
}

这是矩形本身的类,其中包括构造函数和附加方法。

public class Rectangle {
    public int width = 0;
    public int height = 0;
    public Point center;

    public Rectangle(Point center, int width, int height) {
        int x = 0;
        int y = 0;
        width=x;
        height=y;
    }

    public Point getTopLeft() {
        Point point = new Point(center.getX(), center.getY());
        point.moveRel(- width / 2, height / 2);
        return point;
    }

    public Point getBottomRight() {
        Point point = new Point(center.getX(), center.getY());
        point.moveRel(width / 2, - height / 2);
        return point;
    }

    public int getWidth() {

        return width;
    }


    public int getHeight() {

        return height;
    }
}

主要问题是此代码在测试中仅返回零,我猜问题可能在矩形类的构造函数或附加方法中。

英文:

So i code consist of 3 parts 2 classes and test.
This is code for test

@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())
);
}

Class Point works fine, and i am ading it just for clarity

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;
}
}

This is class for Rectangle itself, and it includes both constructor and aditional methods.

public class Rectangle {
public int width = 0;
public int height = 0;
public Point center;
public Rectangle(Point center, int width, int height) {
int x = 0;
int y = 0;
width=x;
height=y;
}
public Point getTopLeft() {
Point point = new Point(center.getX(), center.getY());
point.moveRel(- width / 2, height / 2);
return point;
}
public Point getBottomRight() {
Point point = new Point(center.getX(), center.getY());
point.moveRel(width / 2, - height / 2);
return point;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}

Main problem is that this code return only zeroes in tests, i guess problem is in rectangle class in constructor, or aditionalmethods.

答案1

得分: 1

你的Rectangle类的构造函数总是将宽度和高度设置为0。我认为构造函数应该如下所示...

public Rectangle(Point center, int width, int height) {
    int x = 0;
    int y = 0;
    this.width = width;
    this.height = height;
    this.center = center;
}
英文:

Your constructor for Rectangle is always setting width and height to 0. I think the constructor should look something like...

public Rectangle(Point center, int width, int height) {
int x = 0;
int y = 0;
this.width=width;
this.height=height;
this.center=center;
}

huangapple
  • 本文由 发表于 2020年8月28日 02:17:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63622022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定