-1

I'm half way through an assignment in java where I have two classes and need to calculate a x- and y-position. I get half of the tests correct but can't seem to get the two last ones correct. Could you perhaps guide my in the right direction? I have one class calle Point and one PointMain. I get the arguments in PointMain and need to create the right methods in Point. I can't make any changes in the class PointMain as I got that from the assignment.

Class Point:

public class Point {

    private int x = 0;
    private int y = 0;

    public Point() {

    }

    public Point(int xPoint, int yPoint) {
        x = xPoint;
        y = yPoint;
    }

    public String toString() {
        return x + "," + y;
    }

    public double distanceTo(Point p2) {
        double avstand = Math.sqrt(((p2.x*1 - p2.x*2) * (p2.x*1 - p2.x*2)) + ((p2.y*1 - p2.y*2) * (p2.y*1 - p2.y*2)));
        return avstand;
    }

    public void move(int iPoint, int jPoint) {
        x = x + iPoint; // I have a problem with this that it doesn't add
        y = y + jPoint; // the 3,4 that I got from p2 with the 5,-2.

    }

    public void moveToXY(int xTag, int yTag) {

    }

    public boolean isEqualTo(Point p2) { //And I'm not really sure on how to 
        return false;                    //construct this method either...    
    }

}

Class PointMain:

public class PointMain {

    public static void main(String[] args) {
        Point p1 = new Point();
        Point p2 = new Point(3,4);

        System.out.println(p1.toString());   // ==> (0,0)
        System.out.println(p2.toString());   // ==> (3,4)

        if (p1.isEqualTo(p2))              // False!
            System.out.println("The two points are equal");

        double dist = p1.distanceTo(p2);
        System.out.println("Point Distance: "+dist);

        p2.move(5,-2);         // ==> (8,2)
        p1.moveToXY(8,2);      // ==> (8,2)
            System.out.println(p1);
            System.out.println(p2);
        if (p1.isEqualTo(p2))              // True!
            System.out.println("The two points are equal");

    }

}
Thomas Bengtsson
  • 399
  • 2
  • 10
  • 22
  • When you make a function call p1.distanceTo(p2); then you are changing the values of your class variables. distanceTop() assigns the p2.x and p2.y new values. So they are no longer 3 and 4 as you would expect. – Juniar Dec 17 '17 at 16:49

2 Answers2

1

First of all you should add getters

   public int getX()
   {
      return x;
   }

   public int getY()
   {
       return y;
   }

Than implement isEqual

    public boolean isEqualTo(Point p2) {  
    return x == p2.getX() && y == p2.getY();                       
}

you can also declare x and y public and then there is no need for getters and code is simpler as you can see in implementation of java.awt.Point. I don't see problem with "move" function. And last

public void moveToXY(int xTag, int yTag) {
    x = xTag;
    y = yTag;
}

For additional info you can lookup how java.awt.Point implemented, and work on your function parameters naming iPoint/jPoint is horrible names

Yony Yatsun
  • 153
  • 3
0

For the equality method, think that in which condition the two points are equal. Consider using if for validating the condition and getters for getting the points x and y.

And I don't see any problem with the move method.

And as @Juniar said in the comments, there's another problem in your distanceTo method. You want to get x and y of p2 but they are private variables so you can't have them by this way. In this case your are having the x and y of the object which the method is called on. So the output won't be desirable. (see getters again)

Parsa
  • 1
  • 2