Lab 9
Start Now
## Overview For this lab, you will revise your [Lab 8](Lab8) solution by introducing arrays of `ParkingLot`s in the `District` class. <a name="changes"></a> ## Assignment The previous version of the `District` class you delivered was limited to districts with three parking lots. This was an artificial limit, and you have been contracted to revise the code to support up to twenty parking lots per district. You are to replace the `lot1`, `lot2`, and `lot3` instance variables in `District` by an array, `lots[]`, of `ParkingLot` items. The array is to support up to 20 parking lots. You will introduce a variable `numLots` to track the number of parking lots currently in use. In addition, you will make a small modification to `ParkingLot` as described below. It may be helpful to set up your project before reading through the changes. For detailed instructions see [below](#detailed_start), but the basic steps are to copy your project folder to a new location (perhaps named `lab9`) and replace your existing `ParkingDriver.java` by the <a href="ParkingDriver.java">new</a> version. The following diagram shows the revised API that you are to implement. Detail on each change is given below. <table style="border: 2px solid black; font-family: menlo, monaco, consolas, monospace"> <tr> <td style="text-align: center">ParkingLot</td> </tr> <tr> <td> +<u>CLOSED_THRESHOLD: double {readOnly} = 80.0</u><br /> -name: String </td> </tr> <tr> <td> +ParkingLot(lotName: String, capacity: int)<br /> +ParkingLot(capacity: int)<br /> +closedMinutes(): int<br /> +getName(): String<br /> +isClosed(): boolean<br /> +markVehicleEntry(time: int): void<br /> +markVehicleExit(time: int): void<br /> +toString(): String<br /> +vehiclesInLot(): int<br /> </td> </tr> </table> <table style="border: 2px solid black; font-family: menlo, monaco, consolas, monospace"> <tr> <td style="text-align: center">District</td> </tr> <tr> <td> -lots: ParkingLot[]<br /> <u>+MAX_LOTS: int {readOnly} =20</u><br /> -numLots: int<br /> </td> </tr> <tr> <td> +District()<br /> +addLot(name: String, capacity: int): int<br /> +getLot(index: int): ParkingLot<br /> +isClosed(): boolean<br /> +markVehicleEntry(lotNumber: int, time: int): void<br /> +markVehicleExit(lotNumber: int, time: int): void<br /> +closedMinutes(): int<br /> +toString(): String<br /> +vehiclesParkedInDistrict(): int </td> </tr> </table> ### Changes to `ParkingLot` You will need to make the following changes to your `ParkingLot` class: * Update the class based on lab 8 feedback from your instructor * Rename the `color` instance variable to `name` and `getColor()` method to `getName()` using the Refactor tool in IntelliJ. * Replace the `displayStatus()` method with a `toString()` method. This method is to return a string of the form "Status for [name] parking lot: [x] vehicles ([p])" where [name] is replaced with the name of the lot, [x] is replaced with the number of vehicles currently in the lot, and [p] is replaced with the percentage of the lot that is occupied. As before, the percentage occupied is formatted with at most one digit after the decimal and "CLOSED" if the percentage is at or above `CLOSED_THRESHOLD`. The `DecimalFormat` <strong>#.#</strong> can be used to format the numerical percentage. (See [this Taylorial page](http://taylorial.com/cs1021/DecimalFormat.htm) for some examples of how to use the `DecimalFormat` class.) Having `toString()` is good practice because it allows the client code to print other places besides `System.out`. ### Changes to `District` Most of the changes are to `District`, but you might notice that the parameters and return types for existing methods do not change. The following sequence diagram illustrates how the example from Lab 8 works with the new API. <figure><a href="L9parkingUMLtinyseq.png"><img src="L9parkingUMLtinyseq.png" /></a></figure> More specifically, make the following changes to `District`. Note that you will also need to update the documentation. Whether you do this as you go or later is up to you, but doing it as you go can be easier. * Delete the old `District` constructor. * Declare a constant (a public static final value) called `MAX_LOTS`. Set it to 20. * Replace the declaration for `lot1`, `lot2`, and `lot3` by ``` private ParkingLot[] lots = new ParkingLot[MAX_LOTS]; ``` * Add an instance variable `numLots` to track the number of lots actually in use. * Add a method `addLot()` (see the class diagram) with the following code: ``` int newIndex = numLots; if(newIndex<MAX_LOTS) { lots[newIndex] = new ParkingLot(name, capacity); numLots++; } // return the index of the new lot or -1 if the lot was not added. return newIndex<MAX_LOTS ? newIndex : -1; ``` * Add a method `getLot` which returns the `ParkingLot` at the given index. Note that lot numbers now start at 0. A precondition for this method is that the lot index is valid. * Rewrite `markVehicleEntry` and `markVehicleExit` so they use `getLot` to retrieve the approriate parking lot. * Add a method `vehiclesParkedInDistrict()` that returns the total number of vehicles parked in all of the parking lots in the district. * Rewrite `isClosed()` so that it returns `true` if and only if all of the parking lots in the district are closed. * Rewrite `displayStatus()` as `toString()`. The value returned by `toString()` will be a string with newline characters embedded in it. For example, `toString()` could return a string such as: <pre> District status: Status for purple parking lot: 8 vehicles (CLOSED) Status for gold parking lot: 20 vehicles (33.3%) </pre> Here you should call `ParkingLot.toString()` for each line, appending `"\n"` for the newlines. There should be a newline at the end of each line including the last. ### Testing and Sample Output As before, `ParkingDriver` exercises most of your code. The output is given below, but remember that having slightly different numbers may be okay and that matching this output does not automatically mean your solution is correct. <pre> Status for blacktop parking lot: 0 vehicles (0%) Tiny District status: Status for red parking lot: 0 vehicles (0%) Status for green parking lot: 0 vehicles (0%) Status for blue parking lot: 0 vehicles (0%) District status: Status for red parking lot: 1 vehicles (CLOSED) Status for green parking lot: 0 vehicles (0%) Status for blue parking lot: 2 vehicles (CLOSED) Final TinyDistrict status: Status for red parking lot: 0 vehicles (0%) Status for green parking lot: 1 vehicles (CLOSED) Status for blue parking lot: 2 vehicles (CLOSED) Lots were closed for 3 min. in tiny district. Testing ParkingLot Status for test parking lot: 3 vehicles (75%) Status for test parking lot: 4 vehicles (CLOSED) Final status: Status for test parking lot: 0 vehicles (0%) Airport at time 7: District status: Status for brown parking lot: 7 vehicles (70%) Status for green parking lot: 14 vehicles (CLOSED) Status for black parking lot: 7 vehicles (58.3%) Airport at time 8: District status: Status for brown parking lot: 8 vehicles (CLOSED) Status for green parking lot: 14 vehicles (CLOSED) Status for black parking lot: 7 vehicles (58.3%) Airport at time 10: District status: Status for brown parking lot: 8 vehicles (CLOSED) Status for green parking lot: 14 vehicles (CLOSED) Status for black parking lot: 10 vehicles (CLOSED) Testing heavier usage. At end of day, all lots closed 42 min. Final District status: Status for pink parking lot: 17 vehicles (68%) Status for blue parking lot: 28 vehicles (CLOSED) Status for gray parking lot: 2 vehicles (20%) All tests finished. </pre> <a name="detailed_start"></a> ### Detailed Start A detailed list of steps to set up your project for Lab 9 for those who would like more explicit instructions. 1. Make sure your solution is working for Lab 8, especially the `ParkingLot` class. 2. Open Windows File Explorer (see [here](http://www.dummies.com/how-to/content/how-to-navigate-windows-7-with-windows-explorer.html) if you're not sure how) and navigate to the <strong>lab8</strong> folder containing your previous solution. 3. Right click on the lab8 folder and select Copy. 4. Right click on the open space below the folder and select Paste. This will create a fresh copy of the project as "lab8 - Copy". 5. Right click on "lab8 - Copy", select Rename, and enter **lab9**. 6. In Windows File Explorer, browse to **lab9\src\parking**. 7. Download [`ParkingDriver.java`](ParkingDriver.java) and replace the old version of `ParkingDriver` by the new one. 8. Start IntelliJ and open the <strong>lab9</strong> project. 9. In IntelliJ, confirm that the new `ParkingDriver` has syntax errors. If it doesn't, that means you're still using the old version - you'll need to fix that before going on. Ask for help if you need it! When you are done, go back to the [top](#changes) to see what to change. ## Acknowledgement This laboratory assignment was developed by [Dr. Rob Hasker](http://faculty-web.msoe.edu/hasker/) and the CS1011 instructors. <div class="notetip"> See your professor's instructions for details on submission guidelines and due dates. </div>

Friday, 02-Nov-2018 15:10:51 EDT