Results 1 to 8 of 8
Thread: Heat index arrays
- 12-29-2009, 03:20 AM #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
handle specific tasks.
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.
9. Use the Formatting Grid you downloaded in the last lesson to plan your user-friendly
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();
keyWestTemp.add(temp1);
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();
keyWestHumid.add(hum1);
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()) {
System.out.println("Data does not match, please check your .txt file");
}
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 );
}
}
}
- 12-29-2009, 04:37 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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.
- 12-29-2009, 06:33 AM #3gcampton Guest
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.
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.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);Last edited by gcampton; 12-29-2009 at 06:39 AM.
- 12-29-2009, 07:21 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
"sum" might be better ;)int temptemp=0;
And make it a double: a good strategy in lots of circumstances, including this one where you might want to avoid truncation.
- 12-29-2009, 08:44 AM #5gcampton Guest
why sum? it's a average.... average would be better :P
but i was referring to "temporary temperature" temptemp
- 12-29-2009, 07:31 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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.
- 12-29-2009, 10:53 PM #7
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
this is betterJava 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(); keyWestTemp.add(temp1); 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(); keyWestHumid.add(hum1); 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()) { System.out.println("Data does not match, please check your .txt file"); } 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 ); } } }
- 12-30-2009, 01:07 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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.
Similar Threads
-
New to Java, Arrays- index out of bounds
By Connorhj in forum New To JavaReplies: 8Last Post: 12-02-2009, 09:22 PM -
I can't run my index.jsp
By gissah in forum New To JavaReplies: 0Last Post: 03-23-2009, 01:42 AM -
Help with Flesch Index
By L_22 in forum Advanced JavaReplies: 1Last Post: 03-31-2008, 05:30 PM -
Replacing at an index
By bugger in forum New To JavaReplies: 2Last Post: 01-29-2008, 06:33 AM -
z-Index problem
By mjdousti in forum AWT / SwingReplies: 1Last Post: 12-29-2007, 01:34 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks