I am working on a Java assignment in which I was given a set of guidelines, and asked to correctly write a program that calculates the daily calorie requirement for people who engage in light sports/exercise 1 – 3 days per week based on their gender, weight, height, and age.
One of the main points of the assignment is to learn the use of exceptions
. I have written the code, and it compiles correctly. However, when run, I am always thrown my exception, even in the gender variable is set to F, f, M or m. Any hints are good hints, but I am wondering how to get this program off the ground.
For clarifying, all the comments are from the teacher, while all of the code is from me. I use the comments as a framework for my code. Also, you will notice through the teacher comments that I am supposed to have another thrown IllegalArugmentException regarding the other parameters in calculateCalories(). I have not done this yet, as I am still working on getting the gender portion correct.
import java.util.*;
//add class javadoc comment here
public class CalorieCalculator {
public static final double LIGHT_ACTIVITY_FACTOR = 1.375;
public static final int MALE_BMR_CONSTANT = 66;
public static final int FEMALE_BMR_CONSTANT = 655;
public static final double MALE_WEIGHT_COEFFICIENT = 6.23;
public static final double FEMALE_WEIGHT_COEFFICIENT = 4.35;
public static final double MALE_HEIGHT_COEFFICIENT = 12.7;
public static final double FEMALE_HEIGHT_COEFFICIENT = 4.7;
public static final double MALE_AGE_COEFFICIENT = 6.8;
public static final double FEMALE_AGE_COEFFICIENT = 4.7;
//Prompt the user for gender (m,f,M,F), weight(lbs, double),
//height(inches, integer), and age(yrs, integer).
//You can assume the user will type in a number for weight
//and an integer for height and age - it is okay if the
//program crashes if they don't.
//Pass the user supplied values to the
//calculateCalories() method and output the returned daily calorie
//requirement (with descriptive text).
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.print("Please enter your gender: ");
String gender1 = console.next();
System.out.print("Please enter your weight in pounds: ");
double weight1 = console.nextDouble();
System.out.print("Please enter your height in inches: ");
int height1 = console.nextInt();
System.out.print("Please enter your age in years: ");
int age1 = console.nextInt();
calculateCalories(gender1, weight1, height1, age1);
System.out.println("Women:");
System.out.println("Daily Calorie Requirement for Light Activity = " + (calculateCalories(gender1, weight1, height1, age1)));
}
//Throw an IllegalArgumentException if gender is not
//"f", "F", "m", or "M" or if any of the other parameters
//are not greater than 0. The exception message should
//explain the error.
//Use the formulas given below to calculate and return
//the daily calorie requirement.
public static double calculateCalories(String gender, double weight, int height, int age) {
if (gender.toLowerCase() != "m" || gender.toLowerCase() != "f") {
throw new IllegalArgumentException("Please enter either M, F, m, or f");
} else {
if (gender == "f" || gender == "F") {
double bmrFemale = (FEMALE_BMR_CONSTANT + (FEMALE_WEIGHT_COEFFICIENT * weight) + (FEMALE_HEIGHT_COEFFICIENT * height) - (FEMALE_AGE_COEFFICIENT * age));
return LIGHT_ACTIVITY_FACTOR * bmrFemale;
} else {
double bmrMale = (MALE_BMR_CONSTANT + (MALE_WEIGHT_COEFFICIENT * weight) + (MALE_HEIGHT_COEFFICIENT * height) - (MALE_AGE_COEFFICIENT * age));
return LIGHT_ACTIVITY_FACTOR * bmrMale;
}
}
}