Java
Java

 

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:

  1. 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.
  2. BMI Calculation: The formula for calculating BMI is BMI = weight / (height * height). The program computes this formula using the values entered by the user.
  3. 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
  4. 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:

  1. Ensure you have Java installed on your system. You can download Java from here.
  2. Save the Java code provided above in a file named BMICalculator.java.
  3. Open your terminal or command prompt and navigate to the directory where the file is saved.
  4. Compile the Java program by running the following command:
    javac BMICalculator.java
  5. Run the compiled Java program using the following command:
    java BMICalculator
  6. 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:

  1. 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.
  2. 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 and else 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.
  3. Running the Program:
    • To run this Java program, you must first compile it using javac BMICalculator.java and then execute it with java BMICalculator.
    • The program will prompt you to enter your weight and height, then calculate and display your BMI and classification.

This approach ensures that the program works efficiently and provides a simple, interactive way for users to calculate their BMI.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)