How would I add the average of the BMI\'s along with the other averages in this
ID: 639709 • Letter: H
Question
How would I add the average of the BMI's along with the other averages in this program? I am missing that one but cannot figure how to get it in correctly.
//BMIcalculator_ABC.java
import java.util.Scanner;
public class BMIcalculator_ABC
{
public static void main(String[] args)
{
Scanner keyboard=new Scanner(System.in);
final int CONVERSION_FACTOR=703;
int sumAge=0;
int sumWt=0;
int sumHt=0;
double BMI;
//reading the name,age ,weight and height of the person
System.out.println("Name: ");
String name=keyboard.nextLine();
System.out.println("Age: ");
int age=keyboard.nextInt();
sumAge+=age;
System.out.println("Weight: ");
int pounds=keyboard.nextInt();
sumWt+=pounds;
System.out.println("Height:");
int height=keyboard.nextInt();
sumHt+=height;
//Read the end of line character
keyboard.nextLine();
//Calculate the bmi
BMI = (double)pounds* CONVERSION_FACTOR /((height *height ));
//print the name and bmi value
System.out.println("Name :"+name);
System.out.println("BMI :"+BMI);
//reading the name,age ,weight and height of the person
System.out.println("Name: ");
name=keyboard.nextLine();
System.out.println("Age: ");
age=keyboard.nextInt();
sumAge+=age;
System.out.println("Weight: ");
pounds=keyboard.nextInt();
sumWt+=pounds;
System.out.println("Height:");
height=keyboard.nextInt();
sumHt+=height;
//Read the end of line character
keyboard.nextLine();
BMI = (double)pounds* CONVERSION_FACTOR /((height *height ));
System.out.println("Name :"+name);
System.out.println("BMI :"+BMI);
//reading the name,age ,weight and height of the person
System.out.println("Name: ");
name=keyboard.nextLine();
System.out.println("Age: ");
age=keyboard.nextInt();
sumAge+=age;
System.out.println("Weight: ");
pounds=keyboard.nextInt();
sumWt+=pounds;
System.out.println("Height:");
height=keyboard.nextInt();
sumHt+=height;
//Read the end of line character
keyboard.nextLine();
BMI = (double)pounds* CONVERSION_FACTOR /((height *height ));
System.out.println("Name :"+name);
System.out.println("BMI :"+BMI);
//reading the name,age ,weight and height of the person
System.out.println("Name: ");
name=keyboard.nextLine();
System.out.println("Age: ");
age=keyboard.nextInt();
sumAge+=age;
System.out.println("Weight: ");
pounds=keyboard.nextInt();
sumWt+=pounds;
System.out.println("Height:");
height=keyboard.nextInt();
sumHt+=height;
//Read the end of line character
keyboard.nextLine();
BMI = (double)pounds* CONVERSION_FACTOR /((height *height ));
System.out.println("Name :"+name);
System.out.println("BMI :"+BMI);
//reading the name,age ,weight and height of the person
System.out.println("Name: ");
name=keyboard.nextLine();
System.out.println("Age: ");
age=keyboard.nextInt();
sumAge+=age;
System.out.println("Weight: ");
pounds=keyboard.nextInt();
sumWt+=pounds;
System.out.println("Height:");
height=keyboard.nextInt();
sumHt+=height;
//Read the end of line character
keyboard.nextLine();
BMI = (double)pounds* CONVERSION_FACTOR /((height *height ));
System.out.println("Name :"+name);
System.out.println("BMI :"+BMI);
//Display the average of age , weight and height for two decimal places
System.out.printf("Average of Age %.2f : ",sumAge/5.0);
System.out.printf("Average of Weight %.2f ",sumWt/5.0);
System.out.printf("Average of Height %.2f ",sumHt/5.0);
}
}
Explanation / Answer
/*
* The java program that reads the five peoples
* name,age,weight,height and display the BMI
* of the each person. After reading the five
* peoples data, then finds the average values of
* ages,weight ,height and BMI values and display upto
* two decimal places.
* */
import java.util.Scanner;
public class BMIcalculator_ABC
{
public static void main(String[] args)
{
Scanner keyboard=new Scanner(System.in);
final int CONVERSION_FACTOR=703;
int sumAge=0;
int sumWt=0;
int sumHt=0;
//add a double variable sumBMI
int sumBMI=0;
double BMI;
//reading the name,age ,weight and height of the person
System.out.println("Name: ");
String name=keyboard.nextLine();
System.out.println("Age: ");
int age=keyboard.nextInt();
sumAge+=age;
System.out.println("Weight: ");
int pounds=keyboard.nextInt();
sumWt+=pounds;
System.out.println("Height:");
int height=keyboard.nextInt();
sumHt+=height;
//Read the end of line character
keyboard.nextLine();
//Calculate the bmi
BMI = (double)pounds* CONVERSION_FACTOR /((height *height ));
//Add the BMI to the sumBMI
sumBMI+=BMI;
//print the name and bmi value
System.out.println("Name :"+name);
System.out.println("BMI :"+BMI);
//reading the name,age ,weight and height of the person
System.out.println("Name: ");
name=keyboard.nextLine();
System.out.println("Age: ");
age=keyboard.nextInt();
sumAge+=age;
System.out.println("Weight: ");
pounds=keyboard.nextInt();
sumWt+=pounds;
System.out.println("Height:");
height=keyboard.nextInt();
sumHt+=height;
//Read the end of line character
keyboard.nextLine();
BMI = (double)pounds* CONVERSION_FACTOR /((height *height ));
//Add the BMI to the sumBMI
sumBMI+=BMI;
System.out.println("Name :"+name);
System.out.println("BMI :"+BMI);
//reading the name,age ,weight and height of the person
System.out.println("Name: ");
name=keyboard.nextLine();
System.out.println("Age: ");
age=keyboard.nextInt();
sumAge+=age;
System.out.println("Weight: ");
pounds=keyboard.nextInt();
sumWt+=pounds;
System.out.println("Height:");
height=keyboard.nextInt();
sumHt+=height;
//Read the end of line character
keyboard.nextLine();
BMI = (double)pounds* CONVERSION_FACTOR /((height *height ));
//Add the BMI to the sumBMI
sumBMI+=BMI;
System.out.println("Name :"+name);
System.out.println("BMI :"+BMI);
//reading the name,age ,weight and height of the person
System.out.println("Name: ");
name=keyboard.nextLine();
System.out.println("Age: ");
age=keyboard.nextInt();
sumAge+=age;
System.out.println("Weight: ");
pounds=keyboard.nextInt();
sumWt+=pounds;
System.out.println("Height:");
height=keyboard.nextInt();
sumHt+=height;
//Read the end of line character
keyboard.nextLine();
BMI = (double)pounds* CONVERSION_FACTOR /((height *height ));
//Add the BMI to the sumBMI
sumBMI+=BMI;
System.out.println("Name :"+name);
System.out.println("BMI :"+BMI);
//reading the name,age ,weight and height of the person
System.out.println("Name: ");
name=keyboard.nextLine();
System.out.println("Age: ");
age=keyboard.nextInt();
sumAge+=age;
System.out.println("Weight: ");
pounds=keyboard.nextInt();
sumWt+=pounds;
System.out.println("Height:");
height=keyboard.nextInt();
sumHt+=height;
//Read the end of line character
keyboard.nextLine();
BMI = (double)pounds* CONVERSION_FACTOR /((height *height ));
//Add the BMI to the sumBMI
sumBMI+=BMI;
System.out.println("Name :"+name);
System.out.println("BMI :"+BMI);
//Display the average of age , weight and height for two decimal places
System.out.printf("Average of Age %.2f : ",sumAge/5.0);
System.out.printf("Average of Weight %.2f ",sumWt/5.0);
System.out.printf("Average of Height %.2f ",sumHt/5.0);
//print the average of BMI by dividing sumBMI with 5.0 upto two decimal points
System.out.printf("Average of BMI %.2f ",sumBMI/5.0);
}
}
----------------------------------------------------------------------
Sample Output:
Name:
johnson
Age:
23
Weight:
50
Height:
110
Name :johnson
BMI :2.9049586776859506
Name:
Micheal
Age:
55
Weight:
120
Height:
110
Name :Micheal
BMI :6.971900826446281
Name:
Rhonda
Age:
45
Weight:
60
Height:
120
Name :Rhonda
BMI :2.9291666666666667
Name:
Bryne
Age:
56
Weight:
160
Height:
110
Name :Bryne
BMI :9.295867768595041
Name:
Samantha
Age:
28
Weight:
75
Height:
110
Name :Samantha
BMI :4.357438016528926
Average of Age 41.40 :
Average of Weight 93.00
Average of Height 112.00
Average of BMI 4.60
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.