| CIT
591 Horse Race Example Fall 2006, David Matuszek |
import java.util.Random;
public class Horse {
int distance;
String name;
int speed;
Random random = new Random();
Horse(String name, int speed) {
this.name = name;
this.speed = speed;
this.distance = 0;
}
void run() {
distance = distance + speed + random.nextInt(11) - 5;
System.out.println(name + " is at " + distance);
}
}
|
public class Race {
static int length = 150;
public static void main(String[] args) {
Race race = new Race();
race.doSomething();
}
private void doSomething() {
Horse h1 = new Horse("Alfred", 30);
Horse h2 = new Horse("Alpo", 30);
Horse h3 = new Horse("Joe", 30);
Horse h4 = new Horse("Seabiscuit", 30);
while (h1.distance < length &&
h2.distance < length &&
h3.distance < length &&
h4.distance < length) {
h1.run();
h2.run();
h3.run();
h4.run();
System.out.println();
}
}
}
|