001
002 /**
003 * Point is an abstract class which can be used to store any
004 * dimensional system. A mathematical operation on a Point; add,
005 * multiply, and abs, can be performed using instance or static
006 * methods.*/
007 public abstract class Point implements Cloneable {
008
009 /**
010 * Instance method to return the dimension of the Point, abstract
011 * and must be defined by any subclasses of Point. */
012 public abstract int dimension();
013
014 /**
015 * Instance method to return the coordinate of the Point at index
016 * i, abstract and must be defined by any subclasses of Point. */
017 public abstract double coordinate(int i);
018
019 /**
020 * Instance method to add a Point to the current Point, abstract
021 * and must be defined by any subclasses of Point. */
022 public abstract void add(Point delta);
023
024 /**
025 * Instance method to multiply the current Point by a double,
026 * abstract and must be defined by any subclasses of Point. */
027 public abstract void multiply(double c);
028
029 /**
030 * Method to return the maximum of the Point, abstract and must be
031 * defined by any subclasses of Point. */
032 public abstract double max();
033
034 /**
035 * Method to return the minimum of the Point, abstract and must be
036 * defined by any subclasses of Point. */
037 public abstract double min();
038
039 /**
040 * Instance method to calculate the absolute value of the Point,
041 * abstract and must be defined by any subclasses of Point. */
042 public abstract void abs();
043
044 /**
045 * Instance method to return the contents of the Point in human
046 * readable form, abstract and must be defined by any subclasses
047 * of Point.*/
048 public abstract String toString();
049
050 /**
051 * Instance method to truncate the Point's accuracy to n decimal
052 * places, abstract and must be defined by any subclasses of
053 * Point.*/
054 public abstract void truncate(int n);
055
056 /**
057 * Instance method to round the Point's accuracy to n decimal
058 * places, abstract and must be defined by any subclasses of
059 * Point.*/
060 public abstract void round(int n);
061
062 /**
063 * Overrides the object.clone() method to enable Point cloning
064 * outside of Point package.*/
065 public Object clone() {
066 try {
067 Point p = (Point) super.clone();
068 return (Object) p;
069 } catch (CloneNotSupportedException cloneError) {
070 throw new Error("This should never happen!");
071 }
072 }
073
074 /**
075 * Static method to multiply a Point by a double.*/
076 public static Point multiply(Point p, double c){
077 Point q = (Point) p.clone();
078 q.multiply(c);
079 return q;
080 }
081
082 /**
083 * Static method to add two Points.*/
084 public static Point add(Point a, Point b){
085 Point aClone = (Point) a.clone();
086 aClone.add(b);
087 return aClone;
088 }
089
090 /**
091 * Static method to add three Points.*/
092 public static Point add(Point a, Point b, Point c){
093 Point aClone = (Point) a.clone();
094 aClone.add(b);
095 aClone.add(c);
096 return aClone;
097 }
098
099 /**
100 * Static method to add four Points.*/
101 public static Point add(Point a, Point b, Point c, Point d){
102 Point aClone = (Point) a.clone();
103 aClone.add(b);
104 aClone.add(c);
105 aClone.add(d);
106 return aClone;
107 }
108
109 /**
110 * Static method to calculate and return a new Point equal to the
111 * absolute value of the Point p.*/
112 public static Point abs(Point p) {
113 Point q = (Point) p.clone();
114 q.abs();
115 return q;
116 }
117
118 }
119
120
121
122