Wind Chill Calculator
You will need to import these classes: java.io.FileWriter and java.io.IOException.
private instance variables
Declare a variable 'writer' of type 'FileWriter'.
In the constructor, in the 'try' section, allocate space for 'writer' with a call to 'new FileWriter(output file name)'.
private void writeFixedString(String s, int size)
This method allows you to print Strings of a fixed length, which enables the output from the program to be arranged neatly in columns. This method is written for you.
public int findWindChill(int temp, int wind)
This method accepts a temperature and a wind speed as inputs. It returns a wind chill factor. Here is how wind chill is calculated:
Wind Chill Calculation
|Wind Speed||How to Calculate Wind Chill|
|0 - 4 mph||temperature|
|> 4 - 45 mph||91.4 - (10.45 + 6.69*sqrt(windSpeed) - 0.447 * windSpeed) * (91.4 - temperature) / 22.0|
|> 45 mph||1.6 * temperature - 55.0|
You will want to use an IF - ELSE IF - ELSE structure to handle the three possibilities for wind speed.
You may need to cast a double to an int in this method. To do this, you can use syntax like: int a = (int)(math calculation);
Once the wind chill factor is calculated, use a 'return' statement to return it.
public void generateWindChillTable()
This method prints the wind chill factors in a well-organized table. Degrees are in columns and wind speeds are in rows. Here is how the first several rows of output appear:
|Wind Chill Table|
|degrees F -->||50||40||30||20||10||0||-10||-20||-30||-40||-50||-60|
In the first row is the heading 'Wind Chill Table'. In the second row the degrees are listed from 50° down to -60° F.
In the third row the actual output of wind chill factors begins. First the wind speed for that row is listed. Following are the 12 wind chill factors for temperatures from 50° down to -60° F. Note that, since there is no wind, these wind chill factors are identical to the temperatures in the row above.
In the fourth row are wind chill factors for a wind speed of 5 mph. Note that you can begin to see that the wind is reducing temperatures slightly.
Now, to create a nicely-formatted table, you have to print the numbers in evenly-spaced columns, which is where the method 'writeFixedString()' comes in. To print an integer, first convert it to a String using the 'valueOf()' method of the String class:
chillStr = String.valueOf(temp);
Then send 'chillStr' to 'writeFixedString()' along with the number of columns the integer should occupy:
Put all your Java code inside the 'try' section. In this method, declare integers 'temp', 'wind' and 'chill'. Declare a String 'chillStr'. Make calls to 'writer.write()' to print the first 2 rows of the output table. Recall that printing out '\n' causes output to move to the next line.
Time to calculate and print out the main body of the table. Set up a FOR loop based on 'wind' going from 0 to 50, increasing by 5 each time the loop runs. Inside this loop, call 'writeFixedString()' to print the wind speed, and 'writer.write()' to print the ' mph:' at the beginning of each row (for example: '10 mph: '). Then set up a second, nested FOR loop based on 'temp' going from 50 down to -60 (brrrr), decreasing by 10 each time the loop runs. Inside this loop, make a call to 'findWindChill()', sending in the temperature and wind speed, and storing the result in 'chill'. Convert 'chill' to 'chillStr' using the method outlined above, and print 'chillStr' to the output file.
After the inner loop ends, but still inside the outer loop, print a '\n' character to cause output to move to the next line. After you are done writing to the output file, call 'writer.close()'.
public static void main(String args)
Create a 'WindChill' object by making a call to 'new WindChill()'. Then call the 'generateWindChillTable()' method of that object to get the ball rolling.
You can download WindChill.java to get started.