CS 35
Program 2
due Tuesday 6 Feb at midnight
The overall idea is this: We have a gas station with two pumps. In any
given minute, with probability p, a driver comes along who wants
gas. If a pump is available the car will pull in and buy gas. If the
line for a pump is longer than 2 cars, the car will go to another
gas station and be a customer lost. If the car wants gas and there are
2 or fewer cars in the line for a pump, the car will get in line.
We want to simulate the operation of this gas station for m minutes
and be able to output:
the number of customers served
the number of customers lost
the average time in a waiting line (per customer fully served).
In verbose mode we want to precede these cumulative statistics
with a line for each customer which shows
customer-number, arrival-time, Yes, time-in-queue,
total-time-at-station for a customer that is fully served.
For a customer lost the line should show
customer-number, arrival-time, NO.
Assume the time it takes to service a car is uniformly distributed
between 4 and 10 minutes (inclusive at both ends).
I want you to do this using what is sometimes called a discrete
clock-driven (or time-driven)
simulation. Your program will make a clock-tick for each minute of
the simulation. At a tick, update the pump activity and queues.
Use a random number to determine if a car comes by wanting gas. If so,
use another random number to determine its service time. Then by
considering pump availability and queue length, decide how to dispose
of of this customer (e.g. lost, move to pump, move to a waiting queue).
I want you to implement the queues in a ring-buffer fashion. I'll
explain this in class. It is called a circular array in Weiss and
discussed on pp. 544-545.
Because we are learning about objects and classes, make each
customer an object of type Customer. Also make each Pump an
object of type Pump and each queue an object of type Queue.
The instance variables of an object of type Customer should
be (at least): customer-number (the order in which this
customer appeared), service-time, and arrival time.
The Queue class should be polymorphic and provide methods
(for at least) size, isEmpty, enqueue, and dequeue. As stated
above, it should be implemented by a circular array.
You may assume that service times are integers and that when a car
finishes at a pump, the next car in line moves to the pump and starts
its service time instantly. Also queue movement is instantaneous.
For this week, the inputs to your program should be
1) the probablility, p, that a car wants gas at any given minute;
2) the number of minutes to run the simulation;
3) whether the output should be verbose or not.
If this seems too easy, you may want to parameterize other values and
consider variable queue lengths, n pumps with n separate queues and n
pumps with a common queue.
The first comment, at the beginning of your files, should have your
name first in it. Each file should have a comment giving its
file name and purpose.
Send me a tarball of a directory named by the first 4-letters of your
last name. That directory should contain the Java source code for all
the files needed for a solution. It should also contain a README text
file that tells me what your program can do and how to run it. This
tarball should only contain material and files pertinent to program2.
See /home/cfk/pub/cs35/week1/tarballREADME for an example of
building a tarball.
I should receive the email with tarball attached before midnight
on the due date for a program.
Here are some runs of my programs. The first run comes from
my early efforts when I jsut set variables rather than reading
them in. It is for one pump with one queue.
Script started on Thu Feb 1 16:23:03 2007
You have mail.
dill% java Trystuff
num arr served? inq tottime
1 1 Yes 0 6
6 10 NO
7 11 NO
2 2 Yes 5 14
3 3 Yes 13 21
4 7 Yes 17 22
11 30 NO
5 8 Yes 21 25
13 36 NO
14 37 NO
8 24 Yes 9 16
9 25 Yes 15 19
17 45 NO
18 46 NO
19 47 NO
20 50 NO
21 51 NO
10 29 Yes 15 23
23 54 NO
24 56 NO
25 57 NO
26 58 NO
27 59 NO
12 35 Yes 17 27
29 64 NO
30 66 NO
31 67 NO
15 43 Yes 19 26
33 74 NO
34 75 NO
35 76 NO
16 44 Yes 25 35
37 83 NO
22 53 Yes 26 31
39 85 NO
40 87 NO
28 63 Yes 21 25
42 93 NO
43 94 NO
44 95 NO
45 96 NO
32 69 Yes 19 28
47 100 NO
Running the simulation for 100 minutes with 1 pump and 0.5 probablility
that a passing car wants gas in any given minute,
We served 14 customers.
We lost 29 customers.
Total queue time = 222
So the average time waiting in line was 15.857142 minutes per customer served.
dill% ^Dexit
Script done on Thu Feb 1 16:23:46 2007
After I had a one pump version working satisfactorily and it
gave me reasonable output with different parameters, I
implemented a two pump, two queue version. Below are some
runs from my two pump version. We learn from out MISTAKES.
Well, maybe it is not a mistake, maybe it is a feature illustrating
interesting queue behavior.
But after looking at my output,
I realized that my simulation used somewhat unrealistic
behavior for customers when both pumps were full. See if
you can discover it.
Script started on Thu Feb 1 16:25:48 2007
dill% java Gassim
Enter the number of minutes to simulate: 100
Enter the probablility that a passing car wants gas in any given minute (0.0-1.0): .5
Do you want verbose output? (y or n): n
Running the simulation for 100 minutes with 2 pumps and
0.5 probablility that a passing car wants gas in
any given minute,
we served 27 customers.
We lost 16 customers.
Total queue time = 384
So the average time waiting in line was 14.222222 minutes per customer served.
dill% java Gassim
Enter the number of minutes to simulate: .5 100
Enter the probablility that a passing car wants gas in any given minute (0.0-1.0): .2
Do you want verbose output? (y or n): n
Running the simulation for 100 minutes with 2 pumps and
0.2 probablility that a passing car wants gas in
any given minute,
we served 16 customers.
We lost 0 customers.
Total queue time = 24
So the average time waiting in line was 1.5 minutes per customer served.
dill% java Gassim
Enter the number of minutes to simulate: 100
Enter the probablility that a passing car wants gas in any given minute (0.0-1.0): .2
Do you want verbose output? (y or n): n
Running the simulation for 100 minutes with 2 pumps and
0.2 probablility that a passing car wants gas in
any given minute,
we served 16 customers.
We lost 0 customers.
Total queue time = 28
So the average time waiting in line was 1.75 minutes per customer served.
dill% java Gassim
Enter the number of minutes to simulate: 100
Enter the probablility that a passing car wants gas in any given minute (0.0-1.0): .3
Do you want verbose output? (y or n): n
Running the simulation for 100 minutes with 2 pumps and
0.3 probablility that a passing car wants gas in
any given minute,
we served 25 customers.
We lost 6 customers.
Total queue time = 275
So the average time waiting in line was 11.0 minutes per customer served.
dill% java Gassim
Enter the number of minutes to simulate: 100
Enter the probablility that a passing car wants gas in any given minute (0.0-1.0): .3
Do you want verbose output? (y or n): y
num arr served? inq tottime
1 6 Yes 0 7
2 8 Yes 0 7
3 9 Yes 4 10
4 16 Yes 0 8
5 18 Yes 1 10
6 21 Yes 7 11
7 22 Yes 10 20
8 36 Yes 0 6
9 37 Yes 5 11
11 42 Yes 0 9
10 40 Yes 8 16
15 49 Yes 2 7
16 52 Yes 4 9
17 55 Yes 6 10
12 43 Yes 13 23
24 69 NO
25 70 NO
26 71 NO
19 60 Yes 5 13
13 44 Yes 22 30
20 63 Yes 10 15
30 82 NO
14 48 Yes 26 35
22 67 Yes 11 20
33 89 NO
23 68 Yes 19 23
18 56 Yes 27 36
21 66 Yes 26 30
28 78 Yes 13 18
Running the simulation for 100 minutes with 2 pumps and
0.3 probablility that a passing car wants gas in
any given minute,
we served 24 customers.
We lost 5 customers.
Total queue time = 219
So the average time waiting in line was 9.125 minutes per customer served.
dill% ^Dexit
Script done on Thu Feb 1 16:28:55 2007