01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package myproject.model;

/**
 * A car remembers its position from the beginning of its road.
 * Cars have random velocity and random movement pattern:
 * when reaching the end of a road, the dot either resets its position
 * to the beginning of the road, or reverses its direction.
 */
public class Car implements Agent {
  Car() { } // Created only by this package

  private boolean backAndForth = Math.round(Math.random())==1 ? true : false;
  private double position = (MP.roadLength-MP.carLength) * Math.random ();
  private double velocity = (int) Math.ceil(Math.random() * MP.maxVelocity);
  private java.awt.Color color = new java.awt.Color((int)Math.ceil(128 + Math.random()*127),(int)Math.ceil(128 + Math.random()*127),(int)Math.ceil(Math.random()*255));

  public double getPosition() {
    return position;
  }
  public java.awt.Color getColor() {
    return color;
  }
  public void run(double time) {
    if (backAndForth) {
      if (((position + velocity) < 0) || ((position + velocity) > (MP.roadLength-MP.carLength)))
        velocity *= -1;
    } else {
      if ((position + velocity) > (MP.roadLength-MP.carLength))
        position = 0;
    }
    position += velocity;
  }
}