Results 1 to 8 of 8
  1. #1
    gtamob69 is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default 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 );





    }

    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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. #3
    gcampton Guest

    Default

    Quote Originally Posted by pbrockway2 View Post
    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 07:39 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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. #5
    gcampton Guest

    Default

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

    but i was referring to "temporary temperature" temptemp

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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. #7
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    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();
    
    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 );
    
    
    
    
    
    }
    
    }
    }
    this is better

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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

  1. New to Java, Arrays- index out of bounds
    By Connorhj in forum New To Java
    Replies: 8
    Last Post: 12-02-2009, 10:22 PM
  2. I can't run my index.jsp
    By gissah in forum New To Java
    Replies: 0
    Last Post: 03-23-2009, 02:42 AM
  3. Help with Flesch Index
    By L_22 in forum Advanced Java
    Replies: 1
    Last Post: 03-31-2008, 06:30 PM
  4. Replacing at an index
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-29-2008, 07:33 AM
  5. z-Index problem
    By mjdousti in forum AWT / Swing
    Replies: 1
    Last Post: 12-29-2007, 02:34 AM

Posting Permissions

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