CAR WASH DESIGN EXERCISE
By Autumn C. Spaulding with Alyce Brady
•
Edited by Sandino Vargas-Pérez
PROBLEM DESCRIPTION
The current car wash machine at Squeaky Clean Car Wash requires exactly 4
minutes to complete the regular car wash (including rinse, wax, and dry).
The owners are considering upgrading their equipment, though, because they
are suspicious that the average wait time for the cars is long enough that
they are losing business. They are considering replacing the current
machine with one that would take only 3 minutes. The upgrade will be
expensive, so they would like to know in advance how much difference it
will make.
Construct a simulation of the car wash to determine what the average
waiting time would be during a typical day (9 am to 7 pm) using the new
equipment. A car arrives at the car wash approximately every four minutes
(cars don't arrive exactly four minutes apart: that would be easy to
program!). Since the arrival times are uneven, there is often a waiting
line. This can be simulated using a queue.
Think about how you might go about simulating this problem. How could you
simulate the arrivals, waiting, and washing of the cars at the car wash?
Perhaps it would be useful to think of what can happen in a given minute in
the car wash. (We'll assume that at most one car can arrive in any given
minute, although this is not necessary.) In a car wash, you can have cars
arriving, cars waiting, and cars being washed. You also have cars changing
from one state to another. If a car arrives and there is a car being
washed, the arriving car becomes a waiting car. If there is at least one
car waiting and the car wash bay becomes available, a waiting car becomes a
car being washed.
Some of the questions that are still open are:
How will you determine whether a car should arrive in the current minute?
(In any given minute, there is a 1 in 4 chance that a car will arrive.)
How will you decide whether a car is currently being washed?
How will you decide when the machine is empty?
How will you represent the waiting time of a car?
How will you represent the total waiting time of all the cars for the day?
How will you represent the total time that the car wash is open?
Design Exercise 1
Identify the things (the nouns) that might be useful to represent in a
simulation program. Some might be primitive types, such as
int
or boolean
, some might be common classes such
as ArrayList
, and others might be better represented as new
classes.
⇓
(Continue to exercise)
One Possible Design
First, let's look at the things in the problem description that might turn
into objects in the implementation. The "things" (nouns) are:
car wash machine/equipment |
Squeaky Clean Car Wash |
minutes |
car |
owners |
car wash bay |
average waiting time |
lost business |
simulation |
day |
arrival time |
waiting line (queue) |
Several of these refer to the business as a whole: Squeaky Clean Car
Wash and owners. The word simulation covers
these, and is more relevant for our program. The possibility of lost
business is the reason for the simulation, but not part of it. We
are left with car wash machine and car wash bay (which
seem to refer to the same thing), car, waiting line
(queue), and several objects related to time: minutes,
day, arrival time, and average waiting time.
Let's reconsider the problem in more detail. Our primary task is to create
a simulation of the car wash for one day that calculates the average
waiting time of all the cars. To do that we need to know the total waiting
time for all the cars and the number of cars. To calculate the total
waiting time we need the individual waiting time for each car, which we
calculate from its arrival time (the current time when the car arrives) and
the current time when the car leaves the waiting line and enters the car
wash bay. Since the time a car spends in the wash bay is measured in
minutes, we might as well measure waiting times and the time that the car
wash is open (the length of the day) in minutes as well. As mentioned
above, if a car arrives every four minutes, on average, then in any given
minute there is a one in four chance of a car arriving.
This analysis has introduced several new "things" that did not appear
explicitly in the original problem description: the total waiting time for
all the cars, the number of cars, the waiting time for each individual car,
and the current time. Our new list is:
Integers: |
length of day |
current time |
arrival time |
|
individual waiting time |
total waiting time |
average waiting time |
|
number of cars |
Objects: |
simulation |
car |
waiting line (queue) |
|
car wash bay |
Since the only reason we care about individual waiting times
is to determine the total waiting time, we can avoid keeping track of
them by adding them to the total waiting time as we go.
Design Exercise 2
Come up with a design for a Car Wash Simulation project.
- Start with the consideration of possible objects
above. Is this a good list, or are there things in it that aren't
needed, or things that are missing?
- What are the
classes of objects you think would be useful?
- What are the responsibilities of each type of
object, or the actions you might want to perform
on them? (Or have them perform, to use the more anthropomorphic
perspective common in OO design?)
Remember that design is an incremental, interative process, so you the
problem would be:
⇓
(Continue to next exercise)
Acknowledgments: The behavior of the car wash
simulation in this project is based on a project description by
Todd Ollendyke.