User:Mdomarat/Extralabs/windchill

From CompSciWiki
< User:Mdomarat
Revision as of 10:07, 10 March 2011 by Mdomarat (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


import javax.swing.*;
import java.awt.event.*;



public class windchill extends JFrame {
	public JTextField tempArea;
	public JTextField windArea;
	public JLabel tempLabel;
	public JLabel windLabel;
	public JButton goButton;
	public JTextArea outputArea;
	public JPanel mainPanel;

	private class ButtonListener implements ActionListener {
		public void actionPerformed (ActionEvent e) {

			double windSpeed = Double.parseDouble( windArea.getText());
			double temp = Double.parseDouble ( tempArea.getText());
			double wc = calculateWindchill(windSpeed,temp);

			outputArea.setText( "The windchill is " + wc +".");

		}
	}

	public static void main (String[] args) {
		new windchill();
	}
	
	public windchill () {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("Windchill");

		tempArea = new JTextField(10);
		windArea = new JTextField(10);
		tempLabel = new JLabel("Enter a temperature:");
		windLabel = new JLabel("Enter a windspeed:");

		goButton = new JButton("Calculate");
		goButton.addActionListener(new ButtonListener());

		outputArea = new JTextArea(1,50);
		outputArea.setEditable(false);

		mainPanel = new JPanel();
		
		mainPanel.add(tempLabel);
		mainPanel.add(tempArea);
		mainPanel.add(windLabel);
		mainPanel.add(windArea);
		mainPanel.add(goButton);
		mainPanel.add(outputArea);

		this.add(mainPanel);
		this.setSize(700,100);
		this.setVisible(true);
	}



	public static double calculateWindchill (double windspeed, double temperature) {
	    double velocityPower, windchill;
	    
	    //calculate windchill
	    velocityPower = Math.pow(windspeed, 0.16) ;
	    if (windspeed >= 5) {
	    	windchill = 13.12 + 0.6215 * temperature - 11.37 * velocityPower
	        + 0.3965 * temperature * velocityPower ;
	    } else {
	    	windchill = temperature + (-1.59 + 0.1345 * temperature) * windspeed / 5.0 ;
	    }
	    
	    return windchill;

	}
	
	
}