I am having problems with my java homework in class. Write a program that will i
ID: 639460 • Letter: I
Question
I am having problems with my java homework in class.
Write a program that will interactively read the name, age, weight in pounds, and height in inches for five people and output the BMI for each one. At the end of the program, it should output the average age, average height and average weight of the five participants.
IT HAS TO INCLUDE:
Declare and use a program constant of data type int named CONVERSION_FACTOR to represent the value 703.
Use the int data type for variables age, weight and height.
Print the calculated BMI and all averages using two decimal places.
Use the name BMIcalculator followed by your three initials for the class name.
BUT IT CANNOT INCLUDE LOOPS WHICH IS SAD :(
I have no idea where to start. This is the first time I have tried working with java and my teacher just plopped me into this.
Explanation / Answer
import java.util.Scanner;
public class BMIcalculator {
public static final int CONVERSION_FACTOR = 703;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String name[] = new String[5];
int age[] = new int[5];
int height[] = new int[5];
int weight[] = new int[5];
System.out.println("Enter details of person 1 : ");
System.out.print("Name : ");
name[0] = in.nextLine();
System.out.print("Age : ");
age[0] = Integer.parseInt(in.nextLine());
System.out.print("Height in inches : ");
height[0] = Integer.parseInt(in.nextLine());
System.out.print("Weight in pounds : ");
weight[0] = Integer.parseInt(in.nextLine());
System.out.println("Enter details of person 2 : ");
System.out.print("Name : ");
name[1] = in.nextLine();
System.out.print("Age : ");
age[1] = Integer.parseInt(in.nextLine());
System.out.print("Height in inches : ");
height[1] = Integer.parseInt(in.nextLine());
System.out.print("Weight in pounds : ");
weight[1] = Integer.parseInt(in.nextLine());
System.out.println("Enter details of person 3 : ");
System.out.print("Name : ");
name[2] = in.nextLine();
System.out.print("Age : ");
age[2] = Integer.parseInt(in.nextLine());
System.out.print("Height in inches : ");
height[2] = Integer.parseInt(in.nextLine());
System.out.print("Weight in pounds : ");
weight[2] = Integer.parseInt(in.nextLine());
System.out.println("Enter details of person 4 : ");
System.out.print("Name : ");
name[3] = in.nextLine();
System.out.print("Age : ");
age[3] = Integer.parseInt(in.nextLine());
System.out.print("Height in inches : ");
height[3] = Integer.parseInt(in.nextLine());
System.out.print("Weight in pounds : ");
weight[3] = Integer.parseInt(in.nextLine());
System.out.println("Enter details of person 5 : ");
System.out.print("Name : ");
name[4] = in.nextLine();
System.out.print("Age : ");
age[4] = Integer.parseInt(in.nextLine());
System.out.print("Height in inches : ");
height[4] = Integer.parseInt(in.nextLine());
System.out.print("Weight in pounds : ");
weight[4] = Integer.parseInt(in.nextLine());
double bmi[] = new double[5];
bmi[0] = (weight[0] * CONVERSION_FACTOR) / (height[0] * height[0]);
bmi[1] = (weight[1] * CONVERSION_FACTOR) / (height[1] * height[1]);
bmi[2] = (weight[2] * CONVERSION_FACTOR) / (height[2] * height[2]);
bmi[3] = (weight[3] * CONVERSION_FACTOR) / (height[3] * height[3]);
bmi[4] = (weight[4] * CONVERSION_FACTOR) / (height[4] * height[4]);
double averageAge = (age[0] + age[1] + age[2] + age[3] + age[4]) / 5;
double averageHeight = (height[0] + height[1] + height[2] + height[3] + height[4]) / 5;
double averageWeight = (weight[0] + weight[1] + weight[2] + weight[3] + weight[4]) / 5;
System.out.println();
System.out.println("BMI of person 1 : " + bmi[0]);
System.out.println("BMI of person 2 : " + bmi[1]);
System.out.println("BMI of person 3 : " + bmi[2]);
System.out.println("BMI of person 4 : " + bmi[3]);
System.out.println("BMI of person 5 : " + bmi[4]);
System.out.println("Average age is : " + (double)((int)(averageAge * 100) / 100));
System.out.println("Average height is : " + (double)((int)(averageHeight * 100) / 100));
System.out.println("Average weight is : " + (double)((int)(averageWeight * 100) / 100));
in.close();
}
}
Sample run :
Enter details of person 1 :
Name : gopi
Age : 24
Height in inches : 67
Weight in pounds : 158
Enter details of person 2 :
Name : nagendra
Age : 25
Height in inches : 65
Weight in pounds : 120
Enter details of person 3 :
Name : paul
Age : 35
Height in inches : 66
Weight in pounds : 150
Enter details of person 4 :
Name : ravi
Age : 24
Height in inches : 66
Weight in pounds : 125
Enter details of person 5 :
Name : jack
Age : 24
Height in inches : 69
Weight in pounds : 150
BMI of person 1 : 24.0
BMI of person 2 : 19.0
BMI of person 3 : 24.0
BMI of person 4 : 20.0
BMI of person 5 : 22.0
Average age is : 26.0
Average height is : 66.0
Average weight is : 140.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.