-1

I am doing a homework (composite classes on calculating the distance between two points. I have classes (Line, Point and Main). I have to use the methods my teacher designed on UML. However, i found that i am able to calculate the distance without fulfilling some of the methods . I'm just wondering if anyone knows what they are for.

I believe they are used to calculate and return the distance of Point P. However, Point P has only 1 point so how can it be calculated? Or maybe Point P takes the value of my second constructor and copy constructor and form a calculation? Thank you all for your help.

These are my code below:

Class Point

class Point

{
private int x;
private int y;

//default constructor
public Point()
{
    //do nothing
}
// second constructor
public Point(int x, int y)
{
    this.x=x;
    this.y=y;
}


// Copy constructor 
public Point (Point p)
{
    this (p.x,p.y);
}

private double distance(Point p)
{
    // how can i calculate distance with just a single point?
}

public double getDistance(Point p)
{
    // how can i return distance with just a single point?
}
    // getter
public int getX()
{
return x;
}
public int getY()
{
return y;
}
    /setter
public void set(int x, int y)
{
this.x = x;
this.y = y;
}

My main method will generate random integer and instantiate an object with the following outcome:

Point 1 (43, -90)
Point 2 (-70, -34)
Distance (126.1150)
Kirbbbb
  • 133
  • 1
  • 9

3 Answers3

1

how can i calculate distance with just a single point?

You wouldn't be able to with just one Point. But you don't have just one Point, you have two Points. One is the current object, the other is the object passed to the method.

Not to do the homework for you, but just to clear up the confusion so you can proceed...

public double getDistance(Point p)
{
    // Here you would calculate and return the distance between "this" and "p".
    // So your values are:
    //   this.getX()
    //   this.getY()
    //   p.getX()
    //   p.getY()
}
David
  • 208,112
  • 36
  • 198
  • 279
0

not sure what's the difference between "get Distance" and "Distance" but from my understanding you need to calculate the distance between the current point (this.x, this.y) and another point (the one you're sending via the function).

so:

private double distance(Point p)
{
    // how can i calculate distance with just a single point?

    var dX = this.x- p.x ;
    var dY  = this.y- p.y ;

    return  ( Math.sqrt( dX * dX + dY * dY   ) );


}
Yulian
  • 66
  • 6
0
private double distance(Point p)
{
    // how can i calculate distance with just a single point?
}

How do you execute this method? Somewhere in main method You will create a object of the class point and execute that method and pass another object.

Point pointA = new Point (1,1);
Point pointB = new Point (3,3);
double distance = pointA.distance(pointB);

Now we have the Object and another Object is passed.

private double distance(Point p)
{
    int distanceX = this.x - p.x;
    int distanceY = this.y - p.y;
    double result = //TODO some formula
    return result;

}
Sagar Devkota
  • 1,295
  • 3
  • 12
  • 25