2014年10月24日金曜日

開発環境

Head Firstデザインパターン ―頭とからだで覚えるデザインパターンの基本 (Eric Freeman 著、Elisabeth Freeman 著、Kathy Sierra 著、Bert Bates 著、佐藤 直生 監訳、木下 哲也 翻訳、有限会社 福龍興業 翻訳、オライリージャパン)の2章(Observerパターン: オブジェクトを事情通に)、自分で考えてみよう(p.61)を解いてみる。

その他参考書籍

自分で考えてみよう(p.61)

コード(BBEdit, Emacs)

WeatherStationHeatIndex.java


public class WeatherStationHeatIndex {
    public static void main(String[] args) {
        WeatherData weatherData = new WeatherData();
        CurrentConditionsDisplay currentDisplay =
            new CurrentConditionsDisplay(weatherData);
        HeatIndexDisplay headIndexDisplay = new HeatIndexDisplay(weatherData);

        weatherData.setMeasurements(27, 65, 30.4f);
        weatherData.setMeasurements(28, 70, 29.2f);
        weatherData.setMeasurements(26, 90, 292.f);
    }
}

HeatIndexDisplay.java

public class HeatIndexDisplay implements Observer, Display {
    private float heatIndex;
    private Subject weatherData;

    public HeatIndexDisplay(Subject weatherData) {
        this.weatherData = weatherData;
        weatherData.registerObserver(this);
    }
    public void update(float temperature, float humidity, float pressure) {
        heatIndex = computeHeatIndex(1.8f * temperature + 32, humidity);
        display();
    }

    public void display() {
        System.out.println("熱指数:" + heatIndex);
    }
        
    private float computeHeatIndex(float t, float rh) {
        float index =
            (float)(
                    (16.923 + (0.185212 * t) + (5.37941 * rh) -
                     (0.100254 * t * rh) +
                     (0.00941695 * (t * t)) + (0.00728898 * (rh * rh)) +
                     (0.000345372 * (t * t * rh)) -
                     (0.000814971 * (t * rh * rh)) +
                     (0.0000102102 * (t * t * rh * rh)) -
                     (0.000038646 * (t * t * t)) +
                     (0.0000291583 * (rh * rh * rh)) +
                     (0.00000142721 * (t * t * t * rh)) +
                     (0.000000197483 * (t * rh * rh * rh)) -
                     (0.0000000218429 * (t * t * t * rh * rh)) +     
                     0.000000000843296 * (t * t * rh * rh * rh)) -
                    (0.0000000000481975 * (t * t * t * rh * rh * rh))
                    );
        return index;
    }
}

入出力結果(Terminal)

$ javac HeatIndexDisplay.java
$ java WeatherStationHeatIndex
現在の気象状況:温度27.0度 湿度65.0%
熱指数:83.77633
現在の気象状況:温度28.0度 湿度70.0%
熱指数:87.61084
現在の気象状況:温度26.0度 湿度90.0%
熱指数:85.141815
$

0 コメント:

コメントを投稿