基于中心坐标构造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/2andcenterY - height/2 - Bottom-right point: (30, 40) → calculated by
centerX + width/2andcenterY + height/2 - Width and height to both equal 20
What's wrong with your existing Rectangle code?
- The
centermember variable is never initialized in the constructor. WhengetTopLeft()orgetBottomRight()tries to callcenter.getX(), it throws aNullPointerException. - You have redundant fields:
xCenter/yCenterandcenterstore the same coordinate data, leading to unused variables and potential inconsistency.
Fix Option 1: Simplify with the center field (Recommended)
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
相关产品推荐
相关产品推荐

