It should be a simple java program that a novice can create but I\'m lost Proble
ID: 3590909 • Letter: I
Question
It should be a simple java program that a novice can create but I'm lost
Problem:
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.
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
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: date of this weather record date of this weather record from 1 to 12 date of this weather record from 1 to 31 year month da precipitation amount of precipitation on this day snow 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 amount of snow that fell on this day 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.Explanation / Answer
I am assuming each field in each lines are seperated by comma. Input is taken from myfile.txt file in the same folder of code.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Test {
static final int[] monthSum={0,31,59,90,120,151,181,212,243,273,304,334,366};
static int dayOfYear(int y,int m,int d){
int doy = d+monthSum[m-1];
if(y%4==0 && m>2){
doy++;
}
return doy;
}
static String monthDay(double dday){
int day= (int)(dday+0.5);
for(int month=1;month<=12;month++){
if(day<=monthSum[month]){
return month+"/"+(day-monthSum[month-1]);
}
}
return "?/?";
}
public static void main(String[] args) {
BufferedReader reader;
try {
int set=0;
int year=0;
int num_of_years=0;
int total_days=0;
double average=0.0;
reader = new BufferedReader(new FileReader("myfile.txt")); //myfile.txt is containing the data with field seperated by comma
String line;
for(; (line = reader.readLine()) != null; ) {
String[] tokens = line.split(","); //assuming fields are seperated by comma
if(year==Integer.parseInt(tokens[0])&&set==1) // if the First frost already occured for a year
{
continue;
}
else if(year!=Integer.parseInt(tokens[0])) //If new year have arrived we will set the value set to 0
{
year = Integer.parseInt(tokens[0]);
set=0;
continue; //I am terminating the loop because I am assuming every year begins with 1st January as mentioned in question
}
else{
if(Integer.parseInt(tokens[1])<7) //we are skipping dates having month less than July
continue;
else{
if(Integer.parseInt(tokens[6])<0) //This condition is True implies we have reached at First Frost, this will be true only one time in a year
{
set=1;
System.out.println("First frost in "+tokens[0]+" was on "+tokens[1]+"/"+tokens[2]);
total_days+= dayOfYear(Integer.parseInt(tokens[0]),Integer.parseInt(tokens[1]),Integer.parseInt(tokens[2])); //we will call dayOfYear funtion to calculate total dyas, we are adding all the total days for every year in variable total_days
num_of_years++;
}
}
}
}
reader.close();
average=(((double)total_days)/((double)num_of_years)); //this will calculate average days
System.out.println(" Average first frost is the "+average+" day of the year");
System.out.println(" Average first frost is on "+monthDay(average));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.