0% found this document useful (0 votes)
52 views2 pages

MyPoint Java

The document contains a Java class named MyPoint that represents a point in a 2D space with x and y coordinates. It includes methods for setting coordinates, getting coordinates, calculating distances to another point or the origin, and a main method that demonstrates its functionality. The output shows the coordinates of two points and the distances between them and to the origin.

Uploaded by

Nagaraj Naik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views2 pages

MyPoint Java

The document contains a Java class named MyPoint that represents a point in a 2D space with x and y coordinates. It includes methods for setting coordinates, getting coordinates, calculating distances to another point or the origin, and a main method that demonstrates its functionality. The output shows the coordinates of two points and the distances between them and to the origin.

Uploaded by

Nagaraj Naik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

File: /home/mybfan1011/MyPoint.

java Page 1 of 2

import [Link];

public class MyPoint{


private int x;
private int y;

public MyPoint(){
this.x=0;
this.y=0;
}

public MyPoint(int x,int y){


this.x=x;
this.y=y;
}

public void setXY(int x, int y){


this.x=x;
this.y=y;
}

public int[] getXY(){


return new int[]{x,y};
}

public String toString(){


return"("+x+","+y+")";
}

public double distance(int x, int y){


int xDiff=this.x-x;
int yDiff=this.y-y;
return [Link](xDiff*xDiff+yDiff*yDiff);
}

public double distance(MyPoint another){


int xDiff=this.x-another.x;
int yDiff=this.y-another.y;
return [Link](xDiff*xDiff+yDiff*yDiff);
}

public double distance(){


return distance(0,0);
}

public static void main(String[]args){


MyPoint point1=new MyPoint(1,2);
MyPoint point2=new MyPoint(4,6);

[Link]("Point 1:"+ [Link]());


[Link]("Point 2:"+ [Link]());

[Link]("Distance between Point 1 and (4,6):"+[Link](4,6));


[Link]("Distance between Point 1 and Point
2:"+[Link](point2));
[Link]("Distance between Point 1 and the origin:"+[Link]());
}
}

/** output:-
Point 1:(1,2)
Point 2:(4,6)
Distance between Point 1 and (4,6):5.0
Distance between Point 1 and Point 2:5.0
File: /home/mybfan1011/[Link] Page 2 of 2

Distance between Point 1 and the origin:2.23606797749979 **/

You might also like