Pages

Saturday, 11 June 2016

Chapter 3 Exercise 20, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*3.20 (Science: wind-chill temperature) Programming Exercise 2.17 gives a formula to compute the wind-chill temperature. The formula is valid for temperatures in the range between −58ºF and 41ºF and wind speed greater than or equal to 2. Write a program that prompts the user to enter a temperature and a wind speed. The program displays the wind-chill temperature if the input is valid; otherwise, it displays a message indicating whether the temperature and/or wind speed is invalid.


import java.util.Scanner;
 
public class ProgrammingEx3_20 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out
    .print("Enter the temperature in Fahrenheit between -58°F and 41°F:");
  double ta = input.nextDouble();
  System.out.print("Enter the wind speed (>=2) in miles per hour:");
  double v = input.nextDouble();
  double twc = 0;
 
  if (ta < -58 || ta > 41) {
   System.out.println("The temperature input is incorrect.");
   twc = -1;
  }
 
  if (v < 2) {
   System.out.println("The wind speed input is incorrect.");
   twc = -1;
  }
 
  if (twc == -1) {
   System.exit(0);
  }
 
  twc = 35.74 + 0.6215 * ta - 35.75 * Math.pow(v, 0.16) + 0.4275 * ta
    * Math.pow(v, 0.16);
 
  System.out.print("The wind chill index is " + twc);
 
 }
}

2 comments: