/** * COSC 1010 toString() Demo. * Yet Another Point class. * @author Dennis Brylow * @version 1.0,   2010 Oct 11 */ class Point { int x; int y; public Point(int x, int y) { this.x = x; this.y = y; } /** This toString() method demonstartes how we teach an object to * print itself out. */ public String toString() { String s = new String("("); s = s + this.x; s = s + ","; s = s + this.y; s = s + ")"; return s; } public static void main(String args[]) { Point origin = new Point(0,0); /* By concatentating 'origin' to a String, the Point class * toString() method is automatically invoked on origin. */ System.out.println("Origin is " + origin); Point p1 = new Point(5, 7); /* This line explicitly calls the toString() method on p1, * but effect is the same as the implicit invocation above. */ System.out.println("Point 1 is " + p1.toString()); } }