In: Computer Science
Decision Structures Calorie Calculator Assignment Overview This assignment will give you practice with numerical calculations, simple input/output, and if-else statements. Program Objective: In this exercise, you will write a program the calculates the Daily Caloric needs for weight lose diet. According to everydayhealth.com they recommend this daily caloric formula for a weight loss diet: BMR + Activity Level - 500 calories. Calories Calculator The Harris-Benedict equation estimates the number of calories your body needs to maintain your weight if you do no exercise. This is called your basal metabolic rate or BMR. The Calories needed for a woman to maintain her weight is: BMR = 655 + (4.3 * weight in pounds) + (4.7 * height in inches) - (4.7 * age in years) The Calories needed for a man to maintain his weight is: BMR = 66 + (6.3 * weight in pounds) + (12.9 * height in inches) - (6.8 * age in years) Next, the total daily caloric requirement is calculated by multiplying your BMR by your level of activity: • If the person is inactive (rarely exercise), then increase the calculated BMR 20%. • If the person is somewhat active, then increase the calculated BMR by 30% • If the person is active, then increase the calculated BMR by 40% • If the person is highly active then increase the calculated BMR by 50% Program Specification: Write a program that allows the user to input their weight in pounds, height in inches, and age in years. Ask the user to input the string “M” if the user is a man and “W” if the user is a woman. Use only the male formula to calculate calories if “M” is entered and use only the women formula to calculate calories if “W” is entered. Ask the user if he or she is: A. Inactive B. Somewhat active (exercise occasionally) C. Active (exercise 3-4 days per week) D. Highly active (exercise every day) • If the user answers “Inactive” then increase the calculated BMR by 20 percent. • If the user answers “Somewhat active” then increase the calculated BMR by 30 percent. • If the user answers “Active” then increase the calculated BMR by 40 percent. • Finally, if the user answers “Highly active” then increase the calculated BMR by 50 percent. • The program should then output the calculated daily Calories intake for this person in order to lose one pound per week by subtracting 500 calories from the calculated BMR. Input Errors • If the user enters an age either < 1 or > 100, instantly end the program with an error message. • If the user enters a height < 10 or > 100, instantly end the program with an error message. • If the user enters a weight < 10 or > 500, instantly end the program with an error message. • You can assume that the user does not type some non-numeric value. You don't have to test for this kind of non-numeric error. • If the user does not enter ‘M' for male calculation or 'W' for female calculation, instantly end the program with an error message. • If the user does not enter A, B, C, or D when prompted, instantly end the program with an error message. These limits must be defined as symbolic constants, which should be used throughout the program. Rules: • For this assignment you are limited to the Java features in Chapters 1 through 3; you are not allowed to use more advanced features to solve the problem. Please do not use Java features that are not covered in lecture or the textbook. • Use class constants as appropriate to represent important fixed data values in your program. • Follow programming guidelines and Java’s naming standards about the format of ClassNames, variableNames, and CONSTANT NAMES. • Notice that all real numbers output by the program are printed with no more than 2 digits after the decimal point. To achieve this, you may use the System.out.printf method. • Add the header to the class file and copy/paste the output that is produced by your program into the bottom of the source code file, making it into a block comment. • Class Name: Your program class should be called CalorieCalculator.
/*********************************CalorieCalculator.java****************************/
import java.util.Scanner;
/**
* The Class CalorieCalculator.
*/
public class CalorieCalculator {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
Scanner scan = new
Scanner(System.in);
char option;
do {
double bmr =
0;
System.out.print("Input Weight in pounds: ");
double weight =
scan.nextDouble();
if (weight <
10 || weight > 500) {
System.out.println("Invalid Weight!");
break;
}
System.out.print("Input Height in inches: ");
double height =
scan.nextDouble();
if (height <
10 || height > 100) {
System.out.println("Invalid Height!");
break;
}
System.out.print("Input Age in years: ");
int age =
scan.nextInt();
if (age < 1
|| age > 100) {
System.out.println("Invalid age!");
break;
}
scan.nextLine();
System.out.print("Enter Gender: ");
char gender =
scan.nextLine().toUpperCase().charAt(0);
if (gender ==
'M') {
bmr = 655 + (6.3 * weight) + (12.9 * height) -
(6.8 * age);
} else if
(gender == 'W') {
bmr = 655 + (4.3 * weight) + (4.7 * height) -
(4.7 * age);
}
menu();
System.out.print("Which Type of person you are: ");
String type =
scan.nextLine();
if
(type.equalsIgnoreCase("a")) {
bmr+=bmr * .20;
} else if
(type.equalsIgnoreCase("b")) {
bmr += bmr *.30;
} else if
(type.equalsIgnoreCase("c")) {
bmr += bmr * .40;
} else if
(type.equalsIgnoreCase("d")) {
bmr += bmr * .50;
}
System.out.printf("Daily Caloric needs for weight lose diet: %.2f Calories\n", (bmr - 500));
System.out.println("Do you want to continue(Y/N): ");
option =
scan.nextLine().toUpperCase().charAt(0);
} while (option != 'N');
}
/**
* Menu.
*/
private static void menu() {
System.out.println("A. Inactive
\n" + "B. Somewhat active (exercise occasionally) \n"
+ "C. Active (exercise 3-4 days per week) \n" +
"D. Highly active (exercise every day)");
}
}
/****************output******************/
Input Weight in pounds: 100
Input Height in inches: 70
Input Age in years: 30
Enter Gender: M
A. Inactive
B. Somewhat active (exercise occasionally)
C. Active (exercise 3-4 days per week)
D. Highly active (exercise every day)
Which Type of person you are: B
Daily Caloric needs for weight lose diet: 2079.20 Calories
Do you want to continue(Y/N):
Y
Input Weight in pounds: 200
Input Height in inches: 78
Input Age in years: 25
Enter Gender: W
A. Inactive
B. Somewhat active (exercise occasionally)
C. Active (exercise 3-4 days per week)
D. Highly active (exercise every day)
Which Type of person you are: C
Daily Caloric needs for weight lose diet: 1969.74 Calories
Do you want to continue(Y/N):
N
Please let me know if you have any doubt or modify the answer, Thanks :)