Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我需要的构造器,将创建使用中心坐标,高(X轴)和宽度(y轴)矩形_Java_Rectangles - Fatal编程技术网

Java 我需要的构造器,将创建使用中心坐标,高(X轴)和宽度(y轴)矩形

Java 我需要的构造器,将创建使用中心坐标,高(X轴)和宽度(y轴)矩形,java,rectangles,Java,Rectangles,所以我的代码包括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())

所以我的代码包括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())
        );
    }

课堂点评效果很好,我正在阅读它只是为了清楚

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

主要问题是,这段代码在测试中只返回零,我猜问题出在构造函数中的矩形类中,或者是在aditionalmethods中。

矩形的构造函数总是将宽度和高度设置为0。我认为构造器应该看起来像

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

谢谢但我现在遇到了不同的问题,当我运行test时,它会在test
()->assertEquals(20,rect.getTopLeft().getY())上返回错误的值,
其中它返回40而不是20,test
()->assertEquals(40,rect.getBottomRight().getY()),
返回20而不是40。看起来y值是颠倒的,但我不明白为什么。