Beginning either from your own solution, or the sample solution,
modify the quadratic equation solver from Homework 04 in two ways:
a) If the equation has complex roots (that is, if the discriminant is
negative), tell the user so, and prompt for new coefficients. Repeat
this process until they enter coefficients you can work with.
b) After solving the equation, ask them if they want to solve another one. If they say
yes or sure, loop back to the beginning of the program.
Your new program should be named Quadratic2.
In order to determine whether the timing on the traffic light at Foothill
and Dartmouth is set right, the streets department is trying to
determine the traffic patterns on Dartmouth Avenue in front of the
Olin Building. To that end they have installed a vehicle sensor
across the northbound lane, which they have been turning on for
random intervals 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
one minute after the sensor 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.
A typical data set might be: 2 2 1 1 2 3 2 2 1 3 1 1 1 2 3 1 1 0
You are to implement a program named Traffic 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.
- The total number of vehicles recorded, 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.
Here is a sample run:
Please enter signal data from traffic sensor below:
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 till a full
minute has passed.
One way to solve this problem is to think of the input as consisting
of a series of ones followed by a series of twos and threes followed
by a series of ones, and so on. Therefore, you could use one while
loop to determine whether the survey has ended. Inside that you have
a pair of while loops to read first a sequence of `1's and then a
(possibly empty) sequence of `2's and '3's.
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
HMCSupport.in.nextInt. Also, note that it is possible to
read a value from the keyboard into a variable, and test the value
that was read, all in a single expression. To read an integer into the
variable x and check if the value read was 0, you would
write:
((x = HMCSupport.in.nextInt()) == 0)
I have written a sample solution for the assignment. You should try
to make your program mimic the behavior of the sample solution as
closely as possible. To try the sample solution, click here.
If you want to try it more than once (to test different behaviors)
just use the "reload" option on the page that pops up for the
program.