AlexKangaroo AlexKangaroo - 3 years ago 170
Java Question

How to handle multiple constructors?

Hello I have problems with assignment where I need to create 3 constructs in once class. That initialize 2 coordinates for 2 corners in a rectangle. The error Eclipse is giving: "Duplicate method" and "Multiple markers on this line" errors with the Constructs.

public class Rectangle {

private double lowleftx;
private double lowlefty;

private double uprightx;
private double uprighty;


public Rectangle() {
this.lowleftx = 0;
this.lowlefty = 0;

this.uprightx = 1;
this.uprighty = 1;
}

public Rectangle(uprightx, uprighty) {

this.lowleftx = 0;
this.lowlefty = 0;
}
public Rectangle(uprightx, uprighty, lowleftx, lowlefty) {

this.lowleftx = lowleftx;
this.lowlefty = lowlefty;
this.uprightx = uprightx;
this.uprighty = uprighty;
}


public double getLowleftx() {
return lowleftx;
}

public void setLowleftx(double lowleftx) {
this.lowleftx = lowleftx;
}

public double getLowlefty() {
return lowlefty;
}

public void setLowlefty(double lowlefty) {
this.lowlefty = lowlefty;
}

public double getUprightx() {
return uprightx;
}

public void setUprightx(double uprightx) {
this.uprightx = uprightx;
}

public double getUprighty() {
return uprighty;
}

public void setUprighty(double uprighty) {
this.uprighty = uprighty;
}


}

Answer Source

As said in the comments, you forgot to add the type of the parameters :

public Rectangle(double uprightx, double uprighty...)

You can optimize your code by calling the constructor with all parameters from other constructors :

public class Rectangle {

    private double lowLeftX;
    private double lowLeftY;
    private double upRightX;
    private double upRightY;

    public Rectangle(double lowLeftX, double lowLeftY, double upRightX, double upRightY) {
        this.lowLeftX = lowLeftX;
        this.lowLeftY = lowLeftY;
        this.upRightX = upRightX;
        this.upRightY = upRightY;
    }

    public Rectangle(double upRightX, double upRightY) {
        this(0, 0, upRightX, upRightY); // = Rectangle(0, 0, upRightX, upRightY)
    }

    public Rectangle() {
        this(0, 0, 1, 1); // = Rectangle(0, 0, 1, 1), or Rectangle(1, 1)
    }

    // ...
}

You can also create a class to represent a "point" (a coordinate with a X value and Y value) and use it in your Rectangle class :

// Point.java
public class Point {

    private final double x;
    private final double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    // ...
}

// Rectangle.java
public class Rectangle {

    private final Point lowLeft;
    private final Point upRight;

    public Rectangle(final Point lowLeft, final Point upRight) {
        this.lowLeft = lowLeft;
        this.upRight = upRight;
    }

    public Rectangle(final Point upRight) {
        this(new Point(0, 0), upRight);
    }

    public Rectangle() {
        this(new Point(1, 1));
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download