You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于中心坐标构造Rectangle类的构造器问题排查及修复

Fixing the Rectangle Constructor to Pass the JUnit Test

Let's break down why your current Rectangle class isn't passing the test and fix it up step by step.

First, understand the test expectations

The test creates a Rectangle(20, 30, 20, 20) and expects:

  • Top-left point: (10, 20) → calculated by centerX - width/2 and centerY - height/2
  • Bottom-right point: (30, 40) → calculated by centerX + width/2 and centerY + height/2
  • Width and height to both equal 20

What's wrong with your existing Rectangle code?

  • The center member variable is never initialized in the constructor. When getTopLeft() or getBottomRight() tries to call center.getX(), it throws a NullPointerException.
  • You have redundant fields: xCenter/yCenter and center store the same coordinate data, leading to unused variables and potential inconsistency.

Remove the redundant xCenter/yCenter fields and properly initialize the center Point in the constructor:

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

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

    public Point getCenter() {
        // Return a copy to prevent external code from modifying the internal center
        return new Point(center.getX(), center.getY());
    }

    public Rectangle(int xCenter, int yCenter, int width, int height) {
        this.center = new Point(xCenter, yCenter); // Initialize the center Point
        this.width = width;
        this.height = height;
    }
}

Fix Option 2: Keep xCenter/yCenter and remove unused center field

If you prefer to keep separate coordinate fields, update the getTopLeft() and getBottomRight() methods to use them instead of the uninitialized center:

public class Rectangle {
    public int width = 0;
    public int height = 0;
    public int xCenter;
    public int yCenter;

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

    public Point getBottomRight() {
        Point point = new Point(xCenter, yCenter);
        point.moveRel(width / 2, height / 2);
        return point;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public Point getCenter() {
        return new Point(xCenter, yCenter);
    }

    public Rectangle(int xCenter, int yCenter, int width, int height) {
        this.xCenter = xCenter;
        this.yCenter = yCenter;
        this.width = width;
        this.height = height;
    }
}

Why these fixes work

Both options correctly calculate the required points using the center coordinates and dimensions passed to the constructor. When you create new Rectangle(20, 30, 20, 20):

  • Top-left: 20 - 20/2 = 10 (x), 30 - 20/2 = 20 (y)
  • Bottom-right: 20 + 20/2 = 30 (x), 30 + 20/2 = 40 (y)
  • Width and height remain 20, matching all test assertions.

内容的提问来源于stack exchange,提问作者user14041112

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 00:27:34