Windchill

From CompSciWiki
Revision as of 18:47, 23 September 2010 by WikiSysop (Talk | contribs)

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 use Scanner 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



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**
 

Primitive Data Types

Wiki chars03.jpg

Solution

Create 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 should normally be used here for fixed numbers; these will be explained next week.

String input;
		
int costPSF = 8;       // Cost of $8 per square foot
int installFee = 500;  // $500 installation fee
int coupon = 50;       // $50 off coupon

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).

int length;
int width;
int area;
double totalCost;

User Input

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 Integer.parseInt() function for this.

input = JOptionPane.showInputDialog("Input length:");
length = Integer.parseInt(input);

input = JOptionPane.showInputDialog("Input width:");
width = Integer.parseInt(input);

Perform Arithmetic

To calculate area, multiply length and width and store the result in area. The asterisk '*' 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 totalCost.

area = length * width;
totalCost = (area * costPSF) + installFee;

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 '/' 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 (totalCost / 2) and see if the result changes.

System.out.println("Total area: " + area + " square feet");
System.out.println("Total cost: $" + totalCost);
System.out.println("Your cost: $" + ((totalCost / 2) - coupon));

The program is now complete. Excellent work!

Code

Solution Code

Back to the Program-A-Day homepage