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. ## Resources * [ParkingLotsDriver.java](ParkingLotsDriver.java). <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 is recommended that you create a new project and copy your lab 8 implmentations of the `ParkingLot` and `District` classes to the new project. Begin by fixing any known issues with your lab 8 submission. In place of the `ParkingDriver.java` file used in lab 8, you will need to use the [ParkingLotsDriver.java](ParkingLotsDriver.java). 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 {readOnly} </td> </tr> <tr> <td> +ParkingLot(name: String, capacity: int)<br /> +ParkingLot(capacity: int)<br /> +getMinutesClosed(): int<br /> +getName(): String<br /> +getNumberOfSpotsRemaining(): int<br /> +getPercentFull(): double<br /> +isClosed(): boolean<br /> +markVehicleEntry(timestamp: int): void<br /> +markVehicleExit(timestamp: int): void<br /> +<kbd>toString(): String</kbd> </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> -<kbd>lots: ParkingLot[]</kbd><br /> +<kbd><u>MAX_LOTS: int {readOnly} =20</u></kbd><br /> -<kbd>numLots: int</kbd> </td> </tr> <tr> <td> +<kbd>District()</kbd><br /> +<kbd>addLot(name: String, capacity: int): int</kbd><br /> +<kbd>getLot(index: int): ParkingLot</kbd><br /> +getMinutesClosed(): int<br /> +getNumberOfSpotsRemaining(): int<br /> +isClosed(): boolean<br /> +markVehicleEntry(lotNumber: int, timestamp: int): void<br /> +markVehicleExit(lotNumber: int, timestamp: int): void<br /> +<kbd>toString(): String</kbd> </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 (if available) * 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` class using a pattern of `#.#` can be used to format the numerical percentage. (See [this Taylorial page](https://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. Specifically: * Update all `District` documentation to match the new behavior. * Replace the old `District` constructor with the no-argument constructor below. ``` public District() { lots = new ParkingLot[MAX_LOTS]; } ``` * Declare a class constant called `MAX_LOTS`. Set it to 20. * Replace the declaration for `lot1`, `lot2`, and `lot3` with an array of `ParkingLot`s called `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`, `markVehicleExit`, `getNumberOfSpotsRemaining`, and `isClosed` so they use `getLot` to retrieve the approriate parking lot. * 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: &nbsp;&nbsp;Status for purple parking lot: 8 vehicles (CLOSED) &nbsp;&nbsp;Status for gold parking lot: 20 vehicles (33.3%) </pre> Here you should call `ParkingLot.toString()` for each line, prepending two spaces and appending `"\n"` for the newline. There should be a newline at the end of each line including the last. ### Testing and Sample Output The `ParkingLotsDriver` 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> Testing Small Lot... Status for Blacktop parking lot: 0 vehicles (0%) Finished Testing Small Lot Testing Overfilling a Lot... Finished Testing Overfilling a Lot More Complete Test of Parking Lot... Testing ParkingLot Status for test parking lot: 3 vehicles (75%) Status for test parking lot: 4 vehicles (CLOSED) Status for test parking lot: 2 vehicles (50%) Status for test parking lot: 0 vehicles (0%) Final status: Status for test parking lot: 0 vehicles (0%) Finished More Complete Test of Parking Lot Testing Coming and Going... Finished Testing Coming and Going Testing Tiny District... Tiny District status: &nbsp;&nbsp;Status for Red parking lot: 0 vehicles (0%) &nbsp;&nbsp;Status for Green parking lot: 0 vehicles (0%) &nbsp;&nbsp;Status for Blue parking lot: 0 vehicles (0%) District status: &nbsp;&nbsp;Status for Red parking lot: 1 vehicles (CLOSED) &nbsp;&nbsp;Status for Green parking lot: 0 vehicles (0%) &nbsp;&nbsp;Status for Blue parking lot: 2 vehicles (CLOSED) Final Tiny District status: &nbsp;&nbsp;Status for Red parking lot: 0 vehicles (0%) &nbsp;&nbsp;Status for Green parking lot: 1 vehicles (CLOSED) &nbsp;&nbsp;Status for Blue parking lot: 2 vehicles (CLOSED) Lots were closed for 3 min. in tiny district. Finished Testing Tiny District Finished Testing Normal District... Airport at timestamp 7: District status: &nbsp;&nbsp;Status for Brown parking lot: 7 vehicles (70%) &nbsp;&nbsp;Status for Green parking lot: 14 vehicles (CLOSED) &nbsp;&nbsp;Status for Black parking lot: 7 vehicles (58.3%) Airport at timestamp 8: District status: &nbsp;&nbsp;Status for Brown parking lot: 8 vehicles (CLOSED) &nbsp;&nbsp;Status for Green parking lot: 14 vehicles (CLOSED) &nbsp;&nbsp;Status for Black parking lot: 7 vehicles (58.3%) Airport at timestamp 10: District status: &nbsp;&nbsp;Status for Brown parking lot: 8 vehicles (CLOSED) &nbsp;&nbsp;Status for Green parking lot: 14 vehicles (CLOSED) &nbsp;&nbsp;Status for Black parking lot: 10 vehicles (CLOSED) Finished Testing Normal District Finished Testing Heavily Used District... At end of day, all lots closed 42 min. Final District status: &nbsp;&nbsp;Status for Pink parking lot: 17 vehicles (68%) &nbsp;&nbsp;Status for Blue parking lot: 28 vehicles (CLOSED) &nbsp;&nbsp;Status for Gray parking lot: 2 vehicles (20%) Finished Testing Heavily Used District All tests finished. </pre> ## 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>

Wednesday, 06-Nov-2019 09:14:02 EST