In: Computer Science
How cold is it outside? The temperature alone is not enough to provide the answer. Other factors including wind speed, relative humidity, and sunshine play important roles in determining coldness outside. In 2001, the National Weather Service (NWS) implemented the new wind-chill temperature to measure the coldness using temperature and wind speed. The formula is:
twc = 35.74 + 0.6215ta - 35.75v0.16 + 0.4275tav0.16
t w c = 3.74 + 0.6215 t a − 35.75 v 0.16 + 0.4275 t a v 0.16
where ta is the outside temperature measured in degrees Fahrenheit, v is the speed measured in miles per hour, and twc is the wind-chill temperature. The formula cannot be used for wind speeds below 2mph or temperatures below -58°F or above 41°F.
Write a program that prompts the user to enter a temperature between -58°F and 41°F and a wind speed greater than or equal to 2 then displays the wind-chill temperature. Use Math.pow(a, b) to compute v0.16. Your class must be named Windchill. Here is a sample run:
Enter a temperature between -58°F and 41°F: 5.3 Enter the wind speed (>= 2) in miles per hour: 6 The wind chill index is -5.567068455881625
This is what I made:
import java.util.Scanner;
public class Windchill
{
public static void main(String[]args)
{
//create Scanner
Scanner s=new Scanner(System.in);
double ta= 5.3;
int v= 6;
double v2= Math.pow(v,.16);
double Windchill= 35.74 + 0.6215 * ta-35.75 * v2+0.4275 * ta *
v2;
//get temperature in Fahrenheit
System.out.println("Enter a temperature between -58°F and 41°F:" +"
"+ ta);
//get wind speed
System.out.println("Enter the wind speed (>= 2) in miles per
hour"+" "+ v);
//get windchill index
System.out.println("The windchill index is"+" "+ Windchill);
}
}
My teacher told me: "your program needs to let the user enter the temperature and wind speed at the keyboard."
How do I fix this? please help.
Please find the updated java program below:
////////////////////Windchill.java////////////////////
import java.util.Scanner;
/**
* Class: Windchill
* 1. Instead of taking direct values for
variables ta and v,
* scanner instance(s) is used to read double and int
value from user console.
* 2. An if condition is introduced to check if the
formula can be used or not i.e to print a message
* if value of v is less than 2 or ta is less than -58
or greater than 41
*/
public class Windchill
{
public static void main(String[]args)
{
//create Scanner
Scanner s=new
Scanner(System.in);
//get temperature in
Fahrenheit
System.out.println("Enter a
temperature between -58°F and 41°F:");
double ta = s.nextDouble();//read
the double value from console
//get wind speed
System.out.println("Enter the wind
speed (>= 2) in miles per hour:");
int v = s.nextInt(); //read the
integer value from console
s.close(); //close the scanner as
all user inputs are taken
if(v < 2 || ta < -58 || ta
> 41){//check if the formula can be used
System.out.println("The windchill index cannot be measured for wind
speeds "
+ "below 2mph or temperatures
below -58°F or above 41°F.");
}else{
double v2=
Math.pow(v,0.16);//get v0.16
//using the
formula
double
windchill= 35.74 + 0.6215 * ta -35.75 * v2 + 0.4275 * ta *
v2;
//print
windchill index
System.out.println("The windchill index is"+" "+ windchill);
}
}
}
============================================
OUTPUT
===========================================
RUN1
Enter a temperature between -58°F and 41°F:
5.3
Enter the wind speed (>= 2) in miles per hour:
6
The windchill index is -5.567068455881625
RUN2
Enter a temperature between -58°F and 41°F:
-60
Enter the wind speed (>= 2) in miles per hour:
5
The windchill index cannot be measured for wind speeds below 2mph
or temperatures below -58°F or above 41°F.