Index
Code


Here is the code for a Point class.

This is a class that can give a greeting in various languages.

This code is ready to run -- just copy it into a file named Point.java.

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 + ")";
	}
	
}


Code Doc


Here is the code doc (i.e., code documentation) for the Point class.

It is much more concise -- does not include the code (method bodies).

For message sending, this code doc is all we need.

To learn more about the method headers shown below go here.

public Point()
Returns new Point at origin
this is a no-args constructor)
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 passed param
public void moveXBy(int distance)
Moves the x value of this point per the passed param
public void moveYBy(int distance)
Moves the x value of this point per the passed param
public Point multiply(Point other)
Returns new point, this point multiplied by
public void moveToOrigin()
Moves this point to origin
public void move1()
Moves this point to (100, 70)
public int getX()
Returns x value of this point
public int getY()
Returns y value of this point
public void setX(int aX)
Sets x value of this point
public void setY(int aY)
Sets y value of this point
public 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 object
private String basicToString()


Test (Example Message Sends)


Below we send some messages to a Point object.

This code is ready to run -- just copy it into a file named PointTests.java.

For example, the code doc tells us that the Point object type (class) understands the message sayHelloInIndonesian -- thus we send it as shown below.


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);
	}
	
}


Try-It


Try sending some messages here