Develop a Java program to calculate the BMI of ‘n’ number of peopleusing constructors, methods and provide its sample output.
Question
Develop a Java program to calculate the BMI of ‘n’ number of people using constructors, methods and provide its sample output.
Solution
Sure, here is a simple Java program that calculates the BMI of 'n' number of people using constructors and methods:
import java.util.Scanner;
class Person {
double weight;
double height;
// Constructor
Person(double weight, double height) {
this.weight = weight;
this.height = height;
}
// Method to calculate BMI
double calculateBMI() {
return weight / (height * height);
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of people: ");
int n = scanner.nextInt();
Person[] people = new Person[n];
for (int i = 0; i < n; i++) {
System.out.println("Enter weight for person " + (i+1) + ": ");
double weight = scanner.nextDouble();
System.out.println("Enter height for person " + (i+1) + ": ");
double height = scanner.nextDouble();
people[i] = new Person(weight, height);
}
for (int i = 0; i < n; i++) {
System.out.println("BMI of person " + (i+1) + " is: " + people[i].calculateBMI());
}
}
}
Sample Output:
Enter the number of people:
2
Enter weight for person 1:
70
Enter height for person 1:
1.75
Enter weight for person 2:
60
Enter height for person 2:
1.65
BMI of person 1 is: 22.857142857142858
BMI of person 2 is: 22.03856749311295
This program first asks for the number of people, then for each person it asks for their weight and height. It then calculates and prints the BMI for each person.
Similar Questions
A gym has yoga classes. Each class has 13 students. If there are c classes, write an equation to represent the total number of students s taking yoga.
Write short note on following.1.Ideal body weight (IBW)2.Body mass index (BMI)
How do we classify an individual as obese?*A) BMI is 18B) BMI 24C) BMI above 29D) BMI 15
What is the body mass index of client with height of 5'2" and weight of 100 kilograms and classify the body mass index?
Describe how you would design a fitness program to create muscular endurance in terms of the amount of weight and number of repetitions
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.