//---------- Program BMIApplet (Step 4 Final) -------------------//

import java.awt.*;
import java.awt.event.*;
import java.applet.*;


/**
 * Introduction to OOP with Java 2nd Edition, McGraw-Hill
 *   
 *  Chapter 5
 *
 *  Program BMIApplet
 *
 * <p>
 * This applet computes the body mass index (BMI).
 * It accepts the user’s height and weight and displays
 * his/her BMI according to the formula
 *         
 *        BMI = weight / (height * height)
 *
 */
public class BMIApplet extends Applet implements ActionListener
{

//----------------------------------
//    Data Members
//----------------------------------

    /**
     * The label for heightInput TextField
     */
    private Label      heightLabel;
    
    /**
     * The label for weightInput TextField
     */
    private Label      weightLabel;
    
    /**
     * The label for displaying BMI
     */
    private Label      BMILabel; 

    /**
     * The TextField to accept the user's height
     */
    private TextField  heightInput;
    
    /**
     * The TextField to accept the user's weight
     */
    private TextField  weightInput; 

    /**
     * The Button the user clicks to compute the BMI
     */
    private Button     computeButton;


//----------------------------------
//    Constructors
//----------------------------------
                                
    /**
     * Default constructor for creating and placing GUI objects
     * on this applet.
     */
    public BMIApplet()
    {
        //create objects
        heightLabel    = new Label("Your height (in meters, e.g. 1.88):");
        heightInput    = new TextField( 15 );
  
        weightLabel    = new Label("Your weight (in kilograms, e.g. 80.5):");
        weightInput    = new TextField( 15 );
  
        computeButton  = new Button("           Compute BMI             ");
  
        BMILabel       = new Label("This is your BMI Computer.");
  
        //Place the GUI objects to the applet.
        //The order of placement is significant.
        add(heightLabel);
        add(heightInput);
  
        add(weightLabel);
        add(weightInput);
  
        add(computeButton);
  
        add(BMILabel);
  
        computeButton.addActionListener(this);
    }
    

//-----------------------------------------------------
//     Public Methods:
// 
//         void   actionPerformed   (  ActionEvent   )
//
//-----------------------------------------------------
    /**
     * Implements the abstract method defined in the interface 
     * ActionListener. 
     *
     * @param event the ActionEvent object
     *
     */
    public void actionPerformed(ActionEvent event)
    {
        String  heightString, weightString, result;
        double  height, weight;
        int     BMI;
  
        // Get input values
        heightString = heightInput.getText();
        weightString = weightInput.getText();
  
        //Convert input to numbers
        height = convertToDouble(heightString);
        weight = convertToDouble(weightString);
  
        //Compute the BMI
        BMI = computeBMI(height, weight);
  
        //Display the result
        result = "Your BMI is " + BMI;
        BMILabel.setText(result);
        
        add( BMILabel );
        doLayout( );
  
    }
 
//-------------------------------------------------
//      Private Methods:
// 
//          double  computeBMI       (  double   )
//          double  convertToDouble  (  String   )
//
//------------------------------------------------

    /**
     * Computes the BMI from a given height and weight.
     *
     * @param height the height of a person
     * @param weight the weight of a person
     *
     * @return the BMI value for a given height and weight
     *
     */
    private int computeBMI( double height, double weight)
    {
        int BMI;
        
        BMI = (int) Math.round( weight / (height*height) );
        
        return BMI; 
    }
    
    
    /**
     * Converts the argument String to a double.
     *
     * @param str String to convert to double
     *
     * @return the String converted to a double value
     *
     */
    private double convertToDouble( String str )
    {
        Double doubleObj = new Double( str );
        return doubleObj.doubleValue( );
        
        //Note: You can do the same by replacing the above 
        //      two statements with the following statement:
        //
        //          return Double.parseDouble( str );
    }


}