|
| 1 | +# Car Tracking App in Terminal |
| 2 | + |
| 3 | +**Main Classes** |
| 4 | + |
| 5 | +Car.java |
| 6 | + This class contains self-explanatory set and get methods, mainly, that will be used by the CarTracker application. |
| 7 | + |
| 8 | +CarTracker.java |
| 9 | + The main class that has a user interface and does the tracking. |
| 10 | + |
| 11 | +IndexMinPQ.java |
| 12 | + Minimal Priority Queue that is used for sorting cars. |
| 13 | + |
| 14 | +StdOut.java |
| 15 | + Standard Java class that allows output from java program to terminal screen. |
| 16 | + |
| 17 | + |
| 18 | +**Explanation of Creation** |
| 19 | + |
| 20 | + I first began by designing a Car data structure class called "Car.java" which has basic setters and getters. The notable part of this class is that |
| 21 | +I use a flag called comparingPrice. This flag is set when cars are initialized and before they are put into IndexMinPQ's in the CarTracker.java. The reason is |
| 22 | +because I use two separate cars for each Car that is entered into the program. One of the cars has the flag comparingPrice set to true and the other one to false. |
| 23 | +This way the compareTo() method in Car.java knows which parameter to compare to return the relevant answer. |
| 24 | + |
| 25 | + In CarTracker.java I used a mileagePQ and a pricePQ which I gave a max size of 100000 just in case the input file was very big. These PQ's will keep the |
| 26 | +minimum priority of mileage and price, respectively. I used an integer variable, index, which kept track of the index that I would give a new car that I added. |
| 27 | +To allow for quick accesses of cars I kept a HashMap that mapped VIN Strings to their integer indexes so that I could retrieve the car from the PQ with that index, |
| 28 | +quickly. Then I kept two HashMap's related to Make and Model combinations of cars. They mapped the String of the Make and Model together, without spaces, to an |
| 29 | +IndexMinPQ, either comparing price or mileage, which is just for a specific model and make. |
| 30 | + |
| 31 | + Here I will prove that retrieving the car with minimum mileage/price is logarithmic or better. I will also prove that all updates and removals are |
| 32 | +logarithmic or better. Before we can do any of the above operations, we must first enter cars into our data structure. This was done with one simple call |
| 33 | +to insert(int index) in IndexMinPQ.java. According to the documentation of IndexMinPQ.java, this operation is logarithmic. Now that we have the cars in the data |
| 34 | +structure, we can retrieve the car with either minimum mileage or price by calling the minKey() method on the correct IndexMinPQ<Car>. Once again according to |
| 35 | +the documentation in IndexMinPQ.java, the minKey() operation is logarithmic. So this means retrievals for either attribute are logarithmic. For retrieval of |
| 36 | +an attribute on a specific make and model, we first ask the user for the make and model. Then we take the make and model (key) and retrieve the |
| 37 | +IndexMinPQ<Car> (value). This is an O(1) operation. Once we have this IndexMinPQ<Car> that is specific to our make and model, we can call minKey(), which is |
| 38 | +constant. Two constant operations [O(1) + O(1)] is still constant, O(1). To update a car, the user is asked what the VIN number is. Assuming a valid VIN is passed |
| 39 | +in, we can find the index in our indirection table that corresponds to that object. On average, this is an O(1) constant operation with a HashMap. Once we have |
| 40 | +the index, we delete the old version of the car which is O(log(n)), according to IndexMinPQ.java, and then re-add the new version which is the insert() method. |
| 41 | +This is also O(log(n)). That means updating a car costs O(1)+O(log(n)) + O(log(n)) which simplifies to O(log(n)). Removing a car is done very similarly with |
| 42 | +an O(1) HashMap look-up given the VIN. Then we call the delete(int index) function in IndexMinPQ.java which according to the documentation is O(log(n)). That |
| 43 | +means removing a car has an average complexity of O(1) + O(log(n)) which simplifies to O(log(n)). Therefore, all the operations involved in this project function |
| 44 | +in logarithmic time or better. |
| 45 | + |
| 46 | + To make sure that the major operations of CarTracker.java were logarithmic time or better, more memory was used. The memory requirements of this program |
| 47 | +are relatively high. To take into account the major contributors to the memory overhead, we will ignore minor variables such as int index and BufferedReader reader, |
| 48 | +which consume a minimal amount of memory as compared to the other data structures. The mileagePQ and pricePQ which are IndexMinPQ's of type Car used a memory |
| 49 | +overhead of "one million". This is to say that they used up quite a lot of memory but it is hard to say in bytes how much that is. If n equals the size of the input |
| 50 | +data, then in most cases this term is greater than n (1000000>n). There are two of these so we have at MINIMUM Omega(2n) in memory right there. After this, we have |
| 51 | +a HashMap known as vinMap which contains the VIN String and int Index that is associated with it. This is O(n). After this, we have two more HashMap's that kept |
| 52 | +the make and model as the key and an IndexMinPQ<Car> as their value that either used mileage or price to compare. This means that in total this is another O(2n). |
| 53 | +Overall, this would mean that at a MINIMUM, as a tight lower bound, the memory used by this program is Omega(5n). This is quite a lot of memory and it is hard |
| 54 | +to give a tight upper bound or even average. However, the work of this paragraph shows that this has to be a minimum. Just to make it clear, I would like to state that |
| 55 | +I have been considering memory used by this program in terms of how much physical space it would take to run. If I was to just look at the memory usage as a function |
| 56 | +of input size, then we can say that this program uses Theta(5n) memory. |
| 57 | + |
| 58 | + |
| 59 | +**Contact Information** |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +Yogindra Raghav (YogiOnBioinformatics) |
| 64 | + |
| 65 | +yraghav97@gmail.com |
0 commit comments