0

I am writing a java applet that calculate heat index and wind chill. it requires the user to input temperature , wind speed and humidity, and then let the user choose either Metric ( kph, °C) or English (mph, °F)

I have some problems with the code. First, the Calculate button doesn't work properly. Second, the sign of °C or °F don't appear on the first line on the right side of "Enter Temperature: " and on the right sde of "Wind Chill: ".

if you run the program , you will understand what I mean.

here is my code. I hope someone would be able to help me here. thank you

import java.text.DecimalFormat;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WeatherPanel extends JPanel
{
	DecimalFormat fmt = new DecimalFormat("0.##");
	private static final long serialVersionUID = 1L;

	public static final int INT_PANEL_WIDTH = 500;

	public static final int INT_PANEL_HEIGHT = 300;	
	
	 JLabel jlTemperature;
	 JLabel jlWindSpeed;
	 JLabel jlHumidity;
	 JLabel jlTemperatureUnit;
	 JLabel jlWindSpeedUnit;
	 JLabel jlPercent;
	 
	 JLabel jlWindChill;
	 JLabel jlWindChillResult;
	 JLabel jlHeatIndex;
	 JLabel jlHeatIndexResult;	 
	  JTextField jtfTemperature;
	 JTextField jtfWindSpeed;
	 JTextField jtfHumidity;
	 JButton jbCalculate;
	 JButton jbClear;
	 JRadioButton jrbMetric;
	 JRadioButton jrbEnglish;
	 JPanel jpTemperatureResult;
	 JPanel jpWindSpeedResult;
	 JPanel jpHumidityResult;
	 JPanel jpButton;
	 JPanel jpRadioButton;
	 JPanel jpResult;
	 
	 public WeatherPanel()
	 {
		 jlTemperature = new JLabel("Enter Temperature: ");
		 jlWindSpeed = new JLabel("Enter Wind Speed: ");
		 jlHumidity = new JLabel("Enter Humidity: ");
		 jlPercent = new JLabel("%");
		 jlTemperatureUnit = new JLabel("°F");
		 jlWindSpeedUnit = new JLabel("MPH");		
		 jbCalculate = new JButton("Calculate!");
		 jbClear = new JButton("Clear");
		 jrbMetric = new JRadioButton("Metric");
		 jrbEnglish = new JRadioButton("English", true);
		 jtfTemperature = new JTextField(5);
		 jtfWindSpeed = new JTextField(5);
		 jtfHumidity = new JTextField(5);
		 jlWindChill = new JLabel("Wind Chill: ");
		 jlWindChillResult = new JLabel();
		 jlHeatIndex = new JLabel("Heat Index: ");
		 jlHeatIndexResult = new JLabel();
				 
		 jpTemperatureResult = new JPanel();
		 jpWindSpeedResult = new JPanel();
		 jpHumidityResult = new JPanel();	
		 jpButton = new JPanel();
		 jpRadioButton = new JPanel();
		 jpResult = new JPanel();		 
		 
		 ButtonGroup group = new ButtonGroup();
		 group.add(jrbMetric);
		 group.add(jrbEnglish);	
		 
		 WeatherListener listener = new WeatherListener();
		
		 jrbMetric.addActionListener(listener);
		 jrbEnglish.addActionListener(listener);
		 jtfTemperature.addActionListener(listener);
		 jtfWindSpeed.addActionListener(listener);
		 jtfHumidity.addActionListener(listener); 
		 jbCalculate.addActionListener(listener);
		 jbClear.addActionListener(listener);
		 
		 jpTemperatureResult.add(jlTemperature);
		 jpTemperatureResult.add(jtfTemperature);
		 jpTemperatureResult.add(jlTemperatureUnit);
		 jpWindSpeedResult.add(jlWindSpeed);
		 jpWindSpeedResult.add(jtfWindSpeed);
		 jpWindSpeedResult.add(jlWindSpeedUnit);
		 jpHumidityResult.add(jlHumidity);
		 jpHumidityResult.add(jtfHumidity);
		 jpHumidityResult.add(jlPercent);		 
		 jpRadioButton.add( jrbMetric);
		 jpRadioButton.add( jrbEnglish);
		 jpButton.add(jbCalculate);
		 jpButton.add(jbClear);
		 jpResult.add(jlWindChill);
		 jpResult.add(jlWindChillResult);
		 jpResult.add(jlTemperatureUnit);
		 jpResult.add(jlHeatIndex);
		 jpResult.add(jlHeatIndexResult);
		 jpResult.add(jlTemperatureUnit);		
		
		 
		 Weather WeatherObj = new Weather();		
		 WeatherObj.getTemperature();		
		 WeatherObj.getRelativeHumidity();		
		 WeatherObj.getWindSpeed();
		 
		 jlWindChillResult.setText(fmt.format(WeatherObj.windChill()));
	     jlHeatIndexResult.setText(fmt.format(WeatherObj.heatIndex())); 		 	 
		 jtfTemperature.setText(fmt.format( WeatherObj.getTemperature()));
		 jtfWindSpeed.setText(fmt.format(WeatherObj.getWindSpeed()));
		 jtfHumidity.setText(fmt.format(WeatherObj.getRelativeHumidity()));		 
				 
		 setLayout(new GridLayout(6,1));
		 add(jpTemperatureResult);
		 add(jpWindSpeedResult);
		 add(jpHumidityResult);
		 add(jpRadioButton);
		 add(jpButton);
		 add(jpResult);		 
		 
		setPreferredSize(new Dimension( INT_PANEL_WIDTH, INT_PANEL_HEIGHT));		
	 }	 

	 private class WeatherListener implements ActionListener
	 {
		 public void actionPerformed ( ActionEvent event)
		 {			 			
			 double dblTemperature;
			 double dblHumidity;
			 double dblWindSpeed;
			 			 
			 String strTemperature;
			 String strWindSpeed;
			 String strHumidity;			 	 	
			 
			
			strTemperature = jtfTemperature.getText();
			strWindSpeed = jtfWindSpeed.getText();
			strHumidity = jtfHumidity.getText();
			
			dblTemperature = Double.parseDouble(strTemperature);
			dblHumidity = Double.parseDouble(strHumidity);
			dblWindSpeed = Double.parseDouble(strWindSpeed);
			
			Object objSource = event.getSource();
			
			Weather WeatherObj1 = new Weather(false,dblTemperature, dblHumidity, dblWindSpeed);		

			
			jlWindChillResult.setText(fmt.format(WeatherObj1.windChill()));
			jlHeatIndexResult.setText(fmt.format(WeatherObj1.heatIndex()));
			
						
			
			if(objSource == jrbMetric)
			{
				Weather WeatherObj2 = new Weather(true,dblTemperature, dblHumidity, dblWindSpeed);		
				WeatherObj2.setUnits(true);
				WeatherObj2.getUnits();
				jlTemperatureUnit.setText("°C");
				jlWindSpeedUnit.setText("Kph");				
			
				jtfTemperature.setText(fmt.format(Weather.fahrenheitToCelsius(dblTemperature)));
				jtfWindSpeed.setText(fmt.format(dblWindSpeed*1.609));				
							
				jlWindChillResult.setText(fmt.format(WeatherObj2.windChill()));
				jlHeatIndexResult.setText(fmt.format(WeatherObj2.heatIndex()));
				
				
			}
			else if(objSource == jrbEnglish)
			{
				Weather WeatherObj2 = new Weather(false,dblTemperature, dblHumidity, dblWindSpeed);	
				WeatherObj2.setUnits(false);
				WeatherObj2.getUnits();
				jlTemperatureUnit.setText("°F");
				jlWindSpeedUnit.setText("MPH");				
			
				jtfTemperature.setText(fmt.format(Weather.celsiusToFahrenheit(dblTemperature)));
				jtfWindSpeed.setText(fmt.format(dblWindSpeed/1.609));
				jlWindChillResult.setText(fmt.format(WeatherObj2.windChill()));
				jlHeatIndexResult.setText(fmt.format(WeatherObj2.heatIndex()));				
			
			}
			
			if(event.getSource() == jbClear)
			{
				jtfTemperature.setText("");
				jtfWindSpeed.setText("");	
				jtfHumidity.setText("");
				jlWindChillResult.setText("");
				jlHeatIndexResult.setText("");
				
			}
			
			if(event.getSource()== jbCalculate  )
			{
				strTemperature = jtfTemperature.getText();
				strWindSpeed = jtfWindSpeed.getText();
				strHumidity = jtfHumidity.getText();
				
				dblTemperature = Double.parseDouble(strTemperature);
				dblHumidity = Double.parseDouble(strHumidity);
				dblWindSpeed = Double.parseDouble(strWindSpeed);
				if(objSource == jrbEnglish)
				{
					Weather WeatherObj3 = new Weather (false,dblTemperature, dblHumidity, dblWindSpeed);									
					WeatherObj3.setUnits(false);
					WeatherObj3.getUnits();	
					jlWindChillResult.setText(fmt.format(WeatherObj3.windChill()));
					jlHeatIndexResult.setText(fmt.format(WeatherObj3.heatIndex()));							
				}
				else if(objSource == jrbMetric)
				{
					Weather WeatherObj3 = new Weather (true,dblTemperature, dblHumidity, dblWindSpeed);									
					WeatherObj3.setUnits(true);
					WeatherObj3.getUnits();	
					jlWindChillResult.setText(fmt.format(WeatherObj3.windChill()));
					jlHeatIndexResult.setText(fmt.format(WeatherObj3.heatIndex()));							
				}					
			}		
			
					 
	    }//public void actionPerformed 
		 
		 
	}// private class WeatherListener
	 
	

}//public class WeatherPanel 

public class Weather
{
	boolean blnUnits;
	double dblTemperature;
	double dblWindSpeed;
	double dblRelativeHumidity;
	
	public static double celsiusToFahrenheit( double dblCelsiusIn )
	{
		double dblFahrenheit;	
		dblFahrenheit = (double)9/5 * dblCelsiusIn + 32;		
		return dblFahrenheit;
	}
	
	public static double fahrenheitToCelsius( double dblFahrenheitIn )
	{
		double dblCelsius;
		dblCelsius = (double)5/9 * (dblFahrenheitIn - 32);
		return dblCelsius; 
	}
	
	//Constructors
	public Weather( )
	{
		blnUnits = false;
		dblTemperature = 70;
		dblWindSpeed = 5;
		dblRelativeHumidity = 50;
	}
	
	public Weather( boolean blnUnitsIn,
            double  dblTemperatureIn,
            double  dblRelativeHumidityIn,
            double  dblWindSpeedIn
          )
	{
		blnUnits = blnUnitsIn;
		dblTemperature = dblTemperatureIn;
		dblWindSpeed = dblWindSpeedIn;
		dblRelativeHumidity = dblRelativeHumidityIn;
	}
	
	public boolean getUnits()
	{
		return blnUnits;
	}
	
	public void setUnits( boolean blnUnitsIn )
	{
		blnUnits = blnUnitsIn;
		if(blnUnits == true)
		{
			dblTemperature = fahrenheitToCelsius(dblTemperature);
			dblWindSpeed = dblWindSpeed * 1.609;
		}
		if(blnUnits == false)
		{
			dblTemperature = celsiusToFahrenheit(dblTemperature);
			dblWindSpeed = dblWindSpeed / 1.609;
		}
		
	
		// if the Units have changed, must convert the Temperature and Wind Speed to the proper values. 
	}
	
	public double getTemperature()
	{
		return dblTemperature;
	}
	
	public void setTemperature( double dblTemperatureIn )
	{
		dblTemperature = dblTemperatureIn;
	}
	
	public double getRelativeHumidity()
	{
		return dblRelativeHumidity;
	}
	
	public void setRelativeHumidity( double dblRelativeHumidityIn )
	{
		dblRelativeHumidity = dblRelativeHumidityIn;
	}
	
	public double getWindSpeed()
	{
		return dblWindSpeed;
	}
	
	public void setWindSpeed( double dblWindSpeedIn )
	{
		dblWindSpeed = dblWindSpeedIn;
	}
	
	public String temperatureUnits()
	{
		if(blnUnits = false)
			return "°F";
		else 
			return "°C";
	}
	
	public String speedUnits()
	{
		if(blnUnits = false)
			return "MPH";
		else 
			return "Kph";
	}
	
	public double windChill()
	{
		double dblWindChillTemperature;
		double dblTemperatureTemp;
		if(blnUnits == false)
		{
			dblWindChillTemperature = 35.74 + 0.6125 * dblTemperature - 35.75 * dblWindSpeed * 0.16 + 0.4275 * dblTemperature * dblWindSpeed * 0.16;
			return dblWindChillTemperature;
		}
		else
		{
			dblTemperatureTemp =  celsiusToFahrenheit(dblTemperature);
			dblWindChillTemperature = 35.74 + 0.6125 * dblTemperatureTemp - 35.75 * dblWindSpeed * 0.16 + 0.4275 * dblTemperatureTemp * dblWindSpeed * 0.16;
			dblWindChillTemperature = fahrenheitToCelsius(dblWindChillTemperature);
			return dblWindChillTemperature;
		}
	}	
	public double heatIndex()
	{
		double dblHeatIndex;
		double dblTemperatureTemp;
		if(blnUnits == false)
		{
			dblHeatIndex = -42.379 + 2.04901523 * dblTemperature + 10.14333127 * dblRelativeHumidity - 0.22475541 * dblTemperature * dblRelativeHumidity
							- 0.00683783 * dblTemperature * dblTemperature - 0.05481717 * dblRelativeHumidity * dblRelativeHumidity + 0.00122874 * dblTemperature
							* dblTemperature * dblRelativeHumidity + 0.00085282 * dblTemperature * dblRelativeHumidity * dblRelativeHumidity - 0.00000199 
							* dblTemperature * dblTemperature * dblRelativeHumidity * dblRelativeHumidity;
			return dblHeatIndex;				
		}
		else
		{
			dblTemperatureTemp = celsiusToFahrenheit(dblTemperature);
			
			dblHeatIndex = -42.379 + 2.04901523 * dblTemperatureTemp + 10.14333127 * dblRelativeHumidity - 0.22475541 * dblTemperatureTemp * dblRelativeHumidity
			- 0.00683783 * dblTemperatureTemp * dblTemperatureTemp - 0.05481717 * dblRelativeHumidity * dblRelativeHumidity + 0.00122874 * dblTemperatureTemp
			* dblTemperatureTemp * dblRelativeHumidity + 0.00085282 * dblTemperatureTemp * dblRelativeHumidity * dblRelativeHumidity - 0.00000199 
			* dblTemperatureTemp * dblTemperatureTemp * dblRelativeHumidity * dblRelativeHumidity;
			
			dblHeatIndex = fahrenheitToCelsius(dblHeatIndex);
			
			return dblHeatIndex;			
		}
	}
	
	public String toString()
	{
		return "Wind Chill: " + windChill() + " " + temperatureUnits() + "   Heat Index: " + heatIndex() + " " + temperatureUnits();

	}		

}

import java.awt.Dimension;


import javax.swing.*;


public 	class		WeatherCalc
				extends	JApplet
{

	private static final long	serialVersionUID	= 1L;

	private static final String	STR_FRAME_TITLE = "DPR205 - Weather Calculator";

		private	static				WeatherPanel spnlWeather;
	
	public static void main( String[] args )
  {
        JFrame jfWeather;

    WeatherPanel spnlWeather;

    
    jfWeather = new JFrame( STR_FRAME_TITLE );
    jfWeather.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    spnlWeather = new WeatherPanel();

    jfWeather.getContentPane().add( spnlWeather );
    jfWeather.pack();
    jfWeather.setVisible( true );

    return;

  } 
	public	void	init()
	{
    WeatherCalc.spnlWeather = new WeatherPanel();
    this.resize( new Dimension(	WeatherPanel.INT_PANEL_WIDTH,
    														WeatherPanel.INT_PANEL_HEIGHT
    													)
    					 );
    return;
	}	//	public	void	init()

	public	void	start()
	{
    this.add( WeatherCalc.spnlWeather );

    return;
	}	//	public	void	start()

} // public class WeatherCalc

Edited by YingKang: n/a

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by YingKang
0

The problems I mentioned earlier are solved . However, I have encountered another one. When the user enter some information, the answer for heat index is not correct. actually the first time I run he code with the default input, the heat index is correct, but after I change some input, the heat index is not correct any more. Where is my problem? Can anyone help please? Thanks

package gui;


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

import application.Weather;

public class WeatherPanel extends JPanel
{
    DecimalFormat fmt = new DecimalFormat("0.##");
    private static final long serialVersionUID = 1L;

    public static final int INT_PANEL_WIDTH = 500;

    public static final int INT_PANEL_HEIGHT = 300;    
    
     JLabel jlTemperature;
     JLabel jlWindSpeed;
     JLabel jlHumidity;
     JLabel jlTemperatureUnit1;
     JLabel jlTemperatureUnit2;
     JLabel jlTemperatureUnit3;
     JLabel jlWindSpeedUnit;
     JLabel jlPercent;
     
     JLabel jlWindChill;
     JLabel jlWindChillResult;
     JLabel jlHeatIndex;
     JLabel jlHeatIndexResult;     
     JTextField jtfTemperature;
     JTextField jtfWindSpeed;
     JTextField jtfHumidity;
     JButton jbCalculate;
     JButton jbClear;
     JRadioButton jrbMetric;
     JRadioButton jrbEnglish;
     JPanel jpTemperatureResult;
     JPanel jpWindSpeedResult;
     JPanel jpHumidityResult;
     JPanel jpButton;
     JPanel jpRadioButton;
     JPanel jpResult;
     
     public WeatherPanel()
     {
         jlTemperature = new JLabel("Enter Temperature: ");
         jlWindSpeed = new JLabel("Enter Wind Speed: ");
         jlHumidity = new JLabel("Enter Humidity: ");
         jlPercent = new JLabel("%");
         jlTemperatureUnit1 = new JLabel("°F");
         jlTemperatureUnit2 = new JLabel("°F");
         jlTemperatureUnit3 = new JLabel("°F");
         jlWindSpeedUnit = new JLabel("MPH");        
         jbCalculate = new JButton("Calculate!");
         jbClear = new JButton("Clear");
         jrbMetric = new JRadioButton("Metric");
         jrbEnglish = new JRadioButton("English", true);
         jtfTemperature = new JTextField(5);
         jtfWindSpeed = new JTextField(5);
         jtfHumidity = new JTextField(5);
         jlWindChill = new JLabel("Wind Chill: ");
         jlWindChillResult = new JLabel();
         jlHeatIndex = new JLabel("Heat Index: ");
         jlHeatIndexResult = new JLabel();
                 
         jpTemperatureResult = new JPanel();
         jpWindSpeedResult = new JPanel();
         jpHumidityResult = new JPanel();    
         jpButton = new JPanel();
         jpRadioButton = new JPanel();
         jpResult = new JPanel();         
         
         ButtonGroup group = new ButtonGroup();
         group.add(jrbMetric);
         group.add(jrbEnglish);                     
         
         WeatherListener listener = new WeatherListener();
        
         jrbMetric.addActionListener(listener);
         jrbEnglish.addActionListener(listener);
         jtfTemperature.addActionListener(listener);
         jtfWindSpeed.addActionListener(listener);
         jtfHumidity.addActionListener(listener); 
         jbCalculate.addActionListener(listener);
         jbClear.addActionListener(listener);
         
         jpTemperatureResult.add(jlTemperature);
         jpTemperatureResult.add(jtfTemperature);
         jpTemperatureResult.add(jlTemperatureUnit1);
         jpWindSpeedResult.add(jlWindSpeed);
         jpWindSpeedResult.add(jtfWindSpeed);
         jpWindSpeedResult.add(jlWindSpeedUnit);
         jpHumidityResult.add(jlHumidity);
         jpHumidityResult.add(jtfHumidity);
         jpHumidityResult.add(jlPercent);         
         jpRadioButton.add( jrbMetric);
         jpRadioButton.add( jrbEnglish);
         jpButton.add(jbCalculate);
         jpButton.add(jbClear);
         jpResult.add(jlWindChill);
         jpResult.add(jlWindChillResult);
         jpResult.add(jlTemperatureUnit2);
         jpResult.add(jlHeatIndex);
         jpResult.add(jlHeatIndexResult);
         jpResult.add(jlTemperatureUnit3);        
        
         
         Weather WeatherObj = new Weather();        
         WeatherObj.getTemperature();    
         WeatherObj.getRelativeHumidity();        
         WeatherObj.getWindSpeed();
         
         jlWindChillResult.setText(fmt.format(WeatherObj.windChill()));
         jlHeatIndexResult.setText(fmt.format(WeatherObj.heatIndex()));               
         jtfTemperature.setText(fmt.format( WeatherObj.getTemperature()));
         jtfWindSpeed.setText(fmt.format(WeatherObj.getWindSpeed()));
         jtfHumidity.setText(fmt.format(WeatherObj.getRelativeHumidity()));         
                 
         setLayout(new GridLayout(6,1));
         add(jpTemperatureResult);
         add(jpWindSpeedResult);
         add(jpHumidityResult);
         add(jpRadioButton);
         add(jpButton);
         add(jpResult);         
         
        setPreferredSize(new Dimension( INT_PANEL_WIDTH, INT_PANEL_HEIGHT));        
     }     

     private class WeatherListener implements ActionListener
     {
         public void actionPerformed ( ActionEvent event)
         {                         
             double dblTemperature;
             double dblHumidity;
             double dblWindSpeed;
        
             
             String strTemperature;
             String strWindSpeed;
             String strHumidity;                      
             
            
            strTemperature = jtfTemperature.getText();
            strWindSpeed = jtfWindSpeed.getText();
            strHumidity = jtfHumidity.getText();
            
            dblTemperature = Double.parseDouble(strTemperature);
            dblHumidity = Double.parseDouble(strHumidity);
            dblWindSpeed = Double.parseDouble(strWindSpeed);    
            
            
            if(jrbEnglish.isSelected())
            {
                Weather WeatherObj1 = new Weather(false,dblTemperature, dblHumidity, dblWindSpeed);        
                
                jlWindChillResult.setText(fmt.format(WeatherObj1.windChill()));
                jlHeatIndexResult.setText(fmt.format(WeatherObj1.heatIndex()));
            }    
            else if(jrbMetric.isSelected())
            {
                Weather WeatherObj1 = new Weather(true,dblTemperature, dblHumidity, dblWindSpeed);                    
            
                    
                jlWindChillResult.setText(fmt.format(WeatherObj1.windChill()));
                jlHeatIndexResult.setText(fmt.format(WeatherObj1.heatIndex()));
            }

            Object objSource = event.getSource();
            
            if(objSource == jrbMetric)
            {
                
                Weather WeatherObj2 = new Weather(true,dblTemperature, dblHumidity, dblWindSpeed);        
                WeatherObj2.setUnits(true);
                WeatherObj2.getUnits();    
                jlTemperatureUnit1.setText(WeatherObj2.temperatureUnits());
                jlTemperatureUnit2.setText(WeatherObj2.temperatureUnits());
                jlTemperatureUnit3.setText(WeatherObj2.temperatureUnits());
                jlWindSpeedUnit.setText(WeatherObj2.speedUnits());    
            
                jtfTemperature.setText(fmt.format(Weather.fahrenheitToCelsius(dblTemperature)));
                jtfWindSpeed.setText(fmt.format(dblWindSpeed*1.61));                
                            
                jlWindChillResult.setText(fmt.format(WeatherObj2.windChill()));
                jlHeatIndexResult.setText(fmt.format(WeatherObj2.heatIndex()));
                            
            }            
            else if(objSource == jrbEnglish)
            {
                
                Weather WeatherObj2 = new Weather(false,dblTemperature, dblHumidity, dblWindSpeed);    
                WeatherObj2.setUnits(false);
                WeatherObj2.getUnits();        
            
                jlTemperatureUnit1.setText(WeatherObj2.temperatureUnits());
                jlTemperatureUnit2.setText(WeatherObj2.temperatureUnits());
                jlTemperatureUnit3.setText(WeatherObj2.temperatureUnits());
                jlWindSpeedUnit.setText(WeatherObj2.speedUnits());
                
                jtfTemperature.setText(fmt.format(Weather.celsiusToFahrenheit(dblTemperature)));
                jtfWindSpeed.setText(fmt.format(dblWindSpeed/1.61));
                jlWindChillResult.setText(fmt.format(WeatherObj2.windChill()));
                jlHeatIndexResult.setText(fmt.format(WeatherObj2.heatIndex()));        
            
            }
            
            
            if(event.getSource() == jbClear)
            {
                jtfTemperature.setText("");
                jtfWindSpeed.setText("");    
                jtfHumidity.setText("");
                jlWindChillResult.setText("");
                jlHeatIndexResult.setText("");                            
            }
            
            if(event.getSource()== jbCalculate  )
            {
                strTemperature = jtfTemperature.getText();
                strWindSpeed = jtfWindSpeed.getText();
                strHumidity = jtfHumidity.getText();
                
                dblTemperature = Double.parseDouble(strTemperature);
                dblHumidity = Double.parseDouble(strHumidity);
                dblWindSpeed = Double.parseDouble(strWindSpeed);                
            
                if( jrbEnglish.isSelected())
                {
                    Weather WeatherObj3 = new Weather (false,dblTemperature, dblHumidity, dblWindSpeed);                    
                            
                    jlWindChillResult.setText(fmt.format(WeatherObj3.windChill()));
                    jlHeatIndexResult.setText(fmt.format(WeatherObj3.heatIndex()));                            
                }
                else if(jrbMetric.isSelected())
                {
                    Weather WeatherObj3 = new Weather (true,dblTemperature, dblHumidity, dblWindSpeed);                    
                
                    jlWindChillResult.setText(fmt.format(WeatherObj3.windChill()));
                    jlHeatIndexResult.setText(fmt.format(WeatherObj3.heatIndex()));                            
                }                    
            }        
                                 
        }//public void actionPerformed          
         
    }// private class WeatherListener 
    
}//public class WeatherPanel 

package application;



public class Weather
{
    boolean blnUnits;
    double dblTemperature;
    double dblWindSpeed;
    double dblRelativeHumidity;
    
    public static double celsiusToFahrenheit( double dblCelsiusIn )
    {
        double dblFahrenheit;    
        dblFahrenheit = (double)9/5 * dblCelsiusIn + 32;    
        return dblFahrenheit;
    }
    
    public static double fahrenheitToCelsius( double dblFahrenheitIn )
    {
        double dblCelsius;
        dblCelsius = (double)5/9 * (dblFahrenheitIn - 32);
        return dblCelsius; 
    }
    
    //Constructors
    public Weather( )
    {
        blnUnits = false;
        dblTemperature = 70;
        dblWindSpeed = 5;
        dblRelativeHumidity = 50;
    }
    
    public Weather( boolean blnUnitsIn,
            double  dblTemperatureIn,
            double  dblRelativeHumidityIn,
            double  dblWindSpeedIn
          )
    {
        blnUnits = blnUnitsIn;
        dblTemperature = dblTemperatureIn;
        dblWindSpeed = dblWindSpeedIn;
        dblRelativeHumidity = dblRelativeHumidityIn;
    }
    
    public boolean getUnits()
    {
        return blnUnits;
    }
    
    public void setUnits( boolean blnUnitsIn )
    {
        blnUnits = blnUnitsIn;
        if(blnUnits == true)
        {
            dblTemperature = fahrenheitToCelsius(dblTemperature);
            dblWindSpeed = dblWindSpeed * 1.61;
        }
        if(blnUnits == false)
        {
            dblTemperature = celsiusToFahrenheit(dblTemperature);
            dblWindSpeed = dblWindSpeed / 1.61;
        }
        
    
        // if the Units have changed, must convert the Temperature and Wind Speed to the proper values. 
    }
    
    public double getTemperature()
    {
        return dblTemperature;
    }
    
    public void setTemperature( double dblTemperatureIn )
    {
        dblTemperature = dblTemperatureIn;
    }
    
    public double getRelativeHumidity()
    {
        return dblRelativeHumidity;
    }
    
    public void setRelativeHumidity( double dblRelativeHumidityIn )
    {
        dblRelativeHumidity = dblRelativeHumidityIn;
    }
    
    public double getWindSpeed()
    {
        return dblWindSpeed;
    }
    
    public void setWindSpeed( double dblWindSpeedIn )
    {
        dblWindSpeed = dblWindSpeedIn;
    }
    
    public String temperatureUnits()
    {
        if(blnUnits == false)
            return "°F";
        else 
            return "°C";
    }
    
    public String speedUnits()
    {
        if(blnUnits == false)
            return "MPH";
        else 
            return "Kph";
    }
    
    public double windChill()
    {
        double dblWindChillTemperature;
        double dblTemperatureTemp;
        double dblWindSpeedTemp;
        
        if(blnUnits == false)
        {
            dblWindChillTemperature = 35.74 + 0.6215 * dblTemperature - 35.75 * Math.pow(dblWindSpeed,  0.16) + 0.4275 * dblTemperature * Math.pow(dblWindSpeed,0.16);
            return dblWindChillTemperature;
        }


        else
        {
            dblTemperatureTemp =  celsiusToFahrenheit(dblTemperature);
            dblWindSpeedTemp = dblWindSpeed / 1.61; 
            dblWindChillTemperature = 35.74 + 0.6215 * dblTemperatureTemp - 35.75 * Math.pow(dblWindSpeedTemp , 0.16) + 0.4275 * dblTemperatureTemp * Math.pow(dblWindSpeedTemp,0.16);
            dblWindChillTemperature = fahrenheitToCelsius(dblWindChillTemperature);
            return dblWindChillTemperature;
        }
    }    
    public double heatIndex()
    {
        double dblHeatIndex;
        double dblTemperatureTemp;
        if(blnUnits == false)
        {
            dblHeatIndex = -42.379 + 2.04901523 * dblTemperature + 10.14333127 * dblRelativeHumidity - 0.22475541 * dblTemperature * dblRelativeHumidity
                            - 0.00683783 * dblTemperature * dblTemperature - 0.05481717 * dblRelativeHumidity * dblRelativeHumidity + 0.00122874 * dblTemperature
                            * dblTemperature * dblRelativeHumidity + 0.00085282 * dblTemperature * dblRelativeHumidity * dblRelativeHumidity - 0.00000199 
                            * dblTemperature * dblTemperature * dblRelativeHumidity * dblRelativeHumidity;
            return dblHeatIndex;                
        }
        else
        {
            dblTemperatureTemp = celsiusToFahrenheit(dblTemperature);
            
            dblHeatIndex = -42.379 + 2.04901523 * dblTemperatureTemp + 10.14333127 * dblRelativeHumidity - 0.22475541 * dblTemperatureTemp * dblRelativeHumidity
            - 0.00683783 * dblTemperatureTemp * dblTemperatureTemp - 0.05481717 * dblRelativeHumidity * dblRelativeHumidity + 0.00122874 * dblTemperatureTemp
            * dblTemperatureTemp * dblRelativeHumidity + 0.00085282 * dblTemperatureTemp * dblRelativeHumidity * dblRelativeHumidity - 0.00000199 
            * dblTemperatureTemp * dblTemperatureTemp * dblRelativeHumidity * dblRelativeHumidity;
            
            dblHeatIndex = fahrenheitToCelsius(dblHeatIndex);
            
            return dblHeatIndex;            
        }
    }
    
    public String toString()
    {
        return "Wind Chill: " + windChill() + " " + temperatureUnits() + "   Heat Index: " + heatIndex() + " " + temperatureUnits();

    }        

}

package application;

import gui.WeatherPanel;

import java.awt.Dimension;

import javax.swing.*;


public     class        WeatherCalc
                extends    JApplet
{
        private static final long    serialVersionUID    = 1L;

    private static final String    STR_FRAME_TITLE = "DPR205 - Weather Calculator";

    //    Class  Variables:
    //    -----------------
    private    static                WeatherPanel spnlWeather;
    
    public static void main( String[] args )
  {
    //    Local Variables:
    //    ----------------
    JFrame jfWeather;

    WeatherPanel spnlWeather;

    //    Program Logic:
    //    --------------
    jfWeather = new JFrame( STR_FRAME_TITLE );
    jfWeather.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    spnlWeather = new WeatherPanel();

    jfWeather.getContentPane().add( spnlWeather );
    jfWeather.pack();
    jfWeather.setVisible( true );

    return;

  } // public static void main (String[] )

    public    void    init()
    {
    WeatherCalc.spnlWeather = new WeatherPanel();
    this.resize( new Dimension(    WeatherPanel.INT_PANEL_WIDTH,
                                                            WeatherPanel.INT_PANEL_HEIGHT
                                                        )
                         );
    return;
    }    //    public    void    init()

    public    void    start()
    {
    this.add( WeatherCalc.spnlWeather );

    return;
    }    //    public    void    start()

} // public class WeatherCalc

Edited by YingKang: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.