Homework 7 - \"How Healthy Are You?\" Problenm Write a program that calculates h
ID: 3738310 • Letter: H
Question
Homework 7 - "How Healthy Are You?" Problenm Write a program that calculates health information for people that computes both the Basal Metabolic Rate (BMR) and the Body Mass Index (BMI). BMR and BMI are calculated fora specific person. Programming focus This program uses conditional statements, if, if-else-if, and switch. Description Prompt the user to enter his/her name, age, gender, weight in pounds, height in inches, and activity level. The program must validate this input based on the following criteria: Rules to Validate Input At least one character Input Name Gender M or F (upper or lower case Weight Height At least 100 pounds Between 60 to 84 inches, inclusively At least 18 years old Age Activity Level Between 1 to 5, inclusively Based on valid input, must calculate the user's BMR, BMI, and print some other useful information for the user. Equations The Harris-Benedict formula computes the BMR based on total body weight. The formula is based on the Metric system (kilograms and centimeters) that is calculated as follows: Men: BMR- 66(13.7* wt in kg) + (5 * ht in cm) - (6.8 * age in years) Women: BMR 655(9.6* wt in kg) +(1.8 * ht in cm) (4.7 * age in years) Using accurate conversion factors, convert from inches and pounds to the metric values for use in the above equations. Research these conversion formulas on the internet.Explanation / Answer
// Class Healthy definition
public class Healthy
{
// Instance variables to store data
private String name;
private char gender;
private double weight;
private double height;
private int age;
private int activityLevel;
// Default constructor
public Healthy()
{
name = "";
gender = ' ';
weight = height = age = 0;
}// End of default constructor
// Setter methods
// Method to set name
public void setName(String name)
{
this.name = name;
}// End of method
// Method to set gender
public void setGender(char gender)
{
this.gender = gender;
}// End of method
// Method to set weight
public void setWeight(double weight)
{
this.weight = weight;
}// End of method
// Method to set height
public void setHeight(double height)
{
this.height = height;
}// End of method
// Method to set age
public void setAge(int age)
{
this.age = age;
}// End of method
// Method to set activity level
public void setActivityLevel(int activityLevel)
{
this.activityLevel = activityLevel;
}// End of method
// Getter methods
// Method to return name
public String getName()
{
return name;
}// End of method
// Method to return gender
public char getGender()
{
return gender;
}// End of method
// Method to return weight
public double getWeight()
{
return weight;
}// End of method
// Method to return height
public double getHeight()
{
return height;
}// End of method
// Method to return age
public int getAge()
{
return age;
}// End of method
// Method to return activity level
public int getActivityLevel()
{
return activityLevel;
}// End of method
// Method to return gender message
public String getGenderMessage()
{
String genderMessage = "";
// Checks if the gender is 'M' or 'm' return message for male
if(gender == 'M' || gender == 'm')
genderMessage = " These are for a male.";
// Otherwise gender is 'F' or 'f' return message for female
else
genderMessage = " These are for a female.";
// Returns gender message
return genderMessage;
}// End of method
// Overrides toString() method to display information
public String toString()
{
// Concatenates message with getter method to get the instance variable data
String information = getName() + "'s information";
information += " Weight: " + getWeight() + " puunds Height: " + getHeight() +
" inches Age: " + getAge() + getGenderMessage();
return information;
}// End of method
// Method to convert pound to KG and returns result
double convertPoundsToKg(double pound)
{
return (pound * 0.4535923);
}// End of method
// Method to convert inches to centimeter and returns result
double convertInchesToCentimeters(double inches)
{
return (inches * 2.540000);
}// End of method
// Method to calculate and return BMR
double calculateBMR()
{
double result = 0;
// Checks if the gender is male
if(getGender() == 'M' || getGender() == 'm')
result = 66 + (13.7 * convertPoundsToKg(getWeight()) + (5 * convertInchesToCentimeters(getHeight())) - (6.8 * age));
// Otherwise, gender is female
else
result = 655 + (9.6 * convertPoundsToKg(getWeight()) + (1.8 * convertInchesToCentimeters(getHeight())) - (4.7 * age));
return result;
}// End of method
// Method to calculate and return BMI
double calculateBMI()
{
return ((getWeight()) / Math.pow(getHeight(), 2) * 703);
}// End of method
// Method to calculate and return TDEE
double calculateTDEE()
{
// Checks if the activity level to calculate TDEE
if(activityLevel == 1)
return (calculateBMR() * 1.2);
else if(activityLevel == 2)
return (calculateBMR() * 1.375);
else if(activityLevel == 3)
return (calculateBMR() * 1.55);
else if(activityLevel == 4)
return (calculateBMR() * 1.725);
else
return (calculateBMR() * 1.9);
}// End of method
// Method to return BMI classification message
String BMIClassification()
{
// Checks the BMI and returns appropriate message
if(calculateBMI() < 18.5)
return "Unerweight";
else if(calculateBMI() < 25)
return "Normal weight";
else if(calculateBMI() < 30)
return "Overweight";
else
return "Obese";
}// End of method
}// End of class
-----------------------------------------------------------
package January;
import java.util.Scanner;
// Class HowHealthy definition
public class HowHealthy
{
// Scanner object declared
static Scanner sc;
// Method to accept data
void acceptData(Healthy person)
{
// Local variable to store data entered by the user
String name;
char gender;
double weight;
double height;
int age;
int activityLevel;
// Scanner class object created
sc = new Scanner(System.in);
// Loops till valid name entered by the user
do
{
// Accepts name from the user
System.out.print(" Peron's name: ");
name = sc.nextLine();
// Checks if the length of the name is not zero
if(name.length() != 0)
{
// Calls the method to set the name
person.setName(name);
// Come out of the loop
break;
}// End of if condition
// Otherwise, invalid name. Displays error message
else
System.out.println("Person name cannot be null.");
}while(true);// End of do - while loop
// Loops till valid gender entered by the user
do
{
// Accepts gender from the user
System.out.print(name + ", are you male or female (M/F): ");
gender = sc.next().charAt(0);
// Checks if the gender is either 'M' or 'm' or 'F' or 'f'
if(gender == 'M' || gender == 'm' || gender == 'F' || gender == 'f')
{
// Calls the method to set the gender
person.setGender(gender);
break;
}// End of if condition
// Otherwise, invalid gender. Displays error message
else
System.out.println("Gender can be either (M/F)");
}while(true);// End of do - while loop
// Loops till valid weight entered by the user
do
{
// Accepts weight from the user
System.out.print(name + "'s weight (pounds): ");
weight = sc.nextDouble();
// Checks if the weight is greater than or equals to 100
if(weight >= 100)
{
// Calls the method to set the weight
person.setWeight(weight);
// Come out of the loop
break;
}// End of if condition
// Otherwise, invalid weight. Displays error message
else
System.out.println("Invalid weight - must be at least 100 pounds");
}while(true);// End of do - while loop
// Loops till valid height entered by the user
do
{
// Accepts height from the user
System.out.print(name + "'s height (inches): ");
height = sc.nextDouble();
// Checks if the height is between 60 and 84 inclusive
if(height >= 60 && height <= 84)
{
// Calls the method to set the height
person.setHeight(height);
// Come out of the loop
break;
}// End of if condition
// Otherwise, invalid height. Displays error message
else
System.out.println("Invalid height - must be 60 .. 84 inclusive.");
}while(true);// End of do - while loop
// Loops till valid age entered by the user
do
{
// Accepts age from the user
System.out.print(name + "'s age (year): ");
age = sc.nextInt();
// Checks if the age is greater than or equals to 18
if(age >= 18)
{
// Calls the method to set the age
person.setAge(age);
// Come out of the loop
break;
}// End of if condition
// Otherwise, invalid age. Displays error message
else
System.out.println("Invalid age - must be at least 18 years");
}while(true);// End of do - while loop
// Loops till valid activity level entered by the user
do
{
// Displays activity level menu
System.out.print(" Activity Level: Use these categories: ");
System.out.print(" 1 - Sedentary (little or no exercise, desk job) ");
System.out.print(" 2 - Lightly active (light exercise/sports 1 - 3 days/wk) ");
System.out.print(" 3 - Moderately active (moderate exercise/sports 3 - 5 days/wk) ");
System.out.print(" 4 - Very active (hard exercise/sports 4 - 7 days/wk) ");
System.out.print(" 5 - Extra active (hard daily exercise/sports & physical job or 2x day training i.e marathon, contest etc.) ");
// Accepts activity level from the user
System.out.print(" How active are you? ");
activityLevel = sc.nextInt();
// Checks if the activity level is between 1 and 5 inclusive
if(activityLevel >= 1 && activityLevel <= 5)
{
// Calls the method to set the activity level
person.setActivityLevel(activityLevel);
// Come out of the loop
break;
}// End of if condition
// Otherwise, invalid activity level. Displays error message
else
System.out.print(" Invalid Activity Level.");
}while(true);// End of do - while loop
}// End of method
// main method definition
public static void main(String[] args)
{
// To store number of persons
int numberOfPersons;
sc = new Scanner(System.in);
// Creates an object of the HowHealthy class
HowHealthy howHealthy = new HowHealthy();
// Accepts number of persons
System.out.print(" Enter how many persons: ");
numberOfPersons = sc.nextInt();
// Creates an array of object of class Healthy for number of persons
Healthy person[] = new Healthy[10];
// Loops till number of persons
for(int x = 0; x < numberOfPersons; x++)
{
// Initializes the object using default constructor
person[x] = new Healthy();
// Calls the method to accept and validate data
howHealthy.acceptData(person[x]);
// Calls the methods to display information
System.out.println(person[x]);
System.out.printf(" BMR is %.2f", person[x].calculateBMR());
System.out.printf(" BMI is %.2f", person[x].calculateBMI());
System.out.printf(" TDEE is %.2f", person[x].calculateTDEE());
System.out.println(" Your BMI classifies you as " + person[x].BMIClassification());
}// End of for loop
sc.close();
}// End of main method
}// End of class
Sample Output:
Enter how many persons: 2
Peron's name: Tom
Tom, are you male or female (M/F): f
Tom's weight (pounds): 120
Tom's height (inches): 66
Tom's age (year): 30
Activity Level: Use these categories:
1 - Sedentary (little or no exercise, desk job)
2 - Lightly active (light exercise/sports 1 - 3 days/wk)
3 - Moderately active (moderate exercise/sports 3 - 5 days/wk)
4 - Very active (hard exercise/sports 4 - 7 days/wk)
5 - Extra active (hard daily exercise/sports & physical job or 2x day training i.e marathon, contest etc.)
How active are you? 3
Tom's information
Weight: 120.0 puunds
Height: 66.0 inches
Age: 30
These are for a female.
BMR is 1338.29
BMI is 19.37
TDEE is 2074.35
Your BMI classifies you as Normal weight
Peron's name:
Person name cannot be null.
Peron's name: Pyari
Pyari, are you male or female (M/F): t
Gender can be either (M/F)
Pyari, are you male or female (M/F): m
Pyari's weight (pounds): 50
Invalid weight - must be at least 100 pounds
Pyari's weight (pounds): 220
Pyari's height (inches): 20
Invalid height - must be 60 .. 84 inclusive.
Pyari's height (inches): 90
Invalid height - must be 60 .. 84 inclusive.
Pyari's height (inches): 65
Pyari's age (year): 10
Invalid age - must be at least 18 years
Pyari's age (year): 20
Activity Level: Use these categories:
1 - Sedentary (little or no exercise, desk job)
2 - Lightly active (light exercise/sports 1 - 3 days/wk)
3 - Moderately active (moderate exercise/sports 3 - 5 days/wk)
4 - Very active (hard exercise/sports 4 - 7 days/wk)
5 - Extra active (hard daily exercise/sports & physical job or 2x day training i.e marathon, contest etc.)
How active are you? 5
Pyari's information
Weight: 220.0 puunds
Height: 65.0 inches
Age: 20
These are for a male.
BMR is 2122.63
BMI is 36.61
TDEE is 4032.99
Your BMI classifies you as Obese
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.