The main purpose of this project is to use stacks to simulate the arrangement of cars in a parking garage. The simulated garage is modeled after Stephanie's favorite parking garage in Boston and is designed to maximize the number of cars that can park - not to facilitate easy movement of cars. Cars are parked in lanes, such that the first car in each lane has many cars parked behind it. It will need to be the last one out. In other words, each lane is a stack. The simulation of the parking garage will have 3 stacks representing 3 parking lanes. It will also have a 4th stack, representing a temporary parking lane (we will call it a holding area). This way, if a driver is demanding their car, the attendant can put all the cars behind it into the temporary lane, remove the driver's car, then put the other back in the lane.


Car Class

Create a Car class to represent a car that is being parked or moved around in a parking garage. The Car class is modeled after the Cell class from the previous project, but it will not have an updateState() method or the alive field -- cars are passive agents. The corresponding getAlive() and setAlive() are gone too. The Car class should store its color, store the time it wants to be retrieved from the garage (an int that indicates the time step at which the car should leave the garage), and implement the following methods.

Implement the above methods and test them with a main() method.

CarStack Class

Create a CarStack class to store a stack of Car objects, modeled after the IntStack class you implemented during lab. It should store the index of the top of the stack, an array of Car objects, and implement the following methods:

Implement the above methods and test them with a main() method.

ParkingGarage Class

Create a ParkingGarage class to store lanes (stacks) of Cars. The ParkingGarage class is modeled after the Landscape class from the previous project, but, instead of having a simple advance() method, ParkingGarage has specific methods to park and unpark cars. A ParkingGarage is a smart object in that the logic for moving around cars belongs in its methods. Think of the ParkingGarage as indirectly simulating the parking attendants. It should have fields to store the lanes (an array of CarStacks), the holding area (a CarStack), and the maximum number of cars in any lane, including the holding area (an int). Start by implementing these methods:

Implement the above methods and test them with a main() method.


Make it possible to visualize your ParkingGarage.

  1. Copy LandscapeDisplay.java from last week, making ParkingGarageDisplay.java. Update it so that it stores a ParkingGarage instead of a Landscape.
  2. Add a method to your Car class: public void draw(Graphics g, int x, int y, int w, int h) draws a car in the rectangle that is rooted at (x,y) and has width w, and height h. Be sure to use the color field of the Car to set the color.
  3. Add a method to your ParkingGarage class: public void draw(Graphics g, int scale)draws the parking garage at the given scale. Use information about the location of each lane to determine the location of each car. At a minimum, this should draw the set of parking lanes. It could also include the holding area, but that may not be interesting (spoiler: the holding area is emptied at the end of every time step, so there will be nothing to draw).

Test your visualization by running ParkingGarageDisplay as your main program. You will need to update the main program to create new Cars, create a ParkingGarage, and park the cars.


Add a method to the ParkingGarage class: public void retrieveCar( Car car ) removes the car from the lane it is parked in. If there are cars behind it, move those cars into the holding area, then remove the car, then put the others back into the lane.

Test the method by adding code to your main() method in LandscapeDisplay. Display a full garage, then display one with one of the cars removed. Note that you can create, park, and remove a car with the following code.

Car c1 = new Car( 10, new Color( 1.0f, 0.0f, 0.0f ) );
Car c2 = new Car( 3, new Color( 0.0f, 1.0f, 0.0f ));
parkingGarage.parkCar( c1 );
parkingGarage.parkCar( c2 );
parkingGarage.retrieveCar( c1 );

ParkingSimulation Class

Create a class that will simulate Cars being parked an retrieved. This class will be modeled after LifeSimulation, but will have more logic in it.

Implement a method public void run(double probabilityToPark, int timestep) that runs a simulation. At each time step, at most two new cars are parked, each with proability probabilityToPark. In other words, 0, 1, or 2 cars will drive into the ParkingGarage to be parked at each time step.

The simulation will need to generate new cars, park them, and ask for them to be retrieved. That means that whenever a new car is parked, the simulation can't lose its reference to it. It will need to maintain a master list of parked cars. On each time step, it should cycle through that list, and check its timeToLeave. If it is time for that car to be retrieved, then the simulation should retrieve the car and remove it from the master list.

The outline of the simulation is: