Overview
For this lab, you will revise your Lab 8 solution
by introducing arrays of ParkingLot
s in the District
class.
Resources
You will need to use the ParkingLotsDriver.java to test
your implementation of modified ParkingLot
and District
classes.
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.
You should create a lab 9 project using the GitHub classroom invitation
for lab 9. This project will contain a new driver class,
ParkingLotsDriver.java
. You'll need to copy your ParkingLot
and District
classes from your lab 8 project. Begin by fixing any known issues with
your lab 8 submission.
The following diagram shows the revised API that you are to implement. Detail on each change is given below.
ParkingLot |
+CLOSED_THRESHOLD: double {readOnly} = 80.0 -name: String {readOnly} |
+ParkingLot(name: String, capacity: int) +ParkingLot(capacity: int) +getMinutesClosed(): int +getName(): String +getNumberOfSpotsRemaining(): int +getPercentFull(): double +isClosed(): boolean +markVehicleEntry(timestamp: int): void +markVehicleExit(timestamp: int): void +toString(): String |
District |
-lots: ParkingLot[] +MAX_LOTS: int {readOnly} =20 -numLots: int |
+District() +addLot(name: String, capacity: int): int +getLot(index: int): ParkingLot +getMinutesClosed(): int +getNumberOfSpotsRemaining(): int +isClosed(): boolean +markVehicleEntry(lotNumber: int, timestamp: int): void +markVehicleExit(lotNumber: int, timestamp: int): void +toString(): String |
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 atoString()
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 aboveCLOSED_THRESHOLD
. TheDecimalFormat
class using a pattern of#.#
can be used to format the numerical percentage. (See this Taylorial page for some examples of how to use theDecimalFormat
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
, andlot3
with an array ofParkingLot
s calledlots
- 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 = this.numLots;
if(newIndex < MAX_LOTS) {
this.lots[newIndex] = new ParkingLot(name, capacity);
this.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 theParkingLot
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
, andisClosed
so they usegetLot
to retrieve the approriate parking lot. - Rewrite
displayStatus()
astoString()
. The value returned bytoString()
will be a string with newline characters embedded in it. For example,toString()
could return a string such as:
District status: Status for purple parking lot: 8 vehicles (CLOSED) Status for gold parking lot: 20 vehicles (33.3%)
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.
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: 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 Tiny District 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. Finished Testing Tiny District Finished Testing Normal District... Airport at timestamp 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 timestamp 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 timestamp 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) Finished Testing Normal District Finished Testing Heavily Used District... 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%) Finished Testing Heavily Used District Finished Testing Multiple District Sizes Finished Testing District Lots Validation All tests finished.
Acknowledgement
This laboratory assignment was developed by Dr. Rob Hasker and the CSC1110 instructors.