Menu

[Solved]Cos 160 Program 9 Weather Analysis Due May 5 2019 8 00 M Many Real Programs Work Data Some Q37231479

COS 160 : Program 9 – Weather Analysis
Due: May 5, 2019 at 8:00 A.M.

Many real programs work with data. Sometimes lots of data! Thisdata is often stored in text files and must be read, parsed andconverted into values usable by your program. In this assignment,you are going to write a program to process weather data from theNational Climate Data Center. The NCDC maintains a data collectionof daily weather observations for more than 19,000 locations in theUnited States. Some of these have been recorded since the late 19thcentury.

Download the compressed file WeatherProject.zip found at

http://cs.usm.maine.edu/~briggs/webPage/c160/projects/project10/WeatherProject.zip

  1. The text file with the data,PortlandWeather1941to2017.txt
  2. A source code template you must use,AnalyzeTemperatures.java
  3. Correct output for the basic assignment,weatherOut.txt

The data file contains the daily weather data recorded at thePortland International Jetport since 1941. You are going to beanalyzing this data to look for evidence of climate change. Thestart of this file looks like this:

28124 data records
DATE       TMAX TMIN
———- —- —-
01/01/1941   38   25
01/02/1941   32   20
01/03/1941   31   22
01/04/1941   34   25
01/05/1941   32   20
01/06/1941   29    5
01/07/1941   29 -10       

Data files come in many different formats and sometimes needdifferent techniques to parse them. This file is very simple. It isorganized by line and in fixed width columns. The first three linesare a header describing the size of the file and the data columns.Each subsequent line represents a single day’s weather record. Thecolumns are:

  • DATE – The date of the observation in format MM/DD/YYYY
  • TMAX – The maximum temperature of the day in Fahrenheit
  • TMIN – The minimum temperature of the day in Fahrenheit

Part 1 (1 point) Allocate Arrays to Hold the Data

For this assignment, your program has to be namedAnalyzeTemperatures.java There is a file with thatname in the archive file associated with this project that you mustuse. It has all the output statements in it already and you shouldnot add any output statements of your own so that I may compareyour results electronically with mine. You will need to modify itto create the arrays, load the data, and calculate the results. Itwould be good for you to define methods to accomplish thesetasks.

You will be using a Scanner to process the file. The first linein the file says how many data records there are.

First create your Scanner using a constructor that takes a Fileobject that you create using the file namedPortlandWeather1941to2017.txt as you have seen in the exampleprograms that read from disk files. Then, read the number ofrecords given on the first line into an integer variable,numRecords. If we were writing a production quality program wewould test that the first token in the file is an integer, withhasNextInt, but you may assume it is. Once you have numRecords,create 5 separate integer arrays for month, day, year, tmax, andtmin of that size. These will hold all the data from the file. Thisis a lot of data, but your computer can easily hold it inmemory.

Part 2 (5 points) Read and Store the Data in your Arrays

Call your Scanner’s nextLine() method 3 times to discard theremainder of the 3 header lines. The Scanner’s position should nowbe at the beginning of the first line with temperature data on itand you are going to read and store all the data into yourarrays.

The Scanner’s default behavior is to break up data at whitespace(spaces, tabs, and newlines), but we also want to break apart thedate string which uses forward slashes to separate the month, dayand year. Use code like this to change the delimiter to alsoinclude forward slashes:

      fileScnr.useDelimiter(“[/tnr]+”);

That assumes you named your Scanner fileScnr, but you can adaptthe code if you did not. Now you can just use .nextInt() to readall five fields: month, day, year, tmax, and tmin. The Scanner willthen automatically wrap to the next line and you can continuereading and storing the entire file in your arrays.

I suggest that you either examine your arrays using the debuggeror print your arrays(only during development!) to verify that theyhave been filled with the correct values. It is very easy to getout of synch with the file when you are reading a lot of data froma file with a structured format.  

Part 3 (5 points) Long Term Averages and Extremal Values

In Program 8 you wrote methods to work on arrays. Copy thosemethods into this program and use them to calculate and print:

  • The highest temperature in tmax and the date it occurred on(use arrayMax(), arrayFirstIndexOf(), and then use that index foryour month[], day[], and year[] arrays). Your output line shouldbe

    The overall highest temperature was dd and it occurred onMM/DD/YYYY.

    filling in with the correct values.

  • The lowest temperature in tmin and the date it occurred on,displayed as above but just replacing “highest” with “lowest”
  • The average tmax displayed on a line as

    The average of all the highest temperatures was dd.dd.

    Calculate the average as a double and round to two digits after thedecimal point. (As a check, your value should be 55.61…)

  • The average tmin, adapting the display for average highestappropriately

These should be the first output that your program produces andproduce it in the order listed here.

Part 4 (5 points) Finding the starting index for each year

One aspect of climate change that has been noted in Maine isthat winters are not as cold or as long as they used to be, whichis bad for skiers, but on the other hand the growing season hasgrown longer, which is good for farmers. The growing season is thenumber of days between the last freezing day (32 or below) in thespring and the first freezing day in the fall. We are going toanalyze the minimum daily temperature data to calculate the growingseasons and see how much they have changed.

First we are going to find the starting index of each year inthe ‘year’ array. Your task for Part 4 is to create a loop thatprints:

year   starting index
1941      0
1942      365
1943      730
1944      1095
1945      1461

There are many ways to write this. The easiest is to just useyour arrayFirstIndexOf() method from Program 9.

It is considered poor programming to use specific values such as1941 and 2017 in a program because then the program would have tobe modified to process a different file. Instead use year[0] to getthe starting year and year[numRecords – 1] to get the last year. (Iused the variable numRecords for the number of data records.)

This should be the next output your program produces. Put a blankline between this table and the previous four lines of output.

Part 5 (15 points) Finding the growing seasons

To find the growing seasons, you are going to write a helpermethod, calculateGrowingSeasonDays, that takes an array, the arrayof minimum temperatures, and the starting index for the first dayof a year. It should add 180 to the starting index to get to theindex that is about halfway through the year, and then scanbackward from that position to find the latest index that has atemperature ≤ 32 and forward from the middle position to find thefirst index with a temperature ≤ 32. Once those index positionshave been determined, the number of days in the span where all thetemperatures are > 32 can be calculated. Note, the days wherethe temperature was at or below the freezing temperature of 32 arenot included in the growing season.

Once you have your growing season method working, use it in yourloop from part 4 to prints the complete list of growing seasonslike this:

year   days in growing season
1941            146
1942            140
1943            156
1944            128
1945            140

Make sure your results match exactly.

This table is the last output your program should produce. Put ablank line between the last line of the previous table and theheader line of this table.

Finally, do a little analysis of the results: The growing seasoncan vary a lot from year to year because of a late spring or earlyfall frost. How does the average growing season for the first 10years of the historical record compare to the average of the mostrecent 10 years? Manually calculate the those two averages and putthem in the comment at the top of the file, labeling them “Averagegrowing season for first ten years” and “Average growing season forlast ten years”.

What to turn in

Submit the final version of your code of yourAnalyzeTemperatures.java program, being sure to include theanalysis you did in the comment.

Expert Answer


Answer to COS 160 : Program 9 – Weather Analysis Due: May 5, 2019 at 8:00 A.M. Many real programs work with data. Sometimes lots o… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *