Search code examples
javaconstructorrectangles

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


Rectangle is formed using on this 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())
        );
    }

I already have class for point it should work correctly i mostly add it for understanding.

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

So i stuck in second class for rectangle itself. First of all i kinda have hard time forming consructor that will form rectangle itself. And also on filling methods in rectangle class, as i have hard time in uderstanding what they should return becouse i have little experience in Java and coding.

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

    public Point getBottomRight() {
        Point point = new Point();
        return point;
    }

    public int getWidth() {
        int x = 0;
        return x;
    }


    public int getHeight() {
        int y = 0;
        return y;
    }
}

Solution

  • In getTopLeft() and getBottomRight() you could create a point with center coordinates and then moveRel(int, int) it by width and height divided by 2, as you center point is in the middle of the both.

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

    Constructor should write given values to the fields of the class. Also consider using decimal type for the coordinates, only even height and width would be divisible by 2.