Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Body Mass Index (BMI) is measure of weight that takes height into account. Gener

ID: 3675506 • Letter: B

Question

Body Mass Index (BMI) is measure of weight that takes height into account. Generally, a BMI above 25 is considered high, that is, likely to indicate that an individual is overweight. BMI is calculated as follows for both men and women:

(703 * weight in pounds) / (height in inches)^2

Files BMI.java and BMIPanel.java contain skeletons for a program that uses a GUI to let the user compute their BMI. This is similar to the Fahrenheit program in listings 4.12 and 4.13 of the text. Fill in the code as indicated by the comments and compile and run this program; you should see the BMI calculator displayed.

//********************************************************************

// BMI.java  

//

// Sets up a GUI to calculate body mass index.

//********************************************************************

import javax.swing.JFrame;

public class BMI

{

   //-----------------------------------------------------------------

   // Creates and displays the BMI GUI.

   //-----------------------------------------------------------------

   public static void main (String[] args)

   {

       JFrame frame = new JFrame("BMI");

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       BMIPanel panel = new BMIPanel();

       frame.getContentPane().add(panel);

       frame.pack();

       frame.setVisible(true);

   }

}

//********************************************************************

Explanation / Answer

//********************************************************************
// BMI.java   
//
// Sets up a GUI to calculate body mass index.
//********************************************************************

import javax.swing.JFrame;
public class MyClass
{
//-----------------------------------------------------------------
// Creates and displays the BMI GUI.
//-----------------------------------------------------------------
public static void main (String[] args)
{

JFrame frame = new JFrame("BMI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

BMIPanel panel = new BMIPanel();

frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);

}
}

//********************************************************************
// BMIPanel.java
//
// Computes body mass index in a GUI.
//********************************************************************

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

import javax.swing.*;

public class BMIPanel extends JPanel
{
private int width = 300;
private int height = 120;

private JLabel heightLabel, weightLabel, BMILabel, resultLabel;
private JTextField height, weight;
private JButton calculate;

//-----------------------------------------------------------------
// Sets up the GUI.
//-----------------------------------------------------------------
public BMIPanel()
{

//create labels for the height and weight textfields
heightLabel = new JLabel ("Your height in inches: ");
weightLabel = new JLabel ("Your weight in pounds: ");

//create a "this is your BMI" label
BMILabel = new JLabel ("This is your BMI");
//create a result label to hold the BMI value
resultLabel = new JLabel ("BMI");

//create a JTextField to hold the person's height in inches
height = new JTextField (7);
//create a JTextField to hold the person's weight in pounds
weight = new JTextField (7);

//create a button to press to calculate BMI
calculate = new JButton("Calculate BMI");
//create a BMIListener and make it listen for the button to be pressed
calculate.addActionListener(new BMIButtonListener());

//add the height label and height textfield to the panel
add(heightLabel);
add(height);
//add the weight label and weight textfield to the panel
add(weightLabel);
add(weight);
//add the button to the panel
add(calculate);
//add the BMI label to the panel
add(BMILabel);
//add the label that holds the result to the panel
add(resultLabel);

//set the size of the panel to the WIDTH and HEIGHT constants
setPreferredSize(new Dimension(WIDTH, HEIGHT));
//set the color of the panel to whatever you choose
setBackground(Color.orange);
}

//*****************************************************************
// Represents an action listener for the calculate button.
//*****************************************************************
private class BMIButtonListener implements ActionListener
{
//--------------------------------------------------------------
// Compute the BMI when the button is pressed
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
String heightText, weightText;
int heightVal, weightVal;
double bmi;

//get the text from the height and weight textfields
heightText = height.getText();
weightText = weight.getText();

//Use Integer.parseInt to convert the text to integer values
heightVal = Integer.parseInt(heightText);
weightVal = Integer.parseInt(weightText);

//Calculate the bmi = 703 * weight in pounds / (height in inches)^2
bmi = 703 * weightVal / pow(heightVal,2);;

//Put result in result label. Use Double.toString to convert double to string.
String result = Double.toString(bmi);
resultLabel.setText(result);
}
}
}