Skip to end of metadata
Go to start of metadata

The goal of this project was to create a garage in which "cars" would be stored in "garage" lanes as per a boston parking lot. A car will drive in and be parked in the closest lane. It will have a time it wants to leave, which means that all of the cars before it in the lane, if any, will be taken out before they are put back in. The garage has 5 lanes that hold up to 10 cars each.

Task 1 was to make a car object, which holds its time to leave and its color. It also had a get method to return the time to leave. I tested the class out with a main method that returned this and the color.

Task 2 was to create a data structure that would hold all of the cars. It is modeled like a stack, meaning that I first made an array that would hold these cars. I made an integer to hold the top of the array, as well, which would be incremented for every item that was pushed onto the stack, and be decremented for every item popped off the top. I made a pop and push method. They were fairly straightforward. Note that when the stack is empty, the top pointer is at 0. When I push something onto it, it is still at 0, because I had my pointer point to whatever is at the top of the list, not to the next empty place. I had to be careful with the push and pop methods because of this. For example, in pop, I checked if the top index was 0, and if it was, it would return null if it was empty, and the item in it if it wasn't. I also had to decrement the top pointer every time I popped, except at 0, because then it would be at -1. I didn't realize this and spent hours trying to figure out my mistake--thank you to Stephanie for helping me. For pop, I needed to save the item at the top in a variable, then overwrite the top with null, move the pointer down, and return the saved variable. For push, I also made it so if someone pushed an item onto a stack that was already full, it would double the size of the stack.

Task 3 was to create a "garage" class that would hold all of the lanes. This would need an array to store all of the lanes, a holding area lane for during retrieval, and a maximum lane length variable. The most notable methods here were the retrieveCar and parkCar methods. In retrieveCar, I told it to loop over all of the lanes and cars in them until it found the one matching the argument car. Once this happened, it would store the stack and index of the stack to remember the car location.Then, I wrote the following code:
This would pop the car off of the array, and store the ones before it, if any, in the holding area. Then it would actually pop off the one before it. After, if the holding area had anything in it, it would push those back into the stack they came from.
In parkCar, I told it to loop over every lane and push the car onto the correct lane

Task 4 was to make the garage visualizable. I used the display class from last week, changed a few things to make it fit this project, and then made draw methods for both the garage and Car. In CarGarage.draw(), I passed in the graphics object to draw on and the scale. I told it to call the draw method of each car in the garage with a loop. Drawing at the correct location was the tricky part. In the end, I drew each car at the given (x,y) location plus an offset that has been multiplied by the loop variable. I made the width and height have a 1:2 ratio. In the car draw method, I simply told it to get the color and use that to draw the car rectangle at the passed-in x and y.

Task 5 was just to make a retrieve car function. I talked about it already, under task 3.

Task 6 was to test out the simulation by making a ParkingSimulation class. Following the provided structure, I first looped for the number of time frames that were passed in in the main method. Then I looped twice to up to two cars could park in a time step. I also passed in the probability to park. If a randomly generated float was less than the probability to park, a new car object would be be created, and I told it to park the car. If it could not be parked, I incremented the failed number of cars. Next, I also checked once every step if there were cars whose time it was to leave the garage, and if it was, they would be pulled out. I also printed relevant information, such as how many cars tried to park, how many failed, and how many cars were retrieved.

For an extension, I also added the number of times cars were placed in the holding area during retrieval and the amount of cars that were in the lot currently. To get the amount of times cars were put in holding, I just made an integer in the car retrieval method in the garage file, and incremented it in the loop I had for popping cars before the desired one off the lane. I returned that through a method, which I called in the simulation then printed the result.

I also did the extension of annotating where memory is lost. This happens in the push and pop methods of CarStack.

Here is a gif of my garage:


In this project, I learned how to manipulate arrays to store data in ways that would still allow me to remember what was in them. I also learned how important it is to keep track of the stack size and where the top pointer is. I also had a refresher on how to draw items at certain specific places using loop variables.

Thank you to:

Melody (as usual)