Okay, this is going places. Update.
////////////////////////////////////////////////////////////////////////
//
// Car
//
// The objects and methods in this class simulate a car
//
// Edward Woo
// 28 April 2004
//
////////////////////////////////////////////////////////////////////////
import java.util.Random;
public class Car
{
private Random generator;
private String model;
// the car model (must be non-null)
private int range;
// the maximum range a new car can be from the park
private int time;
// the time waited for cars to move closer to the park
private int randomDistance;
// the random distance from the park generated for a car, as within the range
private int currentDistance;
// the current distance a car has from the park
public Car(String name, int c)
// constructor for a car with a given name
{
model = name;
generator = new Random();
currentDistance = c;
}
public String getModel()
// returns the model of a car
{
return model;
}
public void assignRandomDistance(int r)
// assigns a random distance from the park to a car
// @ param r the range that the car's random distance is generated within
{
range = r;
randomDistance = 1 + generator.nextInt(range);
System.out.println(getModel() + " begins " + randomDistance + "km from Park.");
currentDistance = randomDistance;
}
public void assignDistance(int d)
// assigns a value to a car's distance
{
currentDistance = d;
}
public int getDistance()
// returns a car's current distance
{
return currentDistance;
}
public int generateDistance(int r)
// assigns a random distance from the park to a car
// @ param r the range that the car's random distance is generated within
{
range = r;
randomDistance = 1 + generator.nextInt(range);
return randomDistance;
}
public void assignArrayDistances(int r, Car cars[])
// traverses an array of cars and assigns a random distances to each car
// @ param r the range that the cars' random distances are generated within
{
for (int i=0; i < cars.length; i++)
{
int p = generateDistance(r);
cars[i].assignRandomDistance(p);
}
}
public void wait(int time, Car cars[])
// traverses an array of cars and assigns a random distances to each car
// @ param r the range that the cars' random distances are generated within
{
for (int j=0; j < time; j++)
{
for (int i=0; i < cars.length; i++)
{
if(cars[i].getDistance()==0)
{
}
else
{
int n = cars[i].getDistance() - 1;
cars[i].assignDistance(n);
}
}
}
System.out.println(" ");
for (int i=0; i < cars.length; i++)
cars[i].printDistance();
}
public void printDistance()
// prints a car's present distance to the screen
{
if(currentDistance == 0)
{
System.out.println(getModel() + " has reached the Park!");
}
else
{
System.out.println(getModel() + "'s distance from Park is now " + currentDistance + "km.");
}
}
public static void main(String[] args){
// Car car1 = new Car("car1");
// car1.assignDistance(300);
// Car car2 = new Car("car2");
// car2.assignDistance(100);
// car1.printDistance();
// car2.printDistance();
Car cars[] = {new Car("carA",0), new Car("carB",0), new Car("carC",0),
new Car("carD",0), new Car("carE",0), new Car("carF",0), new Car("carG",0), };
Car basecar = new Car(null,0);
basecar.assignArrayDistances(50,cars);
// cars[0].printDistance();
// cars[4].printDistance();
// cars[3].assignDistance(19);
// cars[3].printDistance();
basecar.wait(5,cars);
}
}