Dear all ,
I have the following UML Diagram and below the associated Code. Can you please help me complete the code?
Thanks in advance

I have written my UML diagram as follows.

-x : double
-y : double

+MyPoint()
+MyPoint(x:double,y:double)
+get X() :double
+getY() : double
+distance: (MyPoint1:MyPoint2):double
+distance(p1:MyPoint:p2:MyPoint):double

My code is the following and I am missing the last two methods

import java.lang.Math
public class MyPoint {
private double x,y, distance;
public MyPoint(){
x=0;
y=0; // assign zero to the attribute y.
}
public MyPoint(double x, double y){
this.x=x;
this.y=y;
}

public getX(double x){
return x;
}
public getY(double y){
return y;
}
public double distance (MyPoint1 , MyPoint2){
// distance =
}

public static void main(String []args)
{
MyPoint MyPoint1 = new MyPoint();
MyPoint MyPoint2 = new MyPoint(10, 30.5);

}

}//end of class

I think there is a problem with two of your method definitions. In the UML, you say that getX() and getY() return a double but in the code, both end up taking a double which contradicts the UML, also the method definition should have double after the word public.

finally, to calculate the distance between 2 points, use the formula:
d = square root((x2-x1)squared + (y2-y1)squared) where x1,y1,x2,y2 are the x and y-coordinates of the points.

The UML for the class MyPoint could be written as follows:

-x : double
-y : double

+MyPoint()
+MyPoint(x:double,y:double)

+getX() : double
+getY() : double
+setX(x:double):void
+setY(y:double):void
+distance(AnotherPoint:MyPoint):double
+distance(p1:MyPoint,p2:MyPoint):double

This article has been dead for over six months. Start a new discussion instead.