Reference no: EM132430698
Write the code for the HarrisBenedict program to accept input for a person's weight, height, and age, and then display the basal metabolic rate for that person.
Afterwards, ask the user to choose from a selection of foods and then output the number they may eat to maintain their current body weight based on their BMR.
Note:
- Use named constants to store the number of calories for each food item.
- You are required to perform input validation for the sex user input. Force the user to enter m, M, f, or F.
- You are required to perform input validation for the food selection input. Force the user to enter 1, 2, or 3.
- You are not required to perform input validation for the weight, height, or age inputs.
Sample session:
Welcome to the basal metabolic rate calculator.
Are you male 'm' or female 'f': T
Invalid input. Please enter 'm' for male or 'f' for female.
Are you male 'm' or female 'f': m
How much do you weight (in pounds)?: 160
How tall are you (in inches)?: 70
What is your age?: 22
You entered:
Sex: m
Weight: 160 lbs
Height: 70 inches
Age: 22 years old
Your basal metabolic rate is 1798.28
What food would you like to eat?
1) Chocolate Bar (210 calories)
2) Pepperoni Pizza Slice (300 calories)
3) Baby Carrot (4 calories)
Selection: 4
Sorry, that's not a selection. Try another number.
Selection: 3
With your BMR, you can eat 449.57 baby carrots to maintain your body weight.
Thank you for using the basal metabolic rate calculator.
This is what I have so far:
import java.util.Scanner;
public class BMR
{
public static void main(String[] args)
{
int userChoice;
int age = 0;
double kgToPounds = 2.2046;
double inchesToCms = 2.54;
double heightInCms;
double WeightInkg;
double rate = 0;
Scanner in = new Scanner ( System.in );
System.out.print("Welcome to the basal metabolic calculator.");
System.out.println();
System.out.print("Are you male 'm' or female 'f': ");
String gender = in.next();
System.out.print("What is your age? ");
String ageString = in.next();
age = Integer.parseInt(ageString);
System.out.print("How tall are you (in inches)? ");
String heightString = in.next();
double height = Double.parseDouble(heightString);
System.out.print("How much do you weigh (in pounds)? ");
String weightString = in.next();
double weight = Double.parseDouble(weightString);
heightInCms = height * inchesToCms; // Do metric conversion
WeightInkg = weight / kgToPounds; // Do metric conversion
System.out.println("Calculate your Basal Metabolism");
System.out.print("n");
if (gender.startsWith("M")) {
System.out.println(" I am a male ");
rate = (66.5 + (13.75 * WeightInkg) + (5.003 * heightInCms) - (6.755 * age));
}
else {
rate = (655.1 + (9.563 * WeightInkg) + (1.850 * heightInCms) - (4.676 * age));
}
System.out.println("You entered: ");
System.out.println("Sex: " + gender);
System.out.println("Weight (kg): " + WeightInkg);
System.out.println("Height (cm): " + heightInCms);
System.out.println("Age: " + age);
System.out.println();
System.out.println("You basal metabolic rate is " + rate);
}
}