You are hired by the local weather bureau to analyze the temperatures record at
ID: 3627140 • Letter: Y
Question
You are hired by the local weather bureau to analyze the temperatures record at their airport for the previous year. At the airport, the weather equipment measures the high and low temperature for each day (starting at 12:00AM and ending at 11:59PM).
1. Set up a class WeatherDataBase that contains the following components:
INSTANCE VARIABLE
an array of 366 integers holding a temperature for each day of the year
METHODS
constructor - initializes the array elements to 0 explicitly
mutator - sets the temperature for the specified day number
accessor - returns the temperature for the specified day number
2. Set up a class DayConversion with a static method that accepts a integer parameter representing the day of the year and a boolean indicating if the year is a leap year. The method should return a string with the month and day number that is equivalent to the given day of the year. For example, if the method is called with the day number 1 and it is not a leap year, it should return "January 1". If the day number is 60 and it is a leap year, then the method should return "February 29". If the day number is 60 and it is not a leap year, then the method should return "March 1". Etc. Any technique is ok here, but there is a way to do this using an array to help you.
3. Set up a class WeatherAnalysis with a main method that creates two instances of the WeatherDataBase class, one for average high temperatures and one for actual high temperatures. Then load the database with all the input data (see INPUT PROCESSING below), calculate some statistics for the year (see STATISTICS below), and print out a table of information (see OUTPUT PROCESSING below).
INPUT PROCESSING
The data for the database will be stored in a file named "temperatures.txt". Use a try-catch block as shown below to open and process the file:
try {
Scanner input= new Scanner(new File("temperatures.txt"));
//
// YOUR CODE (use input.nextInt() to read the next integer)
//
} catch (IOException e) {
System.out.println("Unable to open or read from file.");
}
NOTE: Since the data is not coming from the keyboard here, and with the large amount of data, it wouldn't make sense for the data to ever come from the keyboard, do NOT use prompts to ask the user for the data. There is no need for prompts in this type of program since the data does not come directly from the user.
The data will consist of a year on the first line of input, and then an average high temperature and an actual high temperature on a line (i.e. two numbers per line) with a space in between.
Here is a sample of the first six lines of input for the program:
2011
34 38
34 43
35 47
35 46
35 30
The input indicates that the year we are processing is 2011. On January 1, the average high temperature is 34 and the actual high temperature is 38, on January 2, the average high temperature is 34 and the actual high temperature is 43, etc.
STATISTICS
You will need to calculate the following statistics for the year:
Number of days that are WARM (high temperature is between 5-9 degrees above the average high temperature)
Number of days that are HOT (high temperature is 10 or more degrees above the average high temperature)
Number of days that are COOL (high temperature is between 5-9 degrees below the average high temperature)
Number of days that are COLD (high temperature is 10 or more degrees below the average high temperature)
Number of "cooling days" that are 20 or more degrees above the average YEARLY high temperature
Number of "heating days" that are 20 or more degrees below the average YEARLY high temperature
OUTPUT PROCESSING
You may output to a text file or redirect the output that would normally go to the screen to a file instead, as follows:
java WeatherAnalysis > analysisoutput.txt
You can then examine the output in analysisoutput.txt in any standard text editor. This is very useful when the output is very long and scrolls too fast off the screen. This program is a good example of where redirection is very useful. NOTE: Be sure you redirect to a file you need for other reasons or you will overwrite it! If you do, TOO BAD, YOU LOSE!
Your output should show the year that we are analyzing. Then it should show each day along with its average high temperature, actual high temperature and a comment if the high temperature is warm, hot, cool or cold (as defined above) if applicable. You should also output the statistics you calculated above.
Here is an abbreviated sample of the output of the program: (HINT: You may use ' ' to tab to the next column in your output statements.)
WEATHER ANALYSIS 2011
DATE AVG HI HI TEMP
January 1 34 38
January 2 34 43 WARM
January 3 35 47 HOT
January 4 35 46 HOT
January 5 35 30 COOL
etc.
December 31 34 34
Number of WARM days: 39
Number of HOT days: 17
Number of COOL days: 45
Number of COLD days: 20
Average yearly high temperature: 58
Number of "cooling days": 65
Number of "heating days": 68
Explanation / Answer
please rate - thanks
it's not clear what
Average yearly high temperature: 58
is, so I did the average of the averages
import java.util.*;
import java.io.*;
public class WeatherAnalysis
{public static void main(String[] args)throws FileNotFoundException
{int year;
boolean ly;
String cat;
double avgtemp=0;
DayConversion d=new DayConversion();
WeatherDataBase average=new WeatherDataBase();
WeatherDataBase actual=new WeatherDataBase();
try {
Scanner input= new Scanner(new File("temperatures.txt"));
year=input.nextInt();
ly=leapyear(year);
int i=0,j,warm=0,hot=0,cool=0,cold=0,heating=0,cooling=0,av,ac,sum=0;
System.out.println("WEATHER ANALYSIS "+year);
System.out.println("Date AVG Hi Hi Temp");
while(input.hasNext())
{average.setTemp(i,input.nextInt());
actual.setTemp(i,input.nextInt());
i++;
}
for(j=0;j<i;j++)
{av=average.getTemp(j);
sum+=av;
ac=actual.getTemp(j);
System.out.print(d.getDate(j+1,ly)+" "+av+" "+ac+" ");
cat=getcat(av,ac);
if(cat.equals("WARM"))
warm++;
else if (cat.equals("HOT"))
hot++;
else if (cat.equals("COLD"))
cold++;
else if (cat.equals("COOL"))
cool++;
heating+=countheating(av,ac);
cooling+=countcooling(av,ac);
System.out.println(cat);
}
avgtemp=sum/(double)i;
System.out.println("Number of WARM days: "+warm);
System.out.println("Number of HOT days: "+hot);
System.out.println("Number of COOL days: "+cool);
System.out.println("Number of COLD days: "+cold);
System.out.println("Average yearly high temperature: "+avgtemp);
System.out.println("Number of "cooling days": "+cooling);
System.out.println("Number of "heating days": "+heating);
} catch (IOException e) {
System.out.println("Unable to open or read from file.");
}
}
public static String getcat(int avg,int act)
{String cat;
int diff=(act-avg);
if(diff>=5&&diff<=9)
cat="WARM";
else if(diff>=10)
cat="HOT";
else if(diff<=-5&&diff>=-9)
cat="COOL";
else if(diff<=-10)
cat="COLD";
else
cat="";
return cat;
}
public static int countheating(int avg,int act)
{int diff=act-avg;
if(diff>=20)
return 1;
return 0;
}
public static int countcooling(int avg,int act)
{int diff=avg-act;
if(diff>=20)
return 1;
return 0;
}
public static boolean leapyear(int year)
{boolean leap=false; //a year is a yeap year
if(year%4==0) //if it is divisible by 4
if(year%100==0) //but if it is also divisible by 100
{if(year%400==0) //it must be divisible by 400
leap=true;
}
else
leap=true;
return leap;
}
}
-------------
public class DayConversion
{public static String getDate(int doy,boolean ly)
{int days=0,i=0,n;
String mth[]={"January ","February ","March ","April ","May ","June ","July ",
"August ","September ","October ","November ","December "};
int daysinmonth[]={31,28,31,30,31,30,31,31,30,31,30,31};
String date;
if(ly)
daysinmonth[1]=29;
while(doy>daysinmonth[i])
{doy=doy-daysinmonth[i++];
}
return (mth[i]+" "+doy);
}
}
-------------------------------------
public class WeatherDataBase
{
private int []t=new int[366];
public WeatherDataBase()
{
int i;
for(i=0;i<366;i++)
t[i]=0;
}
public void setTemp(int i,int temp)
{t[i]=temp;
}
public int getTemp(int i)
{return t[i];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.