import geometry.*; // ----------------------------------------------------------------------------- // a data representation of moving, geometric objects // a Circ is one kind of mobile object // an Squr is the other mobile animal interface IMobile { // move this object for t clock ticks IMobile move(int t); // what is the base area of this object double area(); // how much volume does this object traverse as it moves for t clock ticks double volume(int t); } abstract class AMobile implements IMobile { protected Posn p; protected Velocity v; protected AMobile(Posn p, Velocity v) { this.p = p; this.v = v; } public double volume(int t) { double height = v.speed() * t; return height * area(); } } class Circ extends AMobile { private double radius = 50; public Circ(Posn p, Velocity v, double radius) { super(p,v); this.radius = radius; } public IMobile move(int t) { return new Circ(v.translate(p),v,radius); } public double area() { return Math.PI * radius * radius; } } class Squr extends AMobile { private double size = 2; public Squr(Posn p, Velocity v, double size) { super(p,v); this.size = size; } public IMobile move(int t) { return new Squr(v.translate(p),v,size); } public double area() { return size * size; } } // a data representation of 2D velocity class Velocity { private double deltaX; private double deltaY; public Velocity(double deltaX, double deltaY) { this.deltaX = deltaX; this.deltaY = deltaY; } // move the given Posn by this velocity public Posn translate(Posn p) { return new Posn((int)(p.x + deltaX),(int)(p.y + deltaY)); } // the distance (measure) travelled according to this velocity public double speed() { return Math.sqrt(deltaX * deltaX + deltaY * deltaY); } } // ----------------------------------------------------------------------------- // tests for Velocity class VelExamples { Velocity v1 = new Velocity(3,4); Velocity v2 = new Velocity(12,5); boolean b1 = check v1.speed() expect 5 within .0001; boolean b2 = check v2.speed() expect 13 within .1; boolean b3 = check v1.translate(new Posn(0,0)) expect new Posn(3,4); boolean b4 = check v1.translate(new Posn(10,20)) expect new Posn(13,24); } // tests for movable objects class Examples { VelExamples vex = new VelExamples(); Velocity v = vex.v1; IMobile c = new Circ(new Posn(10,10),v,50); IMobile s = new Squr(new Posn(20,20),v,10); boolean bm0 = check c.move(1) expect new Circ(new Posn(13,14),v,50); boolean bm1 = check s.move(1) expect new Squr(new Posn(23,24),v,10); boolean b1 = check c.volume(1) expect 5.0 * 50 * 50 * Math.PI within .1; boolean b2 = check s.volume(2) expect 1000 within .0001; }