Difference between revisions of "Windchill"

From CompSciWiki
Jump to: navigation, search
(Wrote a step-by-step solution for this program, since it was missing in this case)
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{1010PrAD|ProblemName=Windchill
 
{{1010PrAD|ProblemName=Windchill
  
|Problem= As you know from living in Winnipeg, wind can make the air temperature feel much colder. Write a program that will use Scanner to read in an air temperature and a wind speed. Then use the formula below to calculate and output the windchill.  
+
|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.  
  
  
Line 7: Line 7:
  
 
:<small>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 [[kilometres per hour|km/h]].
 
:<small>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 [[kilometres per hour|km/h]].
 
 
:::<small>Equation taken from Wikipedia which cites [[Environment Canada|http://www.msc.ec.gc.ca/education/windchill/science_equations_e.cfm]]
 
:::<small>Equation taken from Wikipedia which cites [[Environment Canada|http://www.msc.ec.gc.ca/education/windchill/science_equations_e.cfm]]
</small>
+
</small></small>
 
+
  
 
<BR>
 
<BR>
 
+
Hint: Use Math.pow to complete the equation.
 +
<br>
 
Sample output with location Winnipeg, air temperature -10 and wind speed 30:
 
Sample output with location Winnipeg, air temperature -10 and wind speed 30:
<pre>
+
{{OutputBlock
 +
|Code=
 
Location: Winnipeg
 
Location: Winnipeg
 
Current Temperature: -10.0 Celsius
 
Current Temperature: -10.0 Celsius
Line 21: Line 21:
 
Temperature with Windchill: -19.52049803338773
 
Temperature with Windchill: -19.52049803338773
 
Programmed by A. Student
 
Programmed by A. Student
**End of Program**</pre>
+
**End of Program**
 +
}}
  
|SideSectionTitle=Primitive Data Types
+
|SideSectionTitle=Introducing Math methods
 
|SideSection=
 
|SideSection=
 
[[Image:Wiki_chars03.jpg|center]]<BR>
 
[[Image:Wiki_chars03.jpg|center]]<BR>
  
 
|Solution=
 
|Solution=
===Create Variables===
+
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.
Start off by declaring and initializing your variables. You will need a string to store user input, along with some integers. Create these variables and set the integers to the values given in the problem. [[Named Constants|Named constants]] should normally be used here for fixed numbers; these will be explained next week.
+
{{CodeBlock
<pre>
+
|Code=
String input;
+
double temperature;                     //temperature amount
+
double windspeed;                      //wind speed amount
int costPSF = 8;       // Cost of $8 per square foot
+
double windchill;                       //windchill amount
int installFee = 500; // $500 installation fee
+
 
int coupon = 50;       // $50 off coupon
+
String location;                       //the location
</pre>
+
String temp;                           //temporary string for initializing
You will also need variables to store the length and width input from the user, the calculated area, and the total cost. Remember that total cost is to be stored as a double (as cost may include dollars and cents).
+
                                                //variables
<pre>
+
}}
int length;
+
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.
int width;
+
{{CodeBlock
int area;
+
|Code=
double totalCost;
+
location = JOptionPane.showInputDialog ("What is the location?");
</pre>
+
temp = JOptionPane.showInputDialog ("What is the current temperature in Celsius?");
===User Input===
+
temperature = Double.parseDouble (temp);
Now the length and width must be read in. Input dialog boxes can be used to prompt the user for the values. The inputs are read in as strings, and then must be parsed to integers before they can be stored as integer variables. Use the <b>Integer.parseInt()</b> function for this.
+
 
<pre>
+
temp = JOptionPane.showInputDialog ("What is the wind speed in kms per hour?")
input = JOptionPane.showInputDialog("Input length:");
+
windspeed = Double.parseDouble (temp);      //converts windspeed to double
length = Integer.parseInt(input);
+
}}
 +
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.
 +
{{CodeBlock
 +
|Code=
 +
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.
 +
{{CodeBlock
 +
|Code=
 +
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**");
 +
}}
 +
 
 +
|SolutionCode=
 +
 
 +
import javax.swing.*;
 +
/*****************************************************************************************
 +
Title: Windchill
 +
Purpose: To input different temperatures and wind speeds and calculate windchill
 +
@author A. Student
 +
@version 2010-Sept-22
 +
*****************************************************************************************/
 +
 
 +
public class Windchill2
 +
{                                              //opens public class
 +
    public static void main (String []args)
 +
    {                                          //opens main method
  
input = JOptionPane.showInputDialog("Input width:");
+
    //variables declared here
width = Integer.parseInt(input);
+
        double temperature;                     //temperature amount
</pre>
+
        double windspeed;                       //wind speed amount
===Perform Arithmetic===
+
        double windchill;                       //windchill amount
To calculate area, multiply <b>length</b> and <b>width</b> and store the result in <b>area</b>. The asterisk '<b>*</b>' character is the operator used for multiplication. To find the total cost, area is first multiplied by the cost per square foot. This equation is placed in brackets in order to separate it from the installation fee, which is added after. The result is then stored in <b>totalCost</b>.
+
<pre>
+
area = length * width;
+
totalCost = (area * costPSF) + installFee;
+
</pre>
+
===Print Output===
+
Finally, print out the total area and total cost on separate lines. To calculate your cost, remember to first divide the total in half as your roommate will be splitting the cost with you. The forward slash '<b>/</b>' character is the operator used for division. Subtract the coupon amount from your portion of the total cost. It's good practice to place parts of an equation in brackets to preserve the proper order of operations. Try removing the brackets around <b>(totalCost / 2)</b> and see if the result changes.
+
<pre>
+
System.out.println("Total area: " + area + " square feet");
+
System.out.println("Total cost: $" + totalCost);
+
System.out.println("Your cost: $" + ((totalCost / 2) - coupon));
+
</pre>
+
  
The program is now complete. Excellent work!
+
        String location;                        //the location
|SolutionCode=import javax.swing.JOptionPane;
+
        String temp;                            //temporary string for initializing
 +
                                                //variables
 +
        //get input
 +
        location = JOptionPane.showInputDialog ("What is the location?");
 +
        temp = JOptionPane.showInputDialog ("What is the current temperature in Celsius?");
 +
        temperature = Double.parseDouble (temp);
  
public class CalculateArea
+
         temp = JOptionPane.showInputDialog ("What is the wind speed in kms per hour?");
{
+
        windspeed = Double.parseDouble (temp);     //converts windspeed to double
    public static void main(String[] args)
+
    {
+
         String input;
+
+
int costPSF = 8;      // Cost of $8 per square foot
+
int installFee = 500; // $500 installation fee
+
int coupon = 50;       // $50 off coupon
+
  
int length;
+
        //calculate windchill
int width;
+
        windchill = 13.12 + .6215*temperature - 11.37 *
int area;
+
        Math.pow(windspeed, .16) + .3965 * temperature * Math.pow(windspeed,.16);
double totalCost;
+
  
input = JOptionPane.showInputDialog("Input length:");
+
        //output
length = Integer.parseInt(input);
+
        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);
  
input = JOptionPane.showInputDialog("Input width:");
+
        System.out.println("Programmed by A. Student");
width = Integer.parseInt(input);
+
        System.out.println("**End of Program**");
  
area = length * width;
+
    }//close main
totalCost = (area * costPSF) + installFee;
+
}//close public class
  
System.out.println("Total area: " + area + " square feet");
 
System.out.println("Total cost: $" + totalCost);
 
System.out.println("Your cost: $" + ((totalCost / 2) - coupon));
 
    }
 
}
 
 
}}
 
}}

Latest revision as of 12:24, 7 December 2011

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