CS 1401 Assignment #10

Date Assigned: Wednesday, March 30, 2005

Due Date: Wednesday, April 6, 2005, before the beginning of your lab section.

Goals: to learn how to include error-handling mechanisms in an application.

Points: This assignment is worth a total of 20 points. No late assignments will be accepted, but credit will be given for partially completed assignments turned in on time.

Assignment: If the winds are blowing in El Paso, then the summer heat can’t be far behind. Fortunately, it’s a “dry heat,” as we often hear people say, and low humidity is one reason for the good climate here. Low humidity is important because the human body cools off through perspiration (evaporation), and higher humidity in the atmosphere slows evaporation, making humid days feel hotter than the actual temperature might indicate. The National Weather Service has developed a heat index, specifying the apparent temperature based upon the actual temperature and relative humidity, in order to help identify potentially dangerous weather conditions for people at risk. Write an application that creates a calculator to determine the heat index based upon the temperature and relative humidity entered by the user. Although a graphical user interface (GUI) would be a natural addition to this application, you do NOT have to write a GUI for the application. Your application should be able to recover from invalid user input by using try-catch-finally statements to provide error handling. After the user has entered valid input values, the application should compute and display the heat index temperature as well as a warning message about health risks associated with various heat index values. Specific details are provided below:

         Write an application class prompting the user to provide an integer value for temperature in degrees Fahrenheit, and an integer value for relative humidity expressed as a percentage. There are different versions of the heat index formula, and we will be using the shorter version of the formula for this lab. The formula to use for computing the heat index is as follows:

Heat Index = -42.379 + 2.04901523T + 10.14333127R - 0.22475541TR - 6.83783x10-3 T2 - 5.481717x10-2 R2 + 1.22874x10-3 T2 R + 8.5282x10-4 TR2 - 1.99x10-6 T2 R2

Where:

T = temperature in degrees Fahrenheit. Valid values for this formula are 80 to 110 degrees (inclusive).

R = relative humidity. Valid values for this formula are 40 to 100 percent (inclusive).

         Your application class should catch and provide error-handling for two types of errors in data input: NumberFormatException and IllegalArgumentException. You will not need to explicitly throw the NumberFormatException; it will be thrown by the JVM if the user input is a string that cannot be converted to an integer temperature or humidity value. You will need to throw an IllegalArgumentException in an object-creating class (see below). The error handling should include displaying the user’s invalid input value and providing the opportunity for the user to correct the input. For the IllegalArgumentException, print out the message that you define for the exception in the object-creating class below. Do not ask the user to enter a value for humidity until a valid value for temperature has been entered. Hint: You may want to use separate try-catch statements for each variable. To repetitively offer the user a chance to correct invalid input, enclose each whole try-catch statement in a while loop.

 

 

         Write an object-creating class to create a heat index calculator. This calculator uses the temperature and humidity provided by the user and the heat index formula. It should have the following methods:

1.      Constructor method, which takes no parameters. The values provided by the user for temperature and humidity will be checked to make sure they are within the valid ranges for this calculator in the mutator methods described below, to avoid repeating code.

2.      Accessor methods to return the values of temperature, humidity, and heat index.

3.      Mutator method to compute the heat index value in degrees Fahrenheit.

4.      Mutator methods to set the values of temperature and humidity to the values provided by the user, if the values are within the valid ranges for this heat index calculator. These methods should check to make sure the values are valid, and if they are not valid, an IllegalArgumentException should be thrown. This exception will be caught in the application class. As part of the throw statement, include as a parameter a string explaining that the value provided by the user is out of range for this heat index calculator.

5.      Facilitator method to return a string providing a possible warning message about the health hazards associated with the computed heat index value. See below for a list of warning messages for each range of heat index values.

6.      toString method returning a string containing the input temperature, input humidity, and computed heat index value.

 

 

         The messages corresponding with different heat index ranges are as follows:

Category

Heat Index

Message

Extreme Danger

130 F or higher

Heatstroke or sunstroke likely.

Danger

105 – 129 F

Sunstroke, muscle cramps, and/or heat exhaustion likely. Heatstroke possible with prolonged exposure and/or physical activity.

Extreme Caution

90 – 104 F

Sunstroke, muscle cramps, and/or heat exhaustion possible with prolonged exposure and/or physical activity.

Caution

80 – 89 F

Fatigue possible with prolonged exposure and/or physical activity.

         Examples of possible input values and messages displayed are as follows

If user enters

Location Where Exception is Thrown

Message displayed

Action Taken

String “ninety” instead of 90 for temperature

NumberFormatException

Application class. No explicit “throw” statement necessary because JVM will do that.

“You entered: ninety. This is not an integer number. Please enter an integer temperature between 80 and 110 (inclusive) representing degrees Fahrenheit.”

Application repeats prompt for user to enter temperature value.

Integer such as 75 for temperature, which is out of range

IllegalArgumentException

Within the heat index calculator class, inside the mutator method used to set value of temperature.

Application catches exception thrown and displays “The temperature you entered, 75, is out of range for this heat index calculator.”

Application repeats prompt for user to enter temperature value.

(Whole process is repeated for humidity)

 

 

 

Temperature of 100 and humidity of 65%

No error

After computing heat index, message displayed is “The heat index for a temperature of 100 F and relative humidity of 65 % is: 136 F. This heat index value is in the category of Extreme Danger, with heat stroke or sunstroke likely for people in high risk groups.” (2nd)

Calculator object uses values to compute heat index (1st)

 

For extra credit: Define your own exception class as a subclass of Exception. Throw your exception in the heat index calculator class instead of the IllegalArgumentException for either temperature or humidity. Catch your exception in the application class. Do you have to modify the mutator heading in order to throw an exception that is a subclass of Exception?

Deliverables: as announced in the labs and explained in the handouts given to you in the labs.