/*
   Chapter 3:  Chapter Walk Through part2 (The Body Mass Index       Calculator)
  Programmer: Brad Shedd
   Date:       April 19, 2006
   Filename:   BodyMassApplet.java
   Purpose: This project calculates the body mass index based
            on a person's height and weight.
*/

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class BodyMassApplet extends Applet implements ActionListener
{
  
// delare variables
   Image logo;    // declare an Image object
   int inches, pounds;
  
double meters, kilograms, index;

 
//constuct components
   Label companyLabel = new Label("THE SUN FITNESS CENTER BODY MASS INDEX CALCULATOR");
   Label heightLabel =
new Label("Enter your height to the nearest inch: ");
      TextField heightField =
new TextField(10);
   Label weightLabel =
new Label("Enter your weight to the nearest pound: ");
      TextField weightField =
new TextField(10);
   Button calcButton =
new Button("Calculate");
      getRootPane().setDefaultButton(calcButton);
   Label outputLabel =
new Label("Click the Calculate button to see your body mass index.");

     
public void init()
      {
         setForeground(Color.red);
         add(companyLabel);
         add(heightLabel);
         add(heightField);
         add(weightLabel);
         add(weightField);
         add(calcButton);
         calcButton.addActionListener(
this);
         add(outputLabel);
         logo = getImage(getDocumentBase(),
"logo.gif");
      }
// end of init method

      public void actionPerformed(ActionEvent e)
      {

         inches = Integer.parseInt(heightField.getText());
         pounds = Integer.parseInt(weightField.getText());
         meters = inches / 39.36;
         kilograms = pounds / 2.2;
         index = kilograms / Math.pow(meters,2);
         outputLabel.setText(
"YOUR BODY MASS INDEX IS " + Math.round(index) + ".");
     }

     
public void paint(Graphics g)
      {
         g.drawImage(logo,125,160,
this);
      }
}
// end of BodyMassApplet class

Homepage