Java Reference
In-Depth Information
Programming Tip: Methods such as getFront and dequeue must behave reasonably
when the queue is empty. Here, we specify that they return null . Another possibility is to
have them throw an exception.
A Problem Solved: Simulating a Waiting Line
In many everyday situations, you will wait in a line. Whether the line is at a store, a ticket win-
dow, or a car wash, a line behaves like the ADT queue. The person at the front of the line is
served first; newcomers go to the back of the line, as Figure 10-3 shows. In this problem, we
will perform a computer simulation of a waiting line.
FIGURE 10-3
A line, or queue, of people
10.5
Most businesses are concerned with the time that their customers must wait for service. A short wait
time enables an organization to increase customer satisfaction, serve more people, and make more
money. If two agents serve one line, you will wait less time than if only one agent is on duty. A busi-
ness, however, does not want to employ more people than necessary. And a car wash certainly would
not build an additional service bay to test its effect on the time its customers wait in a single line.
Computer simulation of a real-world situation is a common way to test various business sce-
narios. In this example, we will simulate one line of people waiting for service from one agent.
Customers arrive at different intervals and require various times to complete their transactions. One
way to achieve this variety is to assume that the events are random.
In a time-driven simulation , a counter enumerates simulated units of time—minutes, for example.
Customers arrive at random times during the simulation and enter the queue. Each customer is assigned a
random transaction time—that is, the amount of time required for the customer's transaction—that does not
exceed some arbitrary upper bound. During the simulation, the time that each customer waits in the queue
is recorded. At the conclusion of the simulation, summary statistics are generated, including the number of
customers served and the average time that each waited.
10.6
Solution design. Two kinds of objects occur in the description of this problem: the waiting line and
the customers. We can design a class for each of these.
 
 
Search WWH ::




Custom Search