Here is the input file: https://www.dropbox.com/s/5hy314zxkqqyv5u/gsoweather.dat
ID: 3704137 • Letter: H
Question
Here is the input file: https://www.dropbox.com/s/5hy314zxkqqyv5u/gsoweather.dat?dl=0
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: ycar month day precipitation date of this weather record date of this weather record from 1 to 12 date of this weather record from 1 to 31 amount of precipitation on this day 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 oest 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 [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 c -If no snow falls during a winter, print the year (of end of winter in March) and say "No snow." a rs with a decimal poin t for the fraction. 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 Ealse 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 iskinterfalse while (scanner.hasNexto) // Read year, month, day, precip, snow, high, low year = scanner.next Int(); // 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 f (month3&&day; iswinterfalseExplanation / Answer
package January;
import java.io.*;
import java.util.*;
// Class SnowFall definition
public class SnowFall
{
// main method definition
public static void main(String[] ss)
{
// Scanner class object declared
Scanner scanner;
// To store the first snow month, date, year
int firstMonth = 0, firstDate = 0, firstYear = 0;
// To store the last snow month, date, year
int lastMonth = 0, lastDate = 0, lastYear = 0;
// To store winter status
boolean isWinter = false;
// To store total per year, and grand total for all the years
double total = 0, grandTotal = 0;
// To store data read from file
int year, month, day, precip, high, low;
// Snow counter to calculate average
int countSnow = 0;
// To store snow value read from file
double snow;
// To check for the first snow date
int flagSnow = 0;
// try block begins
try
{
// Opens the file for reading
scanner = new Scanner(new File("gsoweather.dat"));
// Reads the heading
scanner.nextLine();
// Loops till end of the file
while(scanner.hasNextInt())
{
// Reads year, month, day, precip, snow, high, and low values from file
year = scanner.nextInt();
month = scanner.nextInt();
day = scanner.nextInt();
precip = scanner.nextInt();
// Divides it by 100 to convert it to centimeters
snow = (scanner.nextDouble())/100.0;
high = scanner.nextInt();
low = scanner.nextInt();
// At the start of the winter flip the boolean to true
// Checks if the month is twelve and day is one
if(month == 12 && day == 1)
{
// Set the status to true
isWinter = true;
}
// Process the data during winter
if(isWinter)
{
// Checks if the snow value read from the file is greater than zero
if(snow > 0)
{
// Checks the flagSnow value if it is zero then the first snow fall
if(flagSnow == 0)
{
// Stores the first day, month, and year
firstDate = day;
firstMonth = month;
firstYear = year;
// Set the flagSnow value to one
flagSnow = 1;
}// End of if condition
// Calculates the total for the year
total += snow;
// Stores the last date, month, year for snow fall
lastDate = day;
lastMonth = month;
lastYear = year;
}// End of if condition
// At the end of the winter flip the boolean to true
// Checks if the month is 3 and day is 31
if(month == 3 && day == 31)
{
// Set isWinter to false
isWinter = false;
// Checks if the total is zero no snow fall in that year
if(total == 0)
System.out.print(" Winter " + year + " no snow.");
// Otherwise display the information
else
System.out.printf(" First snow = %d/%d/%d Last snow = %d/%d/%d Total = %.2f",
firstMonth, firstDate, firstYear, lastMonth, lastDate, lastYear, total);
// Calculates the grand total for all the years
grandTotal += total;
// Increase the counter for snow fall
countSnow++;
// Re set the total to zero for next year
total = 0;
// Reset the flagSnow for next year
flagSnow = 0;
}// End of inner if condition
}// End of outer if condition
}// End of while loop
// Calculates and displays average
System.out.printf(" Average snow per winter = %.2f", (grandTotal / countSnow));
}// End of try block
// Catch block to handle FileNotFoundException exception
catch(FileNotFoundException fe)
{
fe.printStackTrace();
}// End of catch block
}// End of main method
}// End of class
Sample Output:
First snow = 12/27/1980 Last snow = 3/23/1981 Total = 26.70
First snow = 12/21/1981 Last snow = 2/27/1982 Total = 34.80
First snow = 12/12/1982 Last snow = 2/11/1983 Total = 16.00
First snow = 2/6/1984 Last snow = 2/6/1984 Total = 9.70
First snow = 1/17/1985 Last snow = 1/28/1985 Total = 13.00
First snow = 12/20/1985 Last snow = 2/14/1986 Total = 3.60
First snow = 1/1/1987 Last snow = 3/12/1987 Total = 59.40
First snow = 1/7/1988 Last snow = 1/7/1988 Total = 21.80
First snow = 12/9/1988 Last snow = 2/24/1989 Total = 34.50
First snow = 12/8/1989 Last snow = 12/13/1989 Total = 6.40
Winter 1991 no snow.
Winter 1992 no snow.
First snow = 2/25/1993 Last snow = 3/13/1993 Total = 17.50
First snow = 12/23/1993 Last snow = 3/3/1994 Total = 8.90
First snow = 1/23/1995 Last snow = 3/3/1995 Total = 1.30
First snow = 1/6/1996 Last snow = 2/16/1996 Total = 43.00
First snow = 1/11/1997 Last snow = 2/13/1997 Total = 9.70
First snow = 12/29/1997 Last snow = 1/19/1998 Total = 14.00
First snow = 12/23/1998 Last snow = 2/23/1999 Total = 5.80
First snow = 1/18/2000 Last snow = 1/25/2000 Total = 38.90
First snow = 12/19/2000 Last snow = 2/22/2001 Total = 7.20
First snow = 1/2/2002 Last snow = 1/3/2002 Total = 20.80
First snow = 12/4/2002 Last snow = 3/30/2003 Total = 38.50
First snow = 12/13/2003 Last snow = 2/27/2004 Total = 47.00
First snow = 1/29/2005 Last snow = 3/17/2005 Total = 4.30
First snow = 2/9/2006 Last snow = 2/18/2006 Total = 3.30
First snow = 1/18/2007 Last snow = 2/1/2007 Total = 2.10
First snow = 1/17/2008 Last snow = 2/13/2008 Total = 10.20
First snow = 1/20/2009 Last snow = 3/2/2009 Total = 16.10
First snow = 12/18/2009 Last snow = 3/3/2010 Total = 42.30
First snow = 12/4/2010 Last snow = 2/10/2011 Total = 25.20
First snow = 2/19/2012 Last snow = 2/19/2012 Total = 4.10
First snow = 1/17/2013 Last snow = 2/22/2013 Total = 9.20
First snow = 1/21/2014 Last snow = 3/17/2014 Total = 38.80
First snow = 2/16/2015 Last snow = 2/26/2015 Total = 24.30
First snow = 1/17/2016 Last snow = 2/14/2016 Total = 11.50
First snow = 1/6/2017 Last snow = 1/7/2017 Total = 20.30
Average snow per winter = 18.65