Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Body mass index is a measure of whether someone\'s weight is appropriate for the

ID: 3816814 • Letter: B

Question

Body mass index is a measure of whether someone's weight is appropriate for their height. A body mass-index value between 19 and 25 is considered to be in the normal range. Here's the formula for calculating body mass index: 704 x weightIn Pounds bmi height In Inches Implement a program that prompts the user for height and weight values and displays the associated body mass index. Perform input validation by making sure that the user enters a positive integer number for feet, a nonnegative decimal number for inches, and a positive decimal number for weight. See the sample session for details. In particular, note the format for the echo-printed height and weight values and for the generated body-mass-index value

Explanation / Answer

import java.util.Scanner;

public class HelloWorld{

     public static void main(String []args){
       
        int feet,flag;
        float inches;
        float pounds;
        float bmi;
        float hinches;
      
        System.out.println("Welcome to the body mass calculator!");
        System.out.println("Enter height using feet space inches (e.g., 5 6.25):");
      
        do{
        
          feet=0;
          flag=0;
          inches=0.0f;
          Scanner in = new Scanner(System.in);
        
          if(in.hasNextInt()){
              feet=in.nextInt();
              if(feet == 0){
                 System.out.println("Invalid feet value. Must be positive.");
                 flag=1;
              }
          }else{
              System.out.println("Invalid feet value. Must be an integer.");
              flag=1;
          }
        
          if(in.hasNextFloat()){
              inches=in.nextFloat();
          }else{
              System.out.println("Invalid inches value. Must be a decimal number.");
              flag=1;
          }
        
          if(flag==1) System.out.println("Re-enter height using feet space inches (e.g., 5 6.25):");
        
        }while(flag==1);
      
        System.out.println("Enter weight in pounds: ");
      
        do{
        
          flag=0;
          pounds=0.0f;
          Scanner in = new Scanner(System.in);
        
          if(in.hasNextFloat()){
              pounds=in.nextFloat();
              if(pounds == 0){
                 System.out.println("Invalid pounds value. Must be positive.");
                 flag=1;
              }
          }else{
              System.out.println("Invalid pounds value. Must be a decimal number.");
              flag=1;
          }
        
          if(flag==1) System.out.println("Re-enter weight in pounds: ");
        
        }while(flag==1);
      
        hinches= ((feet*12) + inches);
        bmi= (704*pounds)/(hinches*hinches);
        System.out.println("height = "+feet+"'-"+inches+"''");
        System.out.println("weight = "+pounds);
        System.out.printf("body mass index = %.1f",bmi);

        System.out.printf(" Thank you, good bye!");
     }
}


/*

   Output :-

    

Welcome to the body mass calculator!                                                                                                                                     

Enter height using feet space inches (e.g., 5 6.25):                                                                                                                     

hi there                                                                                                                                                                 

Invalid feet value. Must be an integer.                                                                                                                                  

Invalid inches value. Must be a decimal number.                                                                                                                          

Re-enter height using feet space inches (e.g., 5 6.25):                                                                                                                  

0 9                                                                                                                                                                      

Invalid feet value. Must be positive.                                                                                                                                    

Re-enter height using feet space inches (e.g., 5 6.25):                                                                                                                  

5.25 0                                                                                                                                                                   

Invalid feet value. Must be an integer.                                                                                                                                  

Re-enter height using feet space inches (e.g., 5 6.25):                                                                                                                  

5 9.25                                                                                                                                                                   

Enter weight in pounds:                                                                                                                                                  

0                                                                                                                                                                        

Invalid pounds value. Must be positive.                                                                                                                                  

Re-enter weight in pounds:                                                                                                                                               

150.5                                                                                                                                                                    

height = 5'-9.25''                                                                                                                                                       

weight = 150.5                                                                                                                                                           

body mass index = 22.1

Thank you, good bye!

*/