Introduction
Body Mass Index (BMI) is a simple measurement used to assess whether a person has a healthy weight for a given height. It is commonly used by healthcare professionals to determine if an individual is underweight, normal weight, overweight, or obese.
BMI is calculated using a person’s weight in kilograms and height in meters.
Objective
The objective of this program is to calculate the Body Mass Index (BMI) of a person based on their weight and height. The program will ask the user for their weight (in kilograms) and height (in meters) and then compute the BMI, along with a classification of the result according to standard BMI categories.
Java Program Code
import java.util.Scanner; public class BMICalculator { public static void main(String[] args) { // Create a Scanner object for user input Scanner scanner = new Scanner(System.in); // Input: Ask the user for their weight (in kg) System.out.print("Enter your weight in kilograms: "); double weight = scanner.nextDouble(); // Input: Ask the user for their height (in meters) System.out.print("Enter your height in meters: "); double height = scanner.nextDouble(); // Calculate BMI: BMI = weight (kg) / height (m)^2 double bmi = weight / (height * height); // Output: Display the BMI System.out.printf("Your BMI is: %.2f\n", bmi); // Determine BMI category based on standard BMI ranges if (bmi < 18.5) { System.out.println("You are underweight."); } else if (bmi >= 18.5 && bmi < 24.9) { System.out.println("You have a normal weight."); } else if (bmi >= 25 && bmi < 29.9) { System.out.println("You are overweight."); } else { System.out.println("You are obese."); } // Close the scanner to prevent resource leak scanner.close(); } }
Program Explanation
This Java program calculates the Body Mass Index (BMI) based on a person’s weight and height. Here’s how the program works:
- Scanner Class: We use the
Scanner
class to capture input from the user. The user is prompted to enter their weight in kilograms and height in meters. - BMI Calculation: The formula for calculating BMI is
BMI = weight / (height * height)
. The program computes this formula using the values entered by the user. - Output: After calculating the BMI, the program displays the result rounded to two decimal places. It then classifies the result into one of the following categories:
- Underweight: BMI less than 18.5
- Normal weight: BMI between 18.5 and 24.9
- Overweight: BMI between 25 and 29.9
- Obese: BMI 30 or higher
- Closing the Scanner: The scanner object is closed to prevent memory leaks and free up system resources.
How to Run the Program
Follow these steps to run the program:
- Ensure you have Java installed on your system. You can download Java from here.
- Save the Java code provided above in a file named
BMICalculator.java
. - Open your terminal or command prompt and navigate to the directory where the file is saved.
- Compile the Java program by running the following command:
javac BMICalculator.java
- Run the compiled Java program using the following command:
java BMICalculator
- The program will prompt you to enter your weight and height. It will then calculate and display your BMI along with the corresponding classification.
Explanation of the Code and How It Works:
- Program Flow:
- The program starts by importing the
Scanner
class, which is used to read user input. - It prompts the user to enter their weight in kilograms and height in meters.
- It calculates the BMI using the formula
BMI = weight / (height * height)
and then prints the result. - It classifies the BMI based on predefined categories and prints the appropriate message.
- The program starts by importing the
- Program Structure:
import java.util.Scanner;
: Imports the Scanner class for reading user input.public class BMICalculator
: Defines the class that contains the main method.Scanner scanner = new Scanner(System.in);
: Creates a new Scanner object for capturing user input.double weight = scanner.nextDouble();
: Reads the user’s weight.double height = scanner.nextDouble();
: Reads the user’s height.- BMI calculation: The formula
bmi = weight / (height * height)
is applied to compute the BMI. - Conditional Statements: The
if
andelse if
blocks are used to classify the BMI result into different categories (underweight, normal weight, overweight, obese). scanner.close();
: Closes the Scanner to avoid any resource leaks.
- Running the Program:
- To run this Java program, you must first compile it using
javac BMICalculator.java
and then execute it withjava BMICalculator
. - The program will prompt you to enter your weight and height, then calculate and display your BMI and classification.
- To run this Java program, you must first compile it using
This approach ensures that the program works efficiently and provides a simple, interactive way for users to calculate their BMI.