This java program is based off the scenario below. I am in need of help creating
ID: 3746612 • Letter: T
Question
This java program is based off the scenario below. I am in need of help creating/completeing this code. The code I currently have keeps giving me errors for some reason. Any chance I could get some help with writing this code?
According to the National Weather Service’s website, the Heat Index (HI) is determined as follows:
The Heat Index Equation The computation of the heat index is a refinement of a result obtained by multiple regression analysis carried out by Lans P. Rothfusz and described in a 1990 National Weather Service (NWS) Technical Attachment (SR 90-23). The regression equation of Rothfusz is
HI = -42.379 + 2.04901523*T + 10.14333127*RH - .22475541*T*RH - .00683783*T*T - .05481717*RH*RH + .00122874*T*T*RH + .00085282*T*RH*RH - .00000199*T*T*RH*RH
where T is temperature in degrees F and RH is relative humidity in percent. HI is the heat index expressed as an apparent temperature in degrees F.
Taking the heat index into account, commonly the following results (according to, e.g., Wikipedia):
Fahrenheit
Notes
80–90 °F
Caution: fatigue is possible with prolonged exposure and activity. Continuing activity could result in heat cramps.
90–105 °F
Extreme caution: heat cramps and heat exhaustion are possible. Continuing activity could result in heat stroke.
105–130 °F
Danger: heat cramps and heat exhaustion are likely; heat stroke is probable with continued activity.
over 130 °F
Extreme danger: heat stroke is imminent.
Based on this information, you are asked to respond to some questions from the Pure Michigan Marketing team that wants to determine alternatives in the timing for the annual bridge walk. One suggestion is to start the walk at 05:00, whereas another suggestion is to let the walk continue until 19:00. (Note that 19:00 is the end time for the walk not the start of the last walker.)
Since nobody likes heat exhaustion or heat stroke, heat indices above 90 are considered prohibitive, as is rain.
To determine feasible options, you are provided with the “LaborDay.csv” files which contain the information from last years' Labor Day for time of day, temperature, relative humidity, and precipitation. They are aware that they can re-purpose this approach for other venues as well, so you are to write a program that performs the tasks in an automated fashion, allowing for flexibility (given the current file format).
Read the values into a multidimensional array, whereby each individual row of the file should be an array itself. (Hint: note the first row contains the labels.) You should then design and implement:
Reading of the file in its own method returning the 2D array,
Calculation of the HI for the values provided in its own method,
Determination of the average and max. heat indices for two-hour time periods which is considered the time it takes one walk,
Simple decision whether a starting/end time would be prohibitive (HI or rain) given the 2-hour time windows.
You are to output the calculation in a simple CSV format to STDOUT that contains columns with the start time, HI averages and maxima (each float format, 2 decimals) and "Yes" / "No" strings if the start at a particular time would be prohibitive. For the output, select to start at full hours only and within the given time window.
This is the information below is what's suppose to be in the txt file that you input to use for the numbers.
LaborDay.txt
Time,T,RH,Prec
0:15,69.3,66,0
0:30,69.4,67,0
0:45,69.4,66,0
1:00,69.6,66,0
1:15,69.4,67,0
1:30,68.4,69,0
1:45,68.9,68,0
2:00,67.8,68,0
2:15,68.7,67,0
2:30,68.2,68,0
2:45,68.5,68,0
3:00,68.4,70,0
3:15,68.5,70,0
3:30,68.4,69,0
3:45,68.7,66,0
4:00,66.7,71,0
4:15,66.2,69,0
4:30,67.1,65,0
4:45,66,71,0.3
5:00,65.5,72,0.3
5:15,65.5,75,0.3
5:30,65.5,75,0.2
5:45,65.1,73,0.1
6:00,65.1,72,0
6:15,64.8,72,0
6:30,64.8,74,0
6:45,64.8,74,0
7:00,64.9,77,0
7:15,65.1,79,0
7:30,65.1,81,0
7:45,66.2,86,0
8:00,66.4,92,0
8:15,66.2,79,0
8:30,66.2,78,0
8:45,66,79,0
9:00,66,80,0
9:15,66.4,79,0
9:30,67.1,79,0
9:45,67.3,79,0
10:00,67.3,79,0
10:15,67.6,81,0
10:30,68.9,82,0
10:45,73.2,80,0
11:00,78.1,77,0
11:15,81,77,0
11:30,84.4,71,0
11:45,81.3,73,0
12:00,86.5,69,0
12:15,86.9,67,0
12:30,87.3,69,0
12:45,87.4,72,0
13:00,88,70,0
13:15,95.2,67,0
13:30,90.9,66,0
13:45,85.5,71,0
14:00,87.8,70,0
14:15,83.1,74,0
14:30,87.8,70,0
14:45,81.5,73,0
15:00,85.5,69,0
15:15,88.2,66,0
15:30,83.8,70,0
15:45,87.6,67,0
16:00,90.7,66,0
16:15,90.7,69,0
16:30,90.9,66,0
16:45,86.7,69,0
17:00,86.2,70,0
17:15,86.9,67,0
17:30,85.8,66,0
17:45,84.7,68,0
18:00,82.2,67,0
18:15,81.1,63,0
18:30,79.2,60,0
18:45,75,61,0
19:00,72.9,63,0
19:15,71.4,64,0
19:30,70,66,0
19:45,68.7,67,0
20:00,67.6,66,0
20:15,66.7,67,0
20:30,66,67,0
20:45,65.3,70,0
21:00,64.6,71,0
21:15,64,71,0
21:30,63.3,70,0
21:45,62.8,73,0
22:00,62.4,75,0
22:15,61.7,76,0
22:30,61.3,76,0
22:45,61,79,0
23:00,60.4,84,0
23:15,60.1,85,0
An example of the output could look like this:
23:00, 69.66, 69.66, Yes
Fahrenheit
Notes
80–90 °F
Caution: fatigue is possible with prolonged exposure and activity. Continuing activity could result in heat cramps.
90–105 °F
Extreme caution: heat cramps and heat exhaustion are possible. Continuing activity could result in heat stroke.
105–130 °F
Danger: heat cramps and heat exhaustion are likely; heat stroke is probable with continued activity.
over 130 °F
Extreme danger: heat stroke is imminent.
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;
/**
*
*
*/
public class HeatPredictionApp {
static String[][] inputData;//col 0->Time,col 1->Temperature,col 2->RH,col 3->Prec ,col 4->HI
static String[][] outputData;//col 0->start Time,col 1->Avg HI,col 2->Max HI,col 3->presipitation ,col 4->Suitable for walk(Yes/No) Single data is of walkDurationInHours .
//So if starttime is 11:00 ,data calculated will be for duration 11:00 - 1:00
static String inputFileName = "LabourDay.txt";
static int timeGapInMinutes = 15; //time gap of input data
static int walkDurationInHours = 2; //output file data duration ie if 11:00 is given in o/p file , it account for 11:00-1:00
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat(".##");//formats to two decimal places
//read file
int count = getDataCount();
inputData = new String[count][5];//Details of values in col . 0->Time,1->Temperature,2->RH,3->Prec , 4->HI (calculated later)
readInputFile();//reads teh input file and updates in inputData
calculateHI(count);//calculate HI and update at col4 in inputData
//does the prediction based on data ie if prescipitaion>0 and HI>90 ,suitability is No
int rowCountOutput=prediction(count);
System.out.println(" Start Time" + " " + "Avg HI" + " " + " Max HI" +" " + "Precipitation"+" " + "Suitability");
for (int j = 0; j < rowCountOutput; j++) {
System.out.println(outputData[j][0] + " " + outputData[j][1] + " " + outputData[j][2] + " " + outputData[j][3]+ " " + outputData[j][4]);
}
}
/**
* Read data from input file
*/
private static void readInputFile() {
File file = new File(inputFileName);
DecimalFormat df = new DecimalFormat(".##");//formats to two decimal places
Scanner fin;
int col;
Scanner strsplit;
try {
fin = new Scanner(file);
fin.nextLine();//ignores the label from file
int row = 0;
while (fin.hasNextLine()) {
strsplit = new Scanner(fin.next()).useDelimiter(",");//get a line from file and splits it based on ,
inputData[row][0] = strsplit.next(); //time
inputData[row][1] = df.format(Double.parseDouble(strsplit.next())); //time
inputData[row][2] = df.format(Double.parseDouble(strsplit.next())); //RH
inputData[row][3] = df.format(Double.parseDouble(strsplit.next())); //precipitation
row++;
}
} catch (FileNotFoundException ex) {
System.out.println(inputFileName + ": input file not found");
}
}
/**
* Returns total lines in file(only data)
*
* @return total data count
*/
private static int getDataCount() {
int count = 0;
File file = new File(inputFileName);
Scanner fin;
try {
fin = new Scanner(file);
fin.nextLine();//ignores the label from file
while (fin.hasNextLine()) {
fin.next();
count++;
}
} catch (FileNotFoundException ex) {
System.out.println(inputFileName + ": input file not found");
}
return (count);
}
/**
* This calculates the HI and updates in inputdata at col 4.
* HI = -42.379 +
* 2.04901523*T + 10.14333127*RH - .22475541*T*RH - .00683783*T*T -
* .05481717*RH*RH + .00122874*T*T*RH + .00085282*T*RH*RH -
* .00000199*T*T*RH*RH
* where T is temperature in degrees F and RH is relative humidity in percent.
*
*
* @param count input data count
*/
private static void calculateHI(int count) {
double T;
double RH;
DecimalFormat df = new DecimalFormat(".##");
for (int i = 0; i < count; i++) {
T = Double.parseDouble(inputData[i][1]);
RH = Double.parseDouble(inputData[i][2]);
inputData[i][4] = "" +df.format(-42.379 + 2.04901523 * T + 10.14333127 * RH - .22475541 * T * RH - .00683783 * T * T - .05481717 * RH * RH + .00122874 * T * T * RH + .00085282 * T * RH * RH - .00000199 * T * T * RH * RH);
}
}
/**
* /does the prediction based on data ie if prescipitaion>0 and HI>90 ,suitability is No
* Timeduration is taken as walkDurationInHours For a single duration avg
* HI,max HI and prediction is calculated
*
* @param count
*/
private static int prediction(int count) {
int dataChunk = walkDurationInHours * 60 / timeGapInMinutes; //chunk of data to be considered at a single go
int rowCountOutput = count / dataChunk; //row count of outputData
int extraRows = count % dataChunk; //rows pending after complete chunk
DecimalFormat df = new DecimalFormat(".##"); //formats to two decimal palces
if ((extraRows) >= 1) {
rowCountOutput += 1;
}
outputData = new String[rowCountOutput][5];
double HI;
double prec;
String startTime;
int rowOutput = 0;
double sum = 0;
double max;
double sumPrec = 0;
double avgHI=0;
for (int rowInput = 0; rowInput < count && rowOutput < rowCountOutput; rowInput = rowInput + dataChunk) {//take data in sets of data chunk
sum = 0;avgHI=0;sumPrec = 0;
startTime = (inputData[rowInput][0].split(":"))[0];
outputData[rowOutput][0] = startTime + ":00";//get the start time of duration
max = Double.parseDouble(inputData[rowInput][4]);
for (int i = rowInput,j=0; j < dataChunk&&i<count; j++,i++) { //calculate avg,max and pres for a duration
HI=Double.parseDouble(inputData[i][4]);
prec=Double.parseDouble(inputData[i][3]);
sum = sum + HI;
sumPrec += prec;
if (max < HI) {
max = HI;
}
}
avgHI=sum / dataChunk;
if (((avgHI) < 90) && sumPrec == 0) {//if avg HI< and no precipitation then only suitable
outputData[rowOutput][4] = "Yes";
} else {
outputData[rowOutput][4] = "No";
}
outputData[rowOutput][1] = "" + df.format(avgHI);
outputData[rowOutput][2] = "" + df.format(max);
outputData[rowOutput][3] = "" + df.format(sumPrec);
rowOutput++;
}
return rowCountOutput;
}
}
Sample Output
run:
Start Time Avg HI Max HI Precipitation Suitability
0:00 74.48 74.76 .0 Yes
2:00 74.1 74.76 .0 Yes
4:00 73.59 75.28 1.2 No
6:00 70.21 73.86 .0 Yes
8:00 70.41 70.87 .0 Yes
10:00 81.51 95.84 .0 Yes
12:00 101.71 120.28 .0 No
14:00 95.05 105.23 .0 No
16:00 96.42 107.33 .0 No
18:00 77.19 83.65 .0 Yes
20:00 74.7 75.55 .0 Yes
22:00 45.14 74.38 .0 Yes
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.