Windchill

From CompSciWiki
Revision as of 12:24, 7 December 2011 by AdamW (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Back to the Program-A-Day homepage

Problem

As you know from living in Winnipeg, wind can make the air temperature feel much colder. Write a program that will uses JOptionPane to read in an air temperature and a wind speed. Then use the formula below to calculate and output the windchill.


<math>T_{wc}=13.12 + 0.6215 T_a-11.37 V^{0.16} + 0.3965 T_a V^{0.16}\,\!</math>
where <math>T_{wc}\,\!</math> is the wind chill index based on the Celsius scale, <math>T_a\,\!</math> is the air temperature in °C, and <math>V\,\!</math> is the air speed in km/h.
Equation taken from Wikipedia which cites http://www.msc.ec.gc.ca/education/windchill/science_equations_e.cfm


Hint: Use Math.pow to complete the equation.
Sample output with location Winnipeg, air temperature -10 and wind speed 30:

 Location: Winnipeg
Current Temperature: -10.0 Celsius
Wind Speed: 30.0 km/h
Temperature with Windchill: -19.52049803338773
Programmed by A. Student
**End of Program** 
 

Introducing Math methods

Wiki chars03.jpg

Solution

Start off by declaring your variables. You will need variables for all the different data included. Temperature, windspeed, and windchill will all be input by the user. Location will also be input by the user. You must declare a String "temp" to be used for initializing variables.

 double temperature;                     //temperature amount
double windspeed;                       //wind speed amount
double windchill;                       //windchill amount

String location;                        //the location
String temp;                            //temporary string for initializing
                                                //variables 

Your next step will be to get the proper input from the user. You will use JOptionPane to get the information, and then parseDouble to glean the actual data from the input. Remember that JOptionPane will return a String, so parseDouble must be used to convert this to usable data.

 location = JOptionPane.showInputDialog ("What is the location?");
temp = JOptionPane.showInputDialog ("What is the current temperature in Celsius?");
temperature = Double.parseDouble (temp);

temp = JOptionPane.showInputDialog ("What is the wind speed in kms per hour?")
windspeed = Double.parseDouble (temp);      //converts windspeed to double 

The next step is fairly straightforward. This is the part of the program that will do the actual calculations. Math.pow can be used to utilize exponents in a formula. The first parameter is the variable, and the second parameter is the exponent.

 windchill = 13.12 + .6215*temperature - 11.37 *
Math.pow(windspeed, .16) + .3965 * temperature * Math.pow(windspeed,.16); 

Finally, you must print out the output so the user can obtain the information. This is fairly simple, you can just use System.out.println to show all the relevant data. It is also good practice to output a message that indicates to the user that the program has finished all processing. Congratulations, you should now have a working program that determines the temperature with windchill. Never again will you be forced to blindly trust the weather network.

 System.out.println ("Location: " + (location));
System.out.println ("Current Temperature: " + (temperature) + (" Celsius"));
System.out.println ("Wind Speed: " + (windspeed) + (" km/h"));
System.out.println ("Temperature with Windchill: " + windchill);

System.out.println("Programmed by A. Student");
System.out.println("**End of Program**"); 

Code

Solution Code

Back to the Program-A-Day homepage