1

I'm working on a program that first takes an even array of doubles and sequentially creates a second array of (x, y) coordinates. Then, an invoking PointArray has to be compared to the argument PointArray by comparing the x-coordinates and y-coordinates of each point in the array. I know conceptually that I'll have to sort each array and compare each point using the indeces and the defined equals method, but I don't know how to refer to a point of the invoking class-type array when it is not given as a parameter.

private double x;
private double y;

public Point(double x_coord, double y_coord)
{
    x = x_coord;
    y = y_coord;
}

public boolean equals(Point anotherPoint)
{
    if(x == anotherPoint.x && y == anotherPoint.y)
    {
        return true;
    }
    return false;
}
int count;
private Point[] points = new Point[count];

public PointArray(double[] doubleArray)
{
    count = (doubleArray.length) / 2;
    if(doubleArray.length % 2 == 0)
    {
        count = (doubleArray.length) / 2;
        for(int i = 0, j = 0; i < count; i++, j += 2)
        {
            double x = doubleArray[j];
            double y = doubleArray[j + 1];
            points[i] = new Point(x, y);
        }
    }
    else
    {
        System.out.println("Error: The given array must be even.");
    }
}
public boolean equals(PointArray anotherPointArray)
{
    double x = 0;
    double y = 0;
    double xAnother = 0;
    double yAnother = 0;
    Point newPoint = new Point(x, y);
    Point newAnotherPoint = new Point(xAnother, yAnother);
    anotherPointArray.sort();
    anotherPointArray.newPoint;
    for(int i = 0; i < points.length; i++)
    {
        for(int j = 0; i < anotherPointArray.length; j++)
        {
            if(newPoint.equals(newAnotherPoint))
            {
                return true;
            }
        }
    }
    return false;
}

EDIT: To clarify, my problem is that I don't know how to set up the Point equals method using the PointArray objects.

  • do you mean you don't know how to get to your `points` array inside the `equals` function?? – J.Baoby Dec 02 '16 at 03:17
  • @J.Baoby Which equals are you asking about? If you mean the equals(PointArray anotherPointArray), then yes, I am having trouble with that. But what I was asking about was how to declare the two arrays. For example, I think "testPointArray1.equals(testPointArray2)" would be how I'd call the method, in which case anotherPointArray is the testPointArray2. However, what's tesrPointArray1 in the context of the boolean? –  Dec 02 '16 at 03:26

1 Answers1

1

When you call a method on an object like:

something.myMethod(argObject);

inside the myMethod you can then refer to something (the object the method is called on) as the this-object. I hope this tuto demonstrates it better than words do.

J.Baoby
  • 2,167
  • 2
  • 11
  • 17
  • I tried to set it up as follows using the this object. The program runs, but it always outputs as false. **public boolean equals(PointArray anotherPointArray) { double x = 0; double y = 0; for(int i = 0; i < points.length; i++) { for(int j = 0; j < anotherPointArray.points.length; j++) { if(this.points.equals(anotherPointArray.points)) { return true; } } } return false; }** –  Dec 02 '16 at 04:35
  • The problem is that inside the for-loops you are saying "return true if the array `this.points` is equal to `otherPointArray.points`, the array of the other PointArray". Two arrays will be defined as equal not if they have the same content but if they actually are the same (so they are pointing to the same). – J.Baoby Dec 02 '16 at 12:30
  • It is a bit like files: if you copy a file you copy the content of your file (all the bits on the harddrive) to somewhere else. For your system this will result in two different files even if they have the same content and even if they have the same name, because they will point to different locations on the harddrive. But if for example you have an entry to a file (bits on the harddrive) and on one way you manage to create another entry to that same file (thus pointing to the same memory location, the same bits) then the system will tell you it is the same file it points to the same location – J.Baoby Dec 02 '16 at 12:40