COMP163 Average Day of First Frost Farmers need to harvest their crops early eno
ID: 3591621 • Letter: C
Question
COMP163 Average Day of First Frost
Farmers need to harvest their crops early enough to avoid a killing frost. For this assignment you are to write a program that reads a file created with data from the National Climatic Data Center to determine the average first day in the fall that a frost occurs. The file weatherDaily.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
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 daily low, although your program must read all data values in each line.
a. [90 points] Write a program that displays the date of the first autumn frost for each year in the file. We define an autumn frost as any day when the temperature is less than zero degrees Celsius. Fall frosts occur after July. The first day of frost is when the daily low is less than zero and the month is greater than 7. After you find the first day of frost, you need to skip the rest of the days until December 31. You can use a boolean variable to indicate that a frost has already been found.
page 1 of 2
COMP163 Average Day of First Frost
b. [10 points] Enhance your program to display the average last frost day of the year. You can use the following methods to convert the year, month and day to day of the year. Your program will also have to count the number of years in the file. You can display the average day of the year and then use that with the monthDay method to get the calendar day.
/* Day of the year for first of month */
static final int[] monthSum = {0, 31, 59, 90, 120, 151, 181, 212,
243, 273, 304, 334, 366};
/**
* Convert year, month and day to day of the year.
* @param y year
* @param m month
* @param d day
*/
static int dayOfYear( int y, int m, int d ) { int doy = d + monthSum[m-1]; // simple day of the year if (y % 4 == 0 && m > 2) { // if leap year and after Feb 29 doy++; // add one to day for leap day
}
return doy;
}
/**
* Convert day of the year to month / day
* @param dday day of the year
*/
static String monthDay( double dday ) {
int day = (int)(dday + 0.5); // round off to integer day for (int month = 1; month <= 12; month++) { if (day <= monthSum[month]) {
return month +"/"+ (day - monthSum[month-1]);
} }
return "?/?";
}
Example output
First frost in 1974 was on 10/4
First frost in 1975 was on 10/31
First frost in 1976 was on 10/19
First frost in 1977 was on 10/18
. . .
First frost in 2013 was on 10/26 First frost in 2014 was on 11/3
First frost in 2015 was on 11/15
First frost in 2016 was on 11/13
Average first frost is the 307.8837209302326 day of the year Average first frost is on 11/4
Explanation / Answer
//FirstFrost.java file
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class FirstFrost {
public static void main(String args[]) {
displayFirstFrostDay();
}
public static void displayFirstFrostDay() {
HashMap<Integer, Integer> firstfrosts = new HashMap<Integer, Integer>();
File inFile = new File("weatherDaily.dat");
try {
Scanner scanner = new Scanner(inFile);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] splitted_line = line.split(" ");
int year = Integer.parseInt(splitted_line[0]);
int month = Integer.parseInt(splitted_line[1]);
int day = Integer.parseInt(splitted_line[2]);
int prec = Integer.parseInt(splitted_line[3]);
int snow = Integer.parseInt(splitted_line[4]);
int hightemp = Integer.parseInt(splitted_line[5]);
int lowtemp = Integer.parseInt(splitted_line[6]);
if (month > 7) { /* after july */
if (lowtemp < 0) { /* if temperature is less than 0 */
if (!firstfrosts.containsKey(year)) { /*if the year's first frost day is not added to the hashmap yet,add it*/
int d = dayOfYear(year, month, day); /* getting the day of the year */
firstfrosts.put(year, d); /* adding to the map */
}
}
}
}
int temp=0;
/*
* we have the results in the hashmap; but hashmap must be sorted to
* display in the right order. (HashMap doesn't keep the order)
*/
TreeMap<Integer, Integer> sorted = new TreeMap<Integer, Integer>(firstfrosts);
Set<Entry<Integer, Integer>> mappings = sorted.entrySet();
for (Entry<Integer, Integer> entry : mappings) {
System.out.println("First frost in " + entry.getKey()+ " was on " + monthDay(entry.getValue())); //displaying the first frost days of every year
temp=temp+entry.getValue(); // helps to find the average
}
double average_frost_day=temp/firstfrosts.size(); /* sum of frost days of all years divided by total number of years*/
System.out.println("Average first frost is the "+average_frost_day+"th day of the year");
System.out.println("Average first frost is on "+monthDay(average_frost_day));
} catch (FileNotFoundException ex) {
System.out.println("file not found");
}
}
public static void createTmpData() { /*
* In the question the input
* weatherDaily.dat file was not given,
* so I've just created a function to create the data file
* using random values, from the year 1974 to 2016; you may not need it
*/
BufferedWriter bw = null;
FileWriter fw = null;
try {
fw = new FileWriter("weatherDaily.dat");
bw = new BufferedWriter(fw);
for (int i = 1974; i < 2017; i++) {
for (int j = 1; j <= 12; j++) {
for (int k = 1; k <= getNoOfDaysInMonth(j, i); k++) {
int randomPrecip = new Random().nextInt(10);
int randomSnow = new Random().nextInt(10);
int randomHighTemp = new Random()
.nextInt(400 + 1 + 100) - 100;
int randomLowTemp = randomHighTemp
- new Random().nextInt(150);
String content = "" + i + " " + j + " " + k + " "
+ randomPrecip + " " + randomSnow + " "
+ randomHighTemp + " " + randomLowTemp + " ";
bw.write(content);
}
}
}
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/* Day of the year for first of month */
static final int[] monthSum = { 0, 31, 59, 90, 120, 151, 181, 212, 243,
273, 304, 334, 366 };
/**
* Convert year, month and day to day of the year.
*
* @param y
* year
* @param m
* month
* @param d
* day
*/
static int dayOfYear(int y, int m, int d) {
int doy = d + monthSum[m - 1]; // simple day of the year
if (y % 4 == 0 && m > 2) { // if leap year and after Feb 29
doy++; // add one to day for leap day
}
return doy;
}
/**
* Convert day of the year to month / day
*
* @param dday
* day of the year
*/
static String monthDay(double dday) {
int day = (int) (dday + 0.5); // round off to integer day
for (int month = 1; month <= 12; month++) {
if (day <= monthSum[month]) {
return month + "/" + (day - monthSum[month - 1]);
}
}
return "?/?";
}
static int getNoOfDaysInMonth(int i, int y) { // function used to find the no of days when I've created the weatherDaily.dat file
int[] daysinmonth;
if (y % 4 == 0) { // leapyear
daysinmonth = new int[] { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31 };
} else {
daysinmonth = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31 };
}
return daysinmonth[i - 1];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.