Explain the working of java BMI calculator.

351    Asked by AnilJha in Java , Asked on Oct 6, 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 meters.


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 Math 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 Anil Mer

You run all the computations in the main method. It's not a good practice. One method should do one focused thing. That's why I'd recommend to create a separate method for "step" of your computation:


converting measurements (something like double poundsToKilograms(double weightInPounds)

computing the BMI given the height and the weight of the user

converting a numeric value of the BMI to a human-readable message

This way, you main method would just read the input, call these methods and print the result. It would make your code more readable and testable (you would be able to test your methods separately)

The comments should explain what the code does and why it does what it does. They shouldn't describe how it works. That is, it's a good idea to create a doc comment for each method saying what it does, how it behaves if it gets an incorrect input and so on. Conversely, comments like //declare variables or //create scanner actually harm the readability. They just create noise. They don't add anything useful to the code itself. Writing self-documenting code is a good practice (that is, ideally it should be clear what your code does from the code itself).

ComputeBMI doesn't sound like a good class name to me. I'd rather call it a Java BMI Calculator (it's conventional to name classes with nouns and methods with verbs).

The message Enter feet: looks kind of strange. I think it should say that it requests the user's height (it's not clear from the message itself).

It's fine to keep the height as an int, but I would show a message to the user saying that. Otherwise, they may get an unexpected error.

You could also add some kind of input validation and error handling so that your program doesn't fail with an exception (it might be confusing for the user) but rather prints a more suitable message and possibly prompts the user again.



Your Answer

Interviews

Parent Categories