import java.awt.geom.Point2D; public class VehicleImpl implements Vehicle { protected double maximumSpeed; protected double speed = 0; protected double direction = 0; protected Point2D position = new Point2D.Double(0., 0.); private void checkPositiveSpeed(double speed) { if (speed < 0) { throw new IllegalArgumentException("negative speed forbidden"); } } public VehicleImpl(double maximumSpeed) { checkPositiveSpeed(maximumSpeed); this.maximumSpeed = maximumSpeed; } public void setDirection(double direction) { this.direction = direction % (2 * Math.PI); } public void setSpeed(double speed) { checkPositiveSpeed(speed); this.speed = Math.min(maximumSpeed, speed); } public double direction() { return direction; } public double speed() { return speed; } public double maximumSpeed() { return maximumSpeed; } public Point2D position() { return position; } public void go(double time) { double distance = time * speed; double x = Math.cos(direction) * distance; double y = Math.sin(direction) * distance; position.setLocation(position.getX() + x, position.getY() + y); } public String toString() { return "Vehicle (" + position.getX() + "," + position.getY() + ") + speed " + speed + ", direction " + direction; } }