Question 181
A small attended car park uses a class ParkingLot to manage its operation. The lot has a fixed total number of spaces, set when the lot opens for the day. The class must support recording cars entering and leaving the lot, reporting capacity information, and reporting how many cars have been admitted across the entire shift.
A ParkingLot object keeps track of three pieces of information:
- the total number of spaces in the lot, set at construction and never changed afterward,
- the current number of cars parked in the lot,
- the total number of cars that have ever been admitted into the lot during the shift (a car that enters and later leaves still counts here).
Write the complete ParkingLot class. Your class must include the instance variables described above and the behaviors specified below. All instance variables must be private.
public ParkingLot(int spaces) — Constructs a new lot with the given number of total spaces. The number of cars currently parked and the total ever admitted are both 0. You may assume spaces > 0.
public boolean enter() — Attempts to admit one car. If the lot is full (the current count equals the total number of spaces), no instance variable changes and the method returns false. Otherwise the current count increases by 1, the total ever admitted increases by 1, and the method returns true.
public boolean leave() — Attempts to release one car. If the lot is empty (the current count is 0), no instance variable changes and the method returns false. Otherwise the current count decreases by 1 and the method returns true. The total ever admitted is never decreased by leave.
public int available() — Returns the number of spaces still free in the lot, computed from the total number of spaces and the current count.
public int served() — Returns the total number of cars that have been admitted during the shift.
public double occupancy() — Returns the fraction of the lot currently occupied, expressed as a value in the range 0.0 to 1.0. The fraction is computed as the current number of cars divided by the total number of spaces. Because spaces > 0 by construction, no division-by-zero check is required.
The table below shows a possible sequence of calls on a newly constructed ParkingLot named lot. Use this sequence to guide the design and behavior of your class.
| Statements and Expressions | Value Returned (blank if no value) | Comment |
|---|---|---|
ParkingLot lot = new ParkingLot(3); |
Creates a new lot with 3 spaces, all empty, no one served yet. | |
lot.available() |
3 |
All spaces are free. |
lot.enter() |
true |
1 car parked, served count becomes 1. |
lot.enter() |
true |
2 cars parked, served count becomes 2. |
lot.enter() |
true |
3 cars parked, served count becomes 3; lot is now full. |
lot.enter() |
false |
Lot is full; rejection does not affect any field. |
lot.available() |
0 |
No spaces remain. |
lot.occupancy() |
1.0 |
Three of three spaces are occupied. |
lot.leave() |
true |
2 cars parked; served count is unchanged. |
lot.leave() |
true |
1 car parked. |
lot.leave() |
true |
0 cars parked; lot is now empty. |
lot.leave() |
false |
Lot is already empty; rejection does not affect any field. |
lot.served() |
3 |
Total ever admitted across the shift is preserved. |
lot.occupancy() |
0.0 |
Zero of three spaces are occupied. |
Write the complete ParkingLot class, including instance variables, constructor, and all methods described above.
Main Solution
Sample Solution
public class ParkingLot
{
private int totalSpaces;
private int currentCars;
private int totalAdmitted;
public ParkingLot(int spaces)
{
totalSpaces = spaces;
currentCars = 0;
totalAdmitted = 0;
}
public boolean enter()
{
if (currentCars == totalSpaces)
{
return false;
}
currentCars = currentCars + 1;
totalAdmitted = totalAdmitted + 1;
return true;
}
public boolean leave()
{
if (currentCars == 0)
{
return false;
}
currentCars = currentCars - 1;
return true;
}
public int available()
{
return totalSpaces - currentCars;
}
public int served()
{
return totalAdmitted;
}
public double occupancy()
{
return currentCars * 1.0 / totalSpaces;
}
}
Scoring notes. Points are earned for: declaring three private instance variables of the right types and capturing spaces into the total field exactly once; initializing the current count and total admitted to 0 in the constructor; rejecting the call to enter when the lot is full and otherwise incrementing both running totals; rejecting the call to leave when the lot is empty and otherwise decrementing only the current count (not the lifetime total); returning the correct difference from available; returning the lifetime total from served; and returning a double ratio from occupancy by promoting one operand. Common errors that lose points include decrementing totalAdmitted in leave, returning true for a rejected enter or leave, and writing currentCars / totalSpaces in occupancy (integer division returns either 0 or 1).
Answer images

