public class Point { private int x; private int y; //----------------------------------------------- //Methods for graphics /** Moves this point per the x and y in passed <other> param */ public void moveBy(Point other) { /*Move this point by "other" Increase our x by other's x Increase our y by other's y e.g. (10,5) + (2,1) = (12,6) Algorithm: Set our x equal to our x + other's x Set our y equal to our y + other's y NOTE WELL: We do YES change "this" point (we do NOT return a new Point) */ this.x = (this.x + other.getX()); this.y = (this.y + other.getY()); } /** Moves the x value of this point per the passed <distance> param */ public void moveXBy(int distance) { this.x += distance; } /** Moves the x value of this point per the passed <distance> param */ public void moveYBy(int distance) { this.y += distance; } /** Returns new point, this point multiplied by <other> */ public Point multiply(Point other) { /* NOTE WELL: We do NOT change "this" point (instead we YES return a new Point) Algorithm: Construct new point, let's call it "p" Set p's x with our (this) x times other's x Set p's y with our (this) y times other's y return p */ Point xy = new Point(); xy.setX(this.getX() * other.getX()); xy.setY(this.getY() * other.getY()); return xy; } //----------------------------------------------- //Constructors /** Returns new Point at origin * this is a no-args constructor) */ public Point() { this.x = 0; this.y = 0; } /** Returns new point at (aX, aY) */ public Point(int aX, int aY) { this.x = aX; this.y = aY; } /** Returns new point at (value, value) */ public Point(int value) { this.x = value; this.y = value; //Could also do this (would need to be first line of constructor): //this(value, value); } //----------------------------------------------- /** Moves this point to origin */ public void moveToOrigin() { x = 0; y = 0; } /** Moves this point to (100, 70) */ public void move1() { x = 100; y = 70; } /** Returns x value of this point */ public int getX() { return x; } /** Returns y value of this point */ public int getY() { return y; } //----------------------------------------------------- //Setting /** Sets x value of this point */ public void setX(int aX) { this.x = aX; } /** Sets y value of this point */ public void setY(int aY) { this.y = aY; } /** Sets both x and y (to passed parameters) */ public void set(int aX, int aY) { this.setX(aX); this.setY(aY); /*Also could do: this.x = value; this.y = value;*/ } /** Sets both x and y (to passed parameter) */ public void set(int value) { //Simplify by calling sister method (method in this class) this.set(value, value); /*Also could do: this.setX(value); this.setY(value);*/ /*Also could do: this.x = value; this.y = value;*/ } //----------------------------------------------------- @Override /** Returns friendly (one-line) string describing this object */ public String toString() { return "Point: " + basicToString(); } private String basicToString() { return "(" + this.x + ", " + this.y + ")"; } }
public Point()public Point(int aX, int aY)Returns new point at (aX, aY)public Point(int value)Returns new point at (value, value)public void moveBy(Point other)Moves this point per the x and y in passedparam public void moveXBy(int distance)Moves the x value of this point per the passedparam public void moveYBy(int distance)Moves the x value of this point per the passedparam public Point multiply(Point other)Returns new point, this point multiplied bypublic void moveToOrigin()Moves this point to originpublic void move1()Moves this point to (100, 70)public int getX()Returns x value of this pointpublic int getY()Returns y value of this pointpublic void setX(int aX)Sets x value of this pointpublic void setY(int aY)Sets y value of this pointpublic void set(int aX, int aY)Sets both x and y (to passed parameters)public void set(int value)Sets both x and y (to passed parameter)public String toString()Returns friendly (one-line) string describing this objectprivate String basicToString()
public class PointTests { public static void main(String[] args) { PointTests test = new PointTests(); test.test_setting(); test.test_getting(); test.test_moveBy(); } public void test_setting() { println("\ntest_setting"); Point p = new Point(); p.setX(100); p.setY(50); println(p); } public void test_getting() { println("\ntest_getting"); Point p = new Point(); p.setX(200); p.setY(300); println("x: " + p.getX()); println("y: " + p.getY()); } public void test_moveBy() { println("\ntest_moveBy"); Point p1 = new Point(), p2 = new Point(); p1.setX(1000); p1.setY(500); p2.setX(50); p2.setY(3); println("Before Move: " + p1); println("p2: " + p2); p1.moveBy(p2); println("After Move by p2: " + p1); } //------------------------------------------------------------- // Helpers public static void println(Object o) { System.out.println(o.toString()); } public static void printf(String template, Object... args) { System.out.printf(template, args); } }
this is a no-args constructor)