What is a java BMI calculator?

526    Asked by TamuraTakada in Java , Asked on Oct 18, 2022

 My task:


Body Mass Index (BMI) is a measure of health based on height and weight. It can be calculated by taking your weight in kilograms and dividing it by the square of your height in metres.

Write a java code to let the user enter weight, feet, and inches and interpret the users BMI.

My code:

//import scanner
import java.util.Scanner;
//import Maths class
import java.lang.Math;
public class ComputeBMI {
   public static void main(String[] args) {
        // TODO Auto-generated method stub
  //create scanner
        Scanner input = new Scanner(System.in);
//declare variables
        double weight;
        int feet;
        int inches;
        //prompt user
        System.out.print("Enter weight in pounds: ");
        weight = input.nextFloat();
        System.out.print("Enter feet: ");
        feet = input.nextInt();
        System.out.print("Enter inches: ");
        inches = input.nextInt();
        //convert measurements
        double weightInKilos = weight * 0.453592;
        double heightInMeters = (((feet * 12) + inches) * .0254);
        double bmi = weightInKilos / Math.pow(heightInMeters, 2.0);
//      double bmi = weightInKilos / (heightInMeters * heightInMeters);
        //display output
        System.out.println("Your BMI is: " + bmi);
        //interpret BMI
        if (bmi < 18>            System.out.print("Underweight");
        }
        else if (bmi >= 18.5 && bmi < 25>            System.out.print("Normal");
        }
        else if (bmi >= 25 && bmi < 30>            System.out.print("Overweight");
        }
        else if (bmi >= 30) {
            System.out.print("Obese");
        }
//      Do I need this last else if there?
//      else {
//          System.out.print("");
//      }
        input.close();
    }
}

I used only the material we've been taught thus far to complete my code. My POC is clarity/fluidity of my code and my variable data types. In my mind, feet and inches should be integers and weight should be a double. Valid hypothesis?


Answered by Test Ankesh

I'll go through your code from top to bottom for a better understanding of the java BMI calculator:


//import scanner

import java.util.Scanner;

The comment "import scanner" is not necessary, since it only repeats what the code below already does.

//import Maths class

import java.lang.Math;

This import can be removed completely, since all classes from java.lang are imported implicitly.

public class ComputeBMI {

    public static void main(String[] args) {

        // TODO Auto-generated method stub

This "TODO" comment is something you have to remove. It makes your code look sloppy, as if you didn't proofread it yourself before showing it to other people.

        //create scanner

        Scanner input = new Scanner(System.in);

As a beginner, you may need these kinds of comments, but as soon as you have written this type of code five times, you should get rid of these comments.

        //declare variables

        double weight;

        int feet;

        int inches;

In Java, it is good practice to declare variables as late as possible, that is when they are first used.

        //prompt user

        System.out.print("Enter weight in pounds: ");

        weight = input.nextFloat();

This line should declare the weight variable: double weight = input.nextDouble();. I changed the method call to nextDouble so that it matches the type of the height variable.

        System.out.print("Enter feet: ");

        feet = input.nextInt();
        System.out.print("Enter inches: ");
        inches = input.nextInt();
        //convert measurements
        double weightInKilos = weight * 0.453592;
        double heightInMeters = (((feet * 12) + inches) * .0254);
        double bmi = weightInKilos / Math.pow(heightInMeters, 2.0);
// double bmi = weightInKilos / (heightInMeters * heightInMeters);

You chose excellent names for your variables. When dealing with different measurement units, each variable should mention the unit, which you did here.

        //display output

        System.out.println("Your BMI is: " + bmi);
        //interpret BMI
        if (bmi < 18>= 18.5 && bmi < 25>
else if (bmi >= 25 && bmi < 30>= 30) {
            System.out.print("Obese");
        }
// Do I need this last else if there?
// else {
// System.out.print("");
// }

No, you don't need the last else if. You can just write } else { System.out.println("Obese"); }.

Note the println instead of print. When printing something, you usually print it in lines. Each line should be terminated properly, which you do with println. The ln in println stands for line break.

        input.close();

    }

}

Altogether, a very nice piece of work. The many comments tell the reader that you are a beginner, but don't worry, in a few weeks you can remove them and still understand the program equally well.



Your Answer