In: Computer Science
JAVA CODE BEGINNER , Please use comments to explain, please
Repeat the calorie-counting program described in Programming Project 8 from Chapter 2. This time 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 female formula to calculate calories if “W” is entered. Output the number of chocolate bars to consume as before.
and this project 8:
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)
A typical chocolate bar will contain around 230 calories. Write a program that allows the user to input his or her weight in pounds, height in inches, and age in years. The program should then output the number of chocolate bars that should be consumed to maintain one’s weight for both a woman and a man of the input weight, height, and age.
Java Code
:
import java.util.Scanner;
// for using Scanner class
public class Solution
{
public static void main(String args[])
{
Scanner obj = new
Scanner(System.in);
// Creating Scanner class object
System.out.print("\nInput the
String : "); //
Asking for the input of the String
String s =
obj.next();
// taking input of String
System.out.println();
double BMR = 0.0;
// it will store the value of
BMR
if(s.equals("W"))
// if the input String is "W", then we'll go with Woman
{
System.out.print("\nEnter the weight in pounds for woman :
"); // Asking for
the input of weight in pounds for woman
double weight = obj.nextDouble();
// taking input of weight in
pounds for woman
System.out.print("\nEnter the height in inches for woman :
"); // Asking for
the input of height in inches for woman
int height = obj.nextInt();
// taking input of height in
inches for woman
System.out.print("\nEnter the age in years for woman :
"); // Asking for
the input of age in years for woman
int age = obj.nextInt();
// taking input of age in
years for woman
BMR = 655 + (4.3 * weight) + (4.7 * height) - (4.7 *
age); //
Calculating BMR for woman
}
else
if(s.equals("M"))
// if the input String is "M", then we'll go with Man
{
System.out.print("\nEnter the weight in pounds for man :
"); // Asking for
the input of weight in pounds for man
double weight = obj.nextDouble();
// taking input of weight in
pounds for man
System.out.print("\nEnter the height in inches for man :
"); // Asking for
the input of height in inches for man
int height = obj.nextInt();
// taking input of height in
inches for man
System.out.print("\nEnter the age in years for man :
"); // Asking for
the input of age in years for man
int age = obj.nextInt();
// taking input of age in
years for man
BMR = 66 + (6.3 * weight) + (12.9 * height) - (6.8 *
age); //
Calculating BMR for man
}
double choc = BMR /
230.0;
// Calculating number of chocolate bars
required
int chocolates =
Math.round((float)choc); // Rounding
off the double value of number of chocolate bars required for
getting exact number of chocolates
System.out.print("\n\nThe number of chocolate bars that should be
consumed : " + chocolates + "\n\n" );
// printing number of
chocolate bars
}
}
Java Code Output :