001package headfirst.observer.WeatherStationObservable;
002
003import java.util.Observable;
004import java.util.Observer;
005
006public class HeatIndexDisplay implements Observer, DisplayElement {
007        float heatIndex = 0.0f;
008
009        public HeatIndexDisplay(Observable observable) {
010                observable.addObserver(this);
011        }
012
013        public void update(Observable observable, Object arg) {
014                if (observable instanceof WeatherData) {
015                        WeatherData weatherData = (WeatherData)observable;
016                        float t = weatherData.getTemperature();
017                        float rh = weatherData.getHumidity();
018                        heatIndex = (float)
019                                        (
020                                                        (16.923 + (0.185212 * t)) +
021                                                        (5.37941 * rh) -
022                                                        (0.100254 * t * rh) +
023                                                        (0.00941695 * (t * t)) +
024                                                        (0.00728898 * (rh * rh)) +
025                                                        (0.000345372 * (t * t * rh)) -
026                                                        (0.000814971 * (t * rh * rh)) +
027                                                        (0.0000102102 * (t * t * rh * rh)) -
028                                                        (0.000038646 * (t * t * t)) +
029                                                        (0.0000291583 * (rh * rh * rh)) +
030                                                        (0.00000142721 * (t * t * t * rh)) +
031                                                        (0.000000197483 * (t * rh * rh * rh)) -
032                                                        (0.0000000218429 * (t * t * t * rh * rh)) +
033                                                        (0.000000000843296 * (t * t * rh * rh * rh)) -
034                                                        (0.0000000000481975 * (t * t * t * rh * rh * rh)));
035                        display();
036                }
037        }
038
039        public void display() {
040                System.out.println("Heat index is " + heatIndex);
041        }
042}