Difference between revisions of "Humidex"

From CompSciWiki
Jump to: navigation, search
m (removed repetition: you have you have => you have)
Line 111: Line 111:
 
|SideSectionTitle=Humidex
 
|SideSectionTitle=Humidex
 
|SideSection=
 
|SideSection=
[[Image:Wiki_start01.jpg|center]]<BR>
+
[[Image:RainGear_casestudy.jpg|center]]<BR>
  
 
}}
 
}}

Revision as of 17:17, 4 April 2011

Back to the Case Studies homepage

Problem

Humidex is a measurement that gives people an idea of how hot it feels, as opposed to how hot it actually is. For example, on days with a high temperature and high humidity, people often think it's hotter than it actually is. Write a complete Java program that will ask the user for the dew point and the air temperature. Then use the formulas given to calculate and output the humidex.

Input

Use Scanner class method nextDouble to prompt the user to input D (dew point) and T (air temperature). Use System.out.println to provide an input prompt. Remember to tell the user to enter values in Celsius.

Processing

The formula for calculating the humidex is:

R = 6.11 * e[5417.7530 * ( (1/273.16) - (1/(273.16 + D)) ) ]
H = (0.5555) * (R - 10.0)
humidex = T + H

Notes

  • e is Euler's number (e = 2.71828182845904523536....) To access Euler’s number use the Math constant Math.E
  • D is the dew point, in Celsius. The user will input this value.
  • T is the air temperature, in Celsius. The user will also input this value.

Output

Echo the input and then print the output using System.out.println. Use Math.round() to round all output to one decimal place.

 

Humidex

RainGear casestudy.jpg

Solution

Input

The problem requires two doubles to be read in. One is the dewpoint (in Celsius) and the other is the air temperature (in Celsius). When asking the user to input the numbers into the response, you should prompt them each time you ask them for what piece of information you need. Using the Scanner class, you can read doubles in easily.

Scanner

The java.util.Scanner class (added in Java 5) allows simple console and file input.

Example

Scanner keyboard = new Scanner(System.in);
double  number   = keyboard.nextDouble();

Processing

Once you have your two Doubles, you can now start programing to work out the humidex.

Math.E

With the formula given to you, it needs the constant e, which can be given with the Math.E()

Example

//The double value that is closer than any other to e, the base of the natural logarithms.
Math.E() //returns the value e

Math.Power

One way to solve the problem is to use Math.power. For x, it would be the constant e. y will be the part of the formula that x is raised too.

Example

// pow(x, y) returns the x raised to the yth power.
Math.pow(2.0,2.0) //returns 2.0 to the power of 2.0, which is 4.0

Math.exp

The other option for solving this is to use Math.exp(). Compared to when you would be using Math.Power, you will only need to input one value. This is because e is already set to the base for the power. x will be the part of the formula that e is raised too.

Example

//exp(x) returns e raised to the xth power
Math.exp( 2 );// returns e squared (or e * e), which is approximately 7.3890 

Output

Once you have your value from your formula, you will have to round it to an appropriate value before you print it out. You will be able to achieve this by using Math.Round.

Math.Round

Math.round is a method that will round a double to the nearest integer. So, if you want to use this method to round a double to a number with decimal places, you must first make it bigger by multiplying by a power of 10, then round it to an integer, and then divide it by the same power of 10.

Example

//Math.round(x) returns x to the nearest integer
x = Math.round (34.4567 * 1000) / 1000.0 // returns the double 34.4567 rounded to THREE decimals

Output

Once you have the humidex value rounded to the appropriate decimal place, print it out using System.out.println().

Code

Solution Code

Back to the Case Studies homepage