CS 631 - Computer Science II - Spring 2007
Abstract Data Types
Loyola College >
Department of Computer Science >
Dr. James Glenn >
CS 631 >
Examples and Lecture Notes >
Abstract Data Types
Point.java
/**
* A point in the Cartesian plane.
*
* @author Jim Glenn
* @version 0.1 1/31/2007
*/
public interface Point
{
/**
* Returns the x coordinate of this point.
*
* @return the x coordinate of this point
*/
public double getX();
/**
* Returns the y coordinate of this point.
*
* @return the y coordinate of this point
*/
public double getY();
/**
* Returns the distance of this point from the origin.
*
* @return the distance of this point from the origin
*/
public double getDistanceFromOrigin();
/**
* Returns the angle, in degrees, between the line from this point to the
* origin and the x-axis. This point must not be the origin.
*
* @return the angle of this point in polar coordinates
* @throws IllegalStateException if this point is the origin
*/
public double getAngle();
/**
* Moves this pointso that it is the given factor further away from the
* origin, keeping the (point,origin,x-axis) angle the same.
*
* @param factor how much to scale this point by
*/
public void scale(double factor);
}
XYPoint.java
/**
* A point in the Cartesian plane.
*
* @author Jim Glenn
* @version 0.1 1/31/2007
*/
public class XYPoint implements Point
{
/**
* The x coordinate of this point.
*/
private double x;
/**
* The y coordinate of this point.
*/
private double y;
/**
* Creates a point at the given location in the plane.
*
* @param xInit the x coordinate of the new point
* @param yInit the y coordinate of the new point
*/
public XYPoint(double initX, double initY)
{
x = initX;
y = initY;
}
/**
* Returns the x coordinate of this point.
*
* @return the x coordinate of this point
*/
public double getX()
{
return x;
}
/**
* Returns the y coordinate of this point.
*
* @return the y coordinate of this point
*/
public double getY()
{
return y;
}
/**
* Returns the distance of this point from the origin.
*
* @return the distance of this point from the origin
*/
public double getDistanceFromOrigin()
{
return Math.sqrt(x * x + y * y);
}
/**
* Returns the angle, in degrees, between the line from this point to the
* origin and the x-axis.
*
* @return the angle of this point in polar coordinates
*/
public double getAngle()
{
if (getDistanceFromOrigin() == 0)
throw new IllegalStateException("Can't get angle of origin");
double angle;
if (y > 0)
angle = Math.toDegrees(Math.acos(x / getDistanceFromOrigin()));
else
angle = -Math.toDegrees(Math.acos(x / getDistanceFromOrigin()));
return angle;
}
/**
* Moves this point so that it is the given factor further away from the
* origin, keeping the (point,origin,x-axis) angle the same.
*
* @param factor how much to scale this point by
*/
public void scale(double factor)
{
x *= factor;
y *= factor;
}
/**
* Returns a printable representation of this point.
*
* @return a printable representation of this point
*/
public String toString()
{
return "(" + getX() + "," + getY() + ")";
}
}
PolarPoint.java
/**
* A point in the Cartesian plane.
*
* @author Jim Glenn
* @version 0.1 1/31/2007
*/
public class PolarPoint implements Point
{
/**
* The distance from this point to the origin.
*/
private double distance;
/**
* The angle between this point, the origin, and the x-axis, in radians.
*/
private double angle;
/**
* Creates a point at the given location in the plane.
*
* @param xInit the x coordinate of the new point
* @param yInit the y coordinate of the new point
*/
public PolarPoint(double initX, double initY)
{
distance = Math.sqrt(initX * initX + initY * initY);
if (distance > 0.0)
{
if (initY > 0)
angle = Math.acos(initX / distance);
else
angle = -Math.acos(initX / distance);
}
else
angle = 0.0;
}
/**
* Returns the x coordinate of this point.
*
* @return the x coordinate of this point
*/
public double getX()
{
return Math.cos(angle) * distance;
}
/**
* Returns the y coordinate of this point.
*
* @return the y coordinate of this point
*/
public double getY()
{
return Math.sin(angle) * distance;
}
/**
* Returns the distance of this point from the origin.
*
* @return the distance of this point from the origin
*/
public double getDistanceFromOrigin()
{
return distance;
}
/**
* Returns the angle, in degrees, between the line from this point to the
* origin and the x-axis. This point must not be the origin.
*
* @return the angle of this point in polar coordinates
* @throws IllegalStateException if this point is the origin
*/
public double getAngle()
{
if (getDistanceFromOrigin() == 0.0)
throw new IllegalStateException("Can't get angle of origin");
return angle;
}
/**
* Moves this point so that it is the given factor further away from the
* origin, keeping the (point,origin,x-axis) angle the same.
*
* @param factor how much to scale this point by
*/
public void scale(double factor)
{
distance *= factor;
}
/**
* Returns a printable representation of this point.
*
* @return a printable representation of this point
*/
public String toString()
{
return "(" + getX() + "," + getY() + ")";
}
}
This code can also be downloaded from the files
Point.java,
XYPoint.java,
and PolarPoint.java.