/**
*
* The class models the X & Y coordinates on a Cartesian plane:
* the internal representation (the measurement stored in the
* private data members) is in integers.
*
* Name:
* Course:
* Section:
* Last Modified:
* Known Bugs:
*/
import java.io.*;
public class Point {
/**
* instance variables
*/
private int myX, myY;
/**
* sets the default values of the Point object to 0, 0
*/
public Point() {
myX = 0; myY = 0;
}
/**
* sets the value of the Point object to the value of the
* X and Y parameters (which are ints)
*
* @param int X value in the object
* @param int Y value in the object
*/
public Point(int X, int Y) {
myX = X; myY = Y;
}
/**
* sets the value of the Point object to the value of the
* X and Y parameters (which are Strings)
*
* @param int X value in the object
* @param int Y value in the object
*/
public void set(String X, String Y) {
myX = Integer.parseInt(X);
myY = Integer.parseInt(Y);
}
/**
* to be used for debugging or by other methods
*
* @return internal representation of the X value
*
*
*/
public int getX() {
return myX;
}
/**
* to be used for debugging or by other methods
*
* @return internal representation of the Y value
*
*
*/
public int getY() {
return myY;
}
/**
* adds the x, y values of the passed object to the x, y values
* respectively of the Point object that invoked the method
*
*
* @param Point another Point object
* @return Point object with the total new coordinates
*
*/
public Point add(Point other) {
Point P = new Point();
P.myX = myX + other.myX;
P.myY = myY + other.myY;
return P;
}
/**
* determines the distance between the x, y values of the passed
* object to the x, y values respectively of the Point object
* that invoked the method
*
* Uses the distance function:
* distance = sqrt((x2 - x1)^2 + (y2 - y1)^2)
*
*
* @param Point another Point object
* @return double distance between the two Point objects
*
*/
public double distance(Point other) {
double d = Math.sqrt(Math.pow((myX - other.myX), 2)+Math.pow((myY - other.myY), 2));
return d;
}
/**
* return a string with an external representation (X and
* Y) of the values of the Point object. The format
* for this output is X = xx, Y = yy (where xx is the x value
* and yy is the y value).
*
* @return String
*/
public String toString() {
String S = "X = " + myX + ", Y = " + myY;
return S;
}
}