1

I am writing a java project, but am having trouble with it. I am passing new value (0, 0) and (6, 0) in my Horizontal class (subclass), but when I run the program my output is (0, 0) and (4, 4) which are the values passed in in my super class! I do not know how to fix the issue! I would appreciate it if you give me an idea of where I am doing wrong. I need to follow the instructions!

public class Segment implements SegmentInterface {
    // two Points that hold endpoints of the segment
    private Point p1;
    private Point p2;

    // Default constructor that will set the endpoints to new
    // Points with values (0,0) and (4,4)
    public Segment() {
        //this(0, 0, 4, 4);
        this.p1 = new Point(0, 0);
        this.p2 = new Point(4, 4);
    }

    // Parameterized constructor that accepts (int x1, int y1, int x2, int y2)
    // and creates and sets the endpoints
    public Segment(int x1, int y1, int x2, int y2) {

        //this.p1 = new Point(x1, y1);
        //this.p2 = new Point(x2, y2);

        if(x1 == x2 && y1 == y2){
            throw new IllegalArgumentException("Points can't have a lenght of 0!");
        } 

        else{
            this.p1 = new Point(x1, y1);
            this.p2 = new Point(x2, y2);
    }
}

     //A parameterized constructor that accepts (Point p1, Point p2) and sets both
     //the endpoints to a deep copy of the Points that are passed in.
     //Make sure to check to see if both Points are equal.  This would be a
     //theoretical "Segment" of length 0 which is not allowed, so throw a new
     //IllegalArgumentException.
    public Segment(Point p1, Point p2) {

        this.p1 = new Point(p1.getX(), p1.getY());
        this.p2 = new Point(p2.getX(), p2.getY());

            if(p1.equals(p2)){
                throw new IllegalArgumentException("Points cannot have same values!");
            } 
}

    // Copy constructor that accepts a Segment and initializes the data (of the
    // new Segment being created) to be the same as the Segment that was
    // received.
    public Segment(Segment other) {

        this(other.p1, other.p2);

        //if(other.p1 == other.p2){
            //throw new IllegalArgumentException("Undefine!");
        //}

and here is my subclass:

//HorizontalSegment.java
public class HorizontalSegment extends Segment implements
        HorizontalSegmentInterface {

    protected Point p1;
    protected Point p2;

    //A default constructor that will set the endpoints to new
    //Points with values (0,0) and (6,0)
    public HorizontalSegment() {
        this.p1 = new Point(0, 0);
        this.p2 = new Point(6, 0);
    }

    /*A parameterized constructor that accepts (int x1, int y1, int x2, int y2)
     *      and creates and sets the endpoints.  But first, check to see if x1 == x2
     *      and y1== y2.  This would be a theoretical "Segment" of length 0 which
     *      is not allowed, so throw a new IllegalArgumentException like this:
     *          throw new IllegalArgumentException("whatever explanatory String you want");
     *      Also, check to see if y1 != y2.  This would be a line segment that is not
            horizontal, so throw a new IllegalArgumentException if this occurs.*/
    public HorizontalSegment(int x1, int y1, int x2, int y2) {
        super(x1, y1, x2, y2);
        if (x1 == x2 && y1 == y2) {
            throw new IllegalArgumentException("Segment of length 0 is not allowed!");
        }

        else if (y1 != y2) {
            throw new IllegalArgumentException("Line segment that is not horizontal!");
        } 

        else {
            this.p1 = new Point(x1, y1);
            this.p2 = new Point(x2, y2);
        }
    }

    /*A parameterized constructor that accepts (Point p1, Point p2) and sets both
     *      the endpoints to a deep copy of the Points that are passed in.
     *      Make sure to check to see if both Points are equal.  This would be a
     *      theoretical "Segment" of length 0 which is not allowed, so throw a new
     *      IllegalArgumentException.
     *      Also, check to see if the points form a horizontal segment.  If not, throw a
     *      new IllegalArgumentException if this occurs.*/
    public HorizontalSegment(Point p1, Point p2) {
        if (this.p1.getX() == p2.getX() && this.p1.getY() == p2.getY()) {
            throw new IllegalArgumentException(
                    "Segment of length 0 is not allowed!");

        } else if (this.p1.getY() != p2.getY()) {
            throw new IllegalArgumentException(
                    "Line segment that is not horizontal!");
        } 


        else {
            this.p1 = new Point(p1.getX(), p1.getY());
            this.p2 = new Point(p2.getX(), p2.getY());
        }
    }

    public HorizontalSegment(HorizontalSegment hs) {
        //this.p1 = hs.getP1();
        //this.p2 = hs.getP2();
        this(hs.p1, hs.p2);
    }
}
1
  • 1
    Use the Code Sample { } button instead of snippet. Snippets are for runnable web languages.
    – Radiodef
    Nov 30, 2014 at 5:10

1 Answer 1

0

The p1 and p2 Point fields should be protected in the Segment class (not private because then your sub-class cannot access them). Thus you should remove the ones from HorizontalSegment because they add two new instances of Point with the same name as instances from the Segment (but they do not Override those values, they are distinct).

Your code should work as you expect after you make those two changes.

public class HorizontalSegment extends Segment implements
        HorizontalSegmentInterface {
    // protected Point p1;
    // protected Point p2;

And

public class Segment implements SegmentInterface {
    // two Points that hold endpoints of the segment
    protected Point p1; // <-- not private
    protected Point p2;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.