Loops, Functions, Files: Putting it all Together (weather)
CS 161 - Spring 2008 - Dr. Hutchens

Due: March 24, 2008

Goals
- to read sets of data separated by sentinel values
- to combine several programming concepts in a well-constructed program
- to read input from a file rather than typing data at the keyboard

Overall Specification
For this lab, you will create a Java program that summarizes weather information obtained from a weather station. The program will read a file name from standard input and then read the weather information from the file with that name. If the file does not exist, the program should exit gracefully with a message.

The program will process several lines of information where each line of three real values represents one day. A day's data will be a low temperature, a high temperature, and a precipitation total. Sets of data representing a reporting period (a week, a month) will be separated by a sentinel day of data. For each reporting period, your program will output the number of days in the reporting period, the maximum high and minimum low temperatures, the mean (average) high and low temperatures, total precipitation during the period, and number of days with precipitation.

This program uses loops, conditions, and functions. It will take time to develop. You should not put it off until the night before it is due. Develop it in small pieces a little bit at a time. You must use functions and parameters appropriately for part of this. The function to process a reporting period will get a little long.

Input Specification
The input consists of three values per line: low temperature (integer), high temperature (integer), and precipitation (double). Each line corresponds to one reporting day. The input will be terminated by end-of-file. Sets of data will be separated by a sentinel line as noted below.

Temperatures are in Fahrenheit. Precipitation is in inches. Precipitation will be non-negative. The file will be terminated by end-of-file, but there will be no partial lines. Sets of data representing a reporting period (a week, a month) will be separated by a sentinel line that contains a minimum temperature (first value) of -500; absolute zero is about -459°F, so -500 is an illegal value. This line will have only one integer value rather than three. The data will be clean with any day's low no bigger than its high.
 

Output Specification
The output will be a summary of the weather information for each reporting period. You do not need to create an overall summary. The summaries are for each reporting period. You should NOT echo the input data nor prompt for input, other than the file name which you read from standard input.

Print any real values with one digit after the decimal point. The output will be:
- the number of reporting days (integer); if zero days, say so and don't report other info
- the maximum high and minimum low temperatures (integer)
- the mean (average) high and low temperatures (double)
- the total rain in the period (double)
- the number of days with rain (integer)

Print a blank line before each reporting period summary.

Approaching the Problem
Develop your solution incrementally. Start very simply. Don't do too much. My first iteration is about 30 lines of code with comments.

Create a project with a class for the code. Create a New File, not a new class, for the data. I would suggest using a .data extension, but it isn't required.

I suggest the following initial iterations:
- ask for and get the file name and create a scanner on it.
- create methods to process the file and to process one period from the file.
- set up loops to read the data, identify and handle the sentinels, and count the number of days;
- total the rain
- count days with rain
- compute means
- determine maximum high and minimum low; consider adjusting extremes function

Sample Input and Output

Data File:
45 60 0
65 70 1.3
55 75 0.4
-500
-500
78 89 0
72 91 0
45 75 0
-3 34 0

Output:

Reported Days: 3
Rainy Days: 2
Total Precipitation: 1.7
Average Low: 55.0
Average High: 68.3
Minimum Low: 45
Maximum High: 75

No days in reporting period.

Reported Days: 4
Rainy Days: 0
Total Precipitation: 0.0
Average Low: 48.0
Average High: 72.2
Minimum Low: -3
Maximum High: 91