You are on page 1of 2

COMP163 Snowfall Each Winter

Farmers, gardeners, and city planners all find it useful to know how much snow falls each winter. For this
assignment you are to write a program that reads a file created with data from the National Climatic Data
Center to add up the snow that falls from December 1 through March 31. Additionally, print the dates of
the first and last snowfall. The file gsoweather.dat contains several years of Greensboro daily
weather data sorted by increasing date. Each line in the file represents one day’s weather and contains the
values:

year date of this weather record


month date of this weather record from 1 to 12
day date of this weather record from 1 to 31
precipitation amount of precipitation on this day
snow amount of snow that fell on this day in tenths of a millimeter.
daily high highest temperature recorded on this day in tenths of a degree Celsius
daily low lowest temperature recorded on this day in tenths of a degree Celsius

All values are integers. The program only requires the year, month, day and snow amount, although your
program must read all data values in each line. Please divide the snow amount by 100 to get centimeters.

The first year in the file starts with January 1, and the last year ends with December 31. So there are two
partial winters in the file: one at the beginning and one at the end. You should ignore these two years.

a. [90 points] Write a program that displays the date of the first snow, the date of the last snow, and the
amount of snow for each winter in the file. Winter is from December 1 through March 31.
– Please print the dates numerically as month/day/year.
– Print the snow amounts as centimeters with a decimal point for the fraction.
– If no snow falls during a winter, print the year (of end of winter in March) and say “No snow.”

Implementation note: you can use a boolean variable isWinter to detect the start of winter on
December 1. The variable isWinter is intialized to false, it becomes true on Dec 1, and
becomes false again on March 31. The data file starts on Jan 1, 1980. The first winter printed
by your program will be the one that runs Dec 1, 1980 to March 31, 1981.

isWinter = false;
while (scanner.hasNext()) {
// Read year, month, day, precip, snow, high, low
year = scanner.nextInt();
// ... etc
// At the start of winter flip the boolean to true
if (month == 12 && day == 1) isWinter = true;
// Process data during winter...
if (isWinter) {
// if there is snow:
// accumulate total
// remember date of first snow
// remember date of last snowfall
// check for last day of winter
if (month == 3 && day == 31) {
isWinter = false;
// print data for this winter that just ended
}

page 1 of 2
COMP163 Snowfall Each Winter

b. [10 pts.] Compute the average snowfall per winter. Use the snow totals from part a, including the years where
there is no snow.

Example output
First snow=12/27/1980 Last snow=3/23/1981 Total snowfall=26.7 cm.
First snow=12/21/1981 Last snow=2/27/1982 Total snowfall=34.8 cm.
First snow=12/12/1982 Last snow=2/11/1983 Total snowfall=16.0 cm.
First snow=2/6/1984 Last snow=2/6/1984 Total snowfall=9.7 cm.
First snow=1/17/1985 Last snow=1/28/1985 Total snowfall=13.0 cm.
First snow=12/20/1985 Last snow=2/14/1986 Total snowfall=3.6 cm.
First snow=1/1/1987 Last snow=3/12/1987 Total snowfall=59.4 cm.
First snow=1/7/1988 Last snow=1/7/1988 Total snowfall=21.8 cm.
First snow=12/9/1988 Last snow=2/24/1989 Total snowfall=34.5 cm.
First snow=12/8/1989 Last snow=12/13/1989 Total snowfall=6.4 cm.
Winter 1991 no snow.
Winter 1992 no snow.
First snow=2/25/1993 Last snow=3/13/1993 Total snowfall=17.5 cm.
First snow=12/23/1993 Last snow=3/3/1994 Total snowfall=8.9 cm.
... etc ...
First snow=1/17/2016 Last snow=2/14/2016 Total snowfall=11.5 cm.
First snow=1/6/2017 Last snow=1/7/2017 Total snowfall=20.3 cm.
Average snow per winter=18.654054054054054 cm

page 2 of 2

You might also like