Java

Program to Calculate Body Mass Index (BMI) in GUI Java4 min read

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

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:

Program to Calculate Body Mass Index (BMI) in GUI Java
Program to Calculate Body Mass Index (BMI) in GUI Java

Leave a Comment