/* Chapter 3 The Body Mass Index Calc Jim Miller September 19.2002 Filename: BodyMassApplet.java Function: This class calculates a person's body mass based on his/her height and weight. */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class BodyMassApplet extends Applet implements ActionListener { // declare variables String height, weight; int inches, pounds; double kilograms, meters, index; Image logo; // construct 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"); Label outputLabel = new Label("Click the Calculate button to see your body mass index."); 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) + "."); } // End of method actionPerformed 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 paint(Graphics g) { g.drawImage(logo,125,160,this); } // End of paint method } // End class BodyMassApplet