1. Member
Join Date
Dec 2009
Posts
2
Rep Power
0

## Heat index arrays

Ok so for this assignment
Instructions: Write a program to calculate the monthly Heat Index for a specific city.
1. Create a new project called 6.03 Weather Data in the Mod06
Assignments folder.
2. Create a class called HeatIndex in the newly created folder.
3. Be sure you have downloaded the KeyWestTemp.txt and the KeyWestHumid.txt files to the
project folder.
4. Break the program into functional units so that you can use separate for loops and arrays to
5. Your program should read in the data from the two text files.
6. Be sure to use the for-each loop when it is appropriate.
7. Calculate the monthly Heat Index for Key West, Florida.
8. Notice from the table shown in the 6.03 Virtual Lecture Notes that if the temperature and
humidity are below certain levels, the actual temperature and the apparent temperature (i.e.
the Heat Index) are identical. For example, in the expected output shown below, notice that
the Heat Index is only calculated for the months of May – October.
output.
10. Print the results in a user-friendly format (i.e. use printf()) with columns for the Month,
Temperature, Relative Humidity, and the Heat Index.
11. Write the Heat Index data to a file. Include this file when you submit your program.
12. Writing the lengthy Heat Index formula can be very error prone, so watch out for the Order
of Operations.

The problem I am having is I am trying to average up all my results for temp, hum, and heat index. But with no success.
Please can you show me how I am supposed to average my variables. I already have 97% of the program finished. Also I am supposed to use the print writer to write my heat index results to a .txt file, can you show me how to do that too.

Here is the code
/**
* Displays the heat index of Key West Florida from the given text files.
*
* @author Frank Binder
* @version 12/25/09
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

public class HeatIndex {

public static void main(String[] args) throws IOException {
String h = "Heat Index: ";

String keyW = "Key West, Florida";

System.out.printf("%35s%1s\n ", h, keyW);

System.out.println("");

System.out.printf("%40s\n ", "Months");
String[] months = { " Jan ", " Feb ", " Mar ", " Apr ", " May ", " Jun ", " Jul ", " Aug ", " Sep ", " Oct ", " Nov ", " Dec " };

for (String name : months) {
System.out.print(name);
}

System.out.println("");

System.out.println("****************************** *********************************************");

String temp1 = "";
File fileName = new File("KeyWestTemp.txt");
System.out.println("Temp (F)");
Scanner inFile = new Scanner(fileName);

List keyWestTemp = new ArrayList();
while (inFile.hasNext())

{
temp1 = inFile.next();

System.out.printf("%6s", temp1);

} inFile.close();

System.out.println("");

System.out.println("");

String hum1 = "";
File fileName1 = new File("KeyWestHumid.txt");
System.out.println("Humidity");
Scanner inFile1 = new Scanner(fileName1);
List keyWestHumid = new ArrayList();

while (inFile1.hasNext())

{
hum1 = inFile1.next();

System.out.printf("%6s", hum1);
}

inFile.close();

System.out.println("");

System.out.println("");
System.out.println("Heat Index");

//used to see if amount of data given in files are equal

if (keyWestHumid.size() != keyWestTemp.size()) {

}

for (int i = 0; i < keyWestTemp.size(); i++) {

double temp = Double.valueOf((String) keyWestTemp.get(i));

double hum = Double.valueOf((String) keyWestHumid.get(i));

double heatindex = -42.379 + 2.04901523 * temp + 10.14333127 * hum - 0.22475541 * temp * hum - 0.00683783 * Math.pow(temp, 2) - .05481717 * Math.pow(hum, 2) + .00122874 * Math.pow(temp, 2) * hum + .00085282 * temp * Math.pow(hum, 2) - .00000199 * Math.pow(temp * hum, 2);

heatindex = Math.round(100 * heatindex) / 100;

System.out.printf("%6s", heatindex );

}

}
}

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
The problem I am having is I am trying to average up all my results for temp, hum, and heat index. But with no success.

It's not clear from this what the problem is. Do you know what an average is? Are there compiler messages you can't understand? Or runtime behaviour that differs from what you expected or intended? "No success" covers a pretty wide field.

3. gcampton Guest
Originally Posted by pbrockway2
It's not clear from this what the problem is. Do you know what an average is? Are there compiler messages you can't understand? Or runtime behaviour that differs from what you expected or intended? "No success" covers a pretty wide field.
I agree, Also you need to use code tags when posting code on the forums. but an average is the sum of a bunch of numbers divided by the amount of numbers.

The way I would go about this is, you have an array of temperatures and you need to find the average, then figure out how many elements you have (length), and divide that by the total of all teperatures, eg.

Java Code:
```int temptemp=0;
for (int i=0; i<array.length; i++)
{
temptemp += array[i];
}
temptemp = temptemp \ array.length;
System.out.println("The averages temperature for this city is: " + temptemp);```
edit: you seem to disregard the teachers step 12. If I were you I would break up this code some more, so you have 1 method doing the file handling, and another calculating heat index. etc.
Last edited by gcampton; 12-29-2009 at 06:39 AM.

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
int temptemp=0;
"sum" might be better ;)

And make it a double: a good strategy in lots of circumstances, including this one where you might want to avoid truncation.

5. gcampton Guest
why sum? it's a average.... average would be better :P

but i was referring to "temporary temperature" temptemp

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
I see, yes.

(I thought "sum" because it spends a large part of its "life" as the sum of the temperatures. If the data only becomes available bit by bit instead of as an entire array, then it makes sense to keep and update the sum and the count in order to make the average available.)

Mostly though I wanted to suggest to the OP that a double value might serve better for accumulated statistics.

7. Senior Member
Join Date
Oct 2009
Location
California,US
Posts
201
Rep Power
11
Java Code:
```/**
* Displays the heat index of Key West Florida from the given text files.
*
* @author Frank Binder
* @version 12/25/09
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

public class HeatIndex {

public static void main(String[] args) throws IOException {
String h = "Heat Index: ";

String keyW = "Key West, Florida";

System.out.printf("%35s%1s\n ", h, keyW);

System.out.println("");

System.out.printf("%40s\n ", "Months");
String[] months = { " Jan ", " Feb ", " Mar ", " Apr ", " May ", " Jun ", " Jul ", " Aug ", " Sep ", " Oct ", " Nov ", " Dec " };

for (String name : months) {
System.out.print(name);
}

System.out.println("");

System.out.println("****************************** *********************************************");

String temp1 = "";
File fileName = new File("KeyWestTemp.txt");
System.out.println("Temp (F)");
Scanner inFile = new Scanner(fileName);

List keyWestTemp = new ArrayList();
while (inFile.hasNext())

{
temp1 = inFile.next();

System.out.printf("%6s", temp1);

}	inFile.close();

System.out.println("");

System.out.println("");

String hum1 = "";
File fileName1 = new File("KeyWestHumid.txt");
System.out.println("Humidity");
Scanner inFile1 = new Scanner(fileName1);
List keyWestHumid = new ArrayList();

while (inFile1.hasNext())

{
hum1 = inFile1.next();

System.out.printf("%6s", hum1);
}

inFile.close();

System.out.println("");

System.out.println("");
System.out.println("Heat Index");

//used to see if amount of data given in files are equal

if (keyWestHumid.size() != keyWestTemp.size()) {

}

for (int i = 0; i < keyWestTemp.size(); i++) {

double temp = Double.valueOf((String) keyWestTemp.get(i));

double hum = Double.valueOf((String) keyWestHumid.get(i));

double heatindex = -42.379 + 2.04901523 * temp + 10.14333127 * hum - 0.22475541 * temp * hum - 0.00683783 * Math.pow(temp, 2) - .05481717 * Math.pow(hum, 2) + .00122874 * Math.pow(temp, 2) * hum + .00085282 * temp * Math.pow(hum, 2) - .00000199 * Math.pow(temp * hum, 2);

heatindex = Math.round(100 * heatindex) / 100;

System.out.printf("%6s", heatindex );

}

}
}```
this is better

8. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18
I don't think it's helpful to post solutions that, if used, would deprive the OP of the value actually writing the code.

Fix the code format.
Remove the unused imports.
Use generics with both of the List instances.
Fix the months array so it works with the %6 specifier when the table is printed
Use the format specifiers properly to get two decimal places (they support precision as well as width).
Remove the redundant spaces and empty strings in printf() and println() calls.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•