CS 120 Lab
12
Loops From Scratch
Tom Linton, http://www.cs.moravian.edu/~linton
Moravian College, Spring 1999
Work alone on this lab.
All questions should be directed to either Tom
or Ming.
In order to determine whether the timing on the traffic light at Main
and Elizabeth is set right, the streets department is trying to determine
the traffic patterns on Main Street in front of Comenius Hall. To that
end they have installed a vehicle sensor across the northbound lane, which
they have been turning on for random intervals (an integer number of minutes)
each day.
Whenever the sensor is turned on, it begins transmitting a steady stream
of numbers. So that the traffic department can tell if it is working, regardless
of the traffic, it transmits the number '1' at one-minute intervals after
it turns on. The first '1' is transmitted after one minute has passed (as
opposed to the instant it is turned on). Each time a vehicle passes over
the sensor, it transmits a '2' if it is a car, and a '3' if it is a truck
(based on weight assumptions). At the end of the survey it transmits a
zero and is immediately turned off.
A typical data set might be: 2 2 1 1 2 3 2 2 1 3 1 1 1 2 3 1 1
0,
indicating that two cars passed by in the first minute, nothing passed
the second minute, then a car, a truck and 2 cars passed in the third minute,
and so on. You are to implement a program that will read the signals from
a single survey as they come in and output a report giving the following
information:
-
The length of the survey period in minutes (round up, see below).
-
The total number of vehicles recorded, also broken down by category.
-
The length (in minutes) of the longest interval between vehicles.
-
The largest number of vehicles to pass in a single minute.
The term minute, in the last phrase above, should be interpreted
as between two successive ones. Furthermore,
if the second to last entry is NOT a one, you should consider the total
time to be the same total time you would get by adding a one, as the second
to last entry. That is, 1 2 2 3 1 2 0 counts the same as 1 2 2 3 1 2 1
0, i.e. 3 minutes. Here is a sample run. Between the first and second line
of output, various SimpleGUI methods will print various prompts and responses,
but the "output" should end with something similar to the lines after the
last ...
Please enter signal data from traffic sensor:
...various user-program IO prompts and responses...
user input is: 2 2 1 1 2 3 2 2 1 3 1 1 1 2 3 1 1 0
Length of survey period was 8 minutes.
Total number of vehicles recorded was 9: 6 cars and 3 trucks.
Most full minutes to pass without vehicles was 2 minutes.
Largest number of vehicles to pass in a single minute was 4.
Notice that though there is a string of three 1s, the longest time without
a vehicle is two minutes, since the first 1, in the string of three, signals
the end of a minute already in progress, we can't say that a whole minute
has passed without a car yet. But, be careful, if the input starts with
a series of 1's, then the first 1 in that first series does count, since
we know we won't see the first 1 until a full minute has passed.
One way to solve this problem is to think of the input as consisting
of a (possibly empty) series of ones followed by a (possibly empty) series
of twos and threes, followed by a series of (one or more) ones, followed
by a (possibly empty) series of twos and threes, and so on, until a zero
is read. Therefore, you could use one while loop to determine whether the
survey has ended. Inside that while loop, you have a pair of while loops
to read first a sequence of '1's and then a sequence of '2's and '3's.
Following this template, where, for example, you keep reading ones until
you get something that isn't a one, makes the last two quantities easier
to calculate.
Note that, even though there is only a single prompt before the stream
of data is entered, each number is being read by a separate call to some
SimpleGUI input method, like the three argument version of getInt,
or getChoice, corresponding to a menu. Your output MUST reproduce
the (valid) traffic signal input by the user (i.e. the third line in the
sample above).
Your well documented program should be submitted using grade.
Name your file Traffic.java. If you have trouble figuring out
how to start, consider the following partial steps toward a solution.
-
Get the input part of the program to function correctly. You might try
to set up a loop which simply asks the user for an integer from 0 to 3,
echos back whatever integer they type in, until they enter a zero, whence
your program stops. You'll be better off if you use "hard coded" displayResult
calls, as in 4 separate "printing lines" similar to
displayResult("You entered a zero");
as opposed to a single "variable echo line" like
displayResult("You entered a " + numberEntered);
-
Next, add code which keeps track of the total number of ones entered. Whenever
the user enters a one, increase the counter being used to keep track of
the number of ones. If you get this to work, tracking the total number
of cars and trucks should be straightforward.
-
Keeping track of "the most" or "the longest" or "the least", normally requires
two datafields, one to hold the "best so far" and one to hold the "current
count". When the current period ends, you compare the current count to
the best so far, and save the current value if it is better.
If you don't understand how a "loop for ones" then a "loop for twos and
threes" provides a manner to answer the last 2 questions, you might try
a loop which reads any digit, and responds differently to each. In this
case:
-
You'll probably want to use some boolean variables to keep track of what's
been happening recently. For example, you might use one called noVehiclePeriod,
which is true during intervals where no vehichles have passed.
-
It will pay to think about what might change as you read each possible
value. For example, if you just read a two, you clearly need to increase
the car counter and the total vehichle counter. You may have just ended
an interval of "no vehichles", and, since you don't know whether this car
is the last vehichle you'll see this minute, you probably don't need to
worry about whether or not you're in the middle of the minute with the
most vehichles or not (at least not right now). The more counters and boolean
variables you introduce, the more updating you need to do with each signal
datapoint.