Java program to calculate Body Mass Index (BMI)
The Body Mass Index (BMI) is a quick way to assess your body size simply with your weight and height, regardless of your gender. Quickly calculate your BMI and find out which category you fall into.
The Body Mass Index (BMI) is the only index validated by the World Health Organization to assess an individual’s build and therefore health risks. The BMI makes it possible to determine whether one is the situation of thinness, overweight or obesity for example.
In this program we will calculate BMI. Body mass index (BMI) is a measure of body fat based on height and weight that applies to adult men and women.
Write a program that calculates and displays a person’s body mass index java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; /** * * @author H2O */ public class JavaExamples extends javax.swing.JFrame { JavaExamples() { JFrame f = new JFrame();//creating instance of JFrame JLabel lb1 = new JLabel("Height(Inches):"); lb1.setBounds(20, 20, 100, 40);//x axis, y axis, width, height f.add(lb1); //adding component in JFrame JLabel lb2 = new JLabel("Weight(Pounds):"); lb2.setBounds(20, 60, 100, 40);//x axis, y axis, width, height f.add(lb2); //adding component in JFrame JLabel lbResult = new JLabel("BMI Calculator in JAVA GUI"); lbResult.setBounds(20, 90, 300, 40);//x axis, y axis, width, height f.add(lbResult); //adding component in JFrame JTextField txtHeight = new JTextField(""); txtHeight.setBounds(120, 20, 200, 40);//x axis, y axis, width, height f.add(txtHeight); //adding component in JFrame JTextField txtWeight = new JTextField(""); txtWeight.setBounds(120, 60, 200, 40);//x axis, y axis, width, height f.add(txtWeight); //adding component in JFrame JButton btn = new JButton("BMI Calculator in JAVA GUI(CALCULATE)");//creating instance of JButton btn.setBounds(20, 130, 300, 40);//x axis, y axis, width, height //Event btn.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { double weight=Double.parseDouble(txtWeight.getText()); double height=Double.parseDouble(txtHeight.getText()); double bmi = weight / Math.pow(height, 2) * 703; if (bmi < 18.5) { lbResult.setText("underweight - BMI : "+bmi); } else if (bmi < 25) { lbResult.setText("normal - BMI : "+bmi); } else if (bmi < 30) { lbResult.setText("overweight - BMI : "+bmi); } else { lbResult.setText("obese - BMI : "+bmi); } } }); f.add(btn);//adding button in JFrame f.setSize(400, 300); f.setLayout(null); f.setVisible(true); } public static void main(String[] args) { new JavaExamples(); } } |
This code creates a simple Body Mass Index (BMI) calculator using Java Swing. The calculator has two input fields for the user to enter their height and weight in inches and pounds, respectively, and a button labeled “BMI Calculator in JAVA GUI(CALCULATE)” that the user can click to calculate their BMI.
Here’s a step by step breakdown of what the code is doing:
The class JavaExamples
extends the javax.swing.JFrame
class to create a new frame for the calculator.
In the constructor of the class, an instance of JFrame
is created and stored in the variable f
.
The constructor creates three JLabel
instances, one for each label (“Height(Inches):”,”Weight(Pounds):” , “BMI Calculator in JAVA GUI”) to be displayed on the frame. Each label’s position and size are set using the setBounds()
method and added to the frame using the add()
method.
Two JTextField
instances are created and stored in the variables txtHeight
and txtWeight
. Each text field’s position and size are set using the setBounds()
method and added to the frame using the add()
method.
A JButton
instance is created and stored in the variable btn
with the label “BMI Calculator in JAVA GUI(CALCULATE)” on it. The button’s position and size are set using the setBounds()
method.
An ActionListener
is added to the button using the addActionListener()
method. The actionPerformed
method of the listener is overridden to perform the calculation of the BMI.
Inside the actionPerformed
method, the text of the text fields are obtained using the getText()
method, and they are parsed to double values weight
and height
.
The Body Mass Index (BMI) is calculated using the formula weight / Math.pow(height, 2) * 703
Once the BMI value is calculated, the code checks the value to determine whether the user is underweight, normal, overweight, or obese. Depending on the value of the BMI, the text of the lbResult
label is updated accordingly with an appropriate message.
Finally, the frame is configured with a size of 400×300 pixels, set to have no layout manager (using setLayout(null)
) and set to be visible.
The main method calls the constructor of the class, which runs the code that sets up the calculator.
It’s a simple implementation for calculating the BMI and showing the result in a label, but you can make some enhancement like adding more user-friendly interface, also you can store the user data and have history.
Output: