package goal3; /* Nice programming language: http://nice.sf.net */ /* written by Daniel Bonniot */ abstract class Point { double x; double y; double length(); } class Point2d extends Point { toString() = "[" + this.x + "," + this.y + "]"; length() = Math.sqrt(this.x * this.x + this.y * this.y); } P translate(P, double); translate(p#Point2d, distance) = new Point2d(x: p.x + distance, y : p.y); class Point3d extends Point2d { double z; toString() = "[" + this.x + "," + this.y + "," + this.z + "]"; length() = Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z); } translate(p#Point3d, distance) = new Point3d(x: p.x + distance, y : p.y, z : p.z); void test(Point p) { print("" + p.length() + " " + p.translate(1) + " "); } P add(P, P); add(p1#Point2d, p2@Point2d) = new Point2d(x: p1.x + p2.x, y: p1.y + p2.y); add(p1#Point3d, p2#Point2d) = new Point2d(x: p1.x + p2.x, y: p1.y + p2.y); add(p1#Point3d, p2#Point3d) = new Point3d(x: p1.x + p2.x, y: p1.y + p2.y, z: p1.z + p2.z); P twice(P p) = add(p, p); void main(String[] args) { Point2d p2d = new Point2d(x: 3, y: 4); Point3d p3d = new Point3d(x: 1, y: 2, z: 2); Point[] l = [ p2d, p3d ]; test(p2d); test(p3d); l.foreach(Point p => print(" " + p.length())); println(""); println(p3d.translate(1).z); print("" + twice(p2d) + " " + twice(p3d)); l.foreach(Point p => print(" " + twice(p))); println(""); println("" + p3d.add(p2d) + " " + p2d.add(p3d)); println("" + twice(p3d).z); println("" + p3d.add(p3d).z); }