/******************************** FILE HEADER **********************************
Main Class File: Main.java
File: Point.java
Author: Michael Schultz
CS Login: mschultz
Collaborators:
Completion Date: 9/24/02
Course: CS 302
TA: Mr. Schultz
Compiler: Code Warrior IDE 5.0
Platform: Windows 2000
*******************************************************************************/
/**
* The Point class is used to represent a point in some two-dimenstion space,
* the axes of which are labeled here as x and y. Each point also has a
* constant name, so that the points may be reassigned x / y values.
* There is a class method called distance, which computes the euclidean
* distance between two Point instances, using the java.lang.Math class.
*
Bugs: none known
*
* @author Michael Schultz
* @version 1.0
* @see also Main.java
*/
public class Point {
/** The name of the Point instance */
private final String name;
/** The value of the x-coordiante */
private int x;
/** The value of the y-coordinate */
private int y;
/**
* A constructor in which to specify just the name. The other values are
* given their default values according to their primitive types.
*
* @param name The name of the point
*/
public Point(String name) {
this.name = name;
System.out.println("A new Point object, \"" + getName() + "\", has " +
"been created with the default int values for x" +
" and y.\n");
}
/**
* A constructor in which to specify all three data members at once. Uses
* the "this" reference to call the first constructor.
*
* @param name The name of the point, gets passed to the first constructor
* @param startX The initial x-coordinate of the point
* @param startY The initial y-coordinate of the point
*/
public Point(String name, int startX, int startY) {
this(name);
setX(startX);
setY(startY);
System.out.println("A new Point object, \"" + getName() + "\", has " +
"been created at (" + getX() + "," + getY() +
").\n");
}
/**
* A copy constructor, in which to specify the name of the new point, and
* a point whose coordinates to copy into this
*
* @param name The name of the point
* @param source The source Point whose coordinates to copy
*/
public Point(String name, Point source) {
this.name = name;
setX(source.getX());
setY(source.getY());
System.out.println("A new Point object, \"" + getName() + "\", has " +
"been created copying Point " + source.getName() +
" at (" + getX() + "," + getY() + ").\n");
}
/**
* An accessor for the data name
*
*@return The name of the point
*/
public String getName() {
return name;
}
/**
* An accessor for the x-coordiante
*
* @return The value of the x-coordinate
*/
public int getX() {
return x;
}
/**
* An accessor for the y-coordinate
*
* @return The value of the y-coordinate
*/
public int getY() {
return y;
}
/**
* A mutator for the x-coordiante
*
* @param newX The new value for the x-coordinate
*/
public void setX(int newX) {
x = newX;
}
/**
* A mutator for the y-coordiante
*
* @param newY The new value for the y-coordinate
*/
public void setY(int newY) {
y = newY;
}
/**
* A public class method that returns the euclidean distance between two
* point instances. The euclidean distance metric is order-independent.
* Also note that while the method take in intergral point values, the
* return is necessarily a decimal.
*
* @param a One of the Points for the calculation
* @param b One of the Points for the calculation
*
* @return The decimal euclidean distance metric
*/
public static double distance(Point a, Point b) {
int xDistance = distance(a.getX(),b.getX());
int yDistance = distance(a.getY(),b.getY());
double xDistanceSquared = Math.pow(xDistance,2);
double yDistanceSquared = Math.pow(yDistance,2);
return Math.sqrt(xDistanceSquared + yDistanceSquared);
}
/**
* A private class helper method which returns the linear distance
* between two integer numbers. This is order independent.
*
* @param coordinate1 The first integer value
* @param coordinate2 The second integer value
*
* @return The linear distance (absolute value of the difference) between
* the two values
*/
private static int distance(int coordinate1, int coordinate2) {
return Math.abs(coordinate1 - coordinate2);
}
/**
* The method inherited from Object and overriden here to give an
* informative String about the instance at hand
*
* @return An informative String about "this" instance
*/
public String toString() {
return "Point " + getName() + " is located at (" + getX() +"," +
getY() + ").\n";
}
}