I'm really confused with the concept of static vs instance methods. I have created a BMR calculator. I've seperated the GUI from the calculations using different classes.
public class Calculations {
/**
* If user input is correct, this method will calculate the BMR value of the user given their input and measurement choices.
*
* @param userAge, userHeight, userWeight
* @return BMR Value as a string
*/
public static int calcBMR(int age, String gender, double height, double weight) {
// This is the body of the calculations - different formulas used depending on gender. Conversions to kg and cm done earlier so no conversions needed here.
if (gender.equals("M")) { // Uses male bmr formula if genderMale radiobutton is selected
return (int) (Math.round((10 * weight) + (6.25 * height) - (5 * age) + 5)); // This is the Miffin St-Jeor formula, calculations done in cm/kg
} else { // else gender.equals("F") - there are only 2 options for gender, M or F.
return (int) (Math.round((10 * weight) + (6.25 * height) - (5 * age) - 161));
}
}
/**
* If the user selects the TDEE option, this method will be executed after the calcBMR() method.
* A value from the calcBMR() method will be passed down to this method, and is multiplied
* by the activity level parameter passed into this method.
*
* @param selectedActivityLevel
* @return TDEE Value (as a string)
*/
public static int calcTDEE(double activityMultiplier, int bmr) {
System.out.println(activityMultiplier);
return (int) Math.round(bmr * activityMultiplier);
}
}
As you can see, the methods are STATIC, however the variables being passed through (to both methods) are instance variables.
I am only calling these methods through the following lines:
bmrValue = Calculations.calcBMR(userAge, userGender, userHeight, userWeight);
bmrLabel.setText("<html><br /><font size=4>You have a <i><font color=#ce0000>BMR</font></i> of: " + "<font color=#59AF0E>" + bmrValue + "</font></html>");
if (tdeeYes.isSelected()) {
userActivityLevel = activityMap.get(activityLevelBox.getSelectedItem());
// Looks up selected item from combo box, which is the KEY. Then looks up the value to this key from the map - this value is the TDEE multiplier.
tdeeLabel.setText("<html><br /><font size=4>You have a <i><font color=#ce0000>TDEE</font></i> of: " + "<font color=#59AF0E>" + Calculations.calcTDEE(userActivityLevel, bmrValue) + "</font></html>");
}
The variables are defined as:
HashMap<String, Double> activityMap;
String[] activityLevels = {"Sedentary", "Lightly Active", "Moderately Active", "Very Active", "Extra Active"};
int userAge;
String userGender;
double userHeight;
double userWeight;
double userActivityLevel;
int bmrValue;
Am I using static/instance variables correctly? Earlier I had all my parameter variables as static, as I know static methods can only access static variables. I didn't know that the parameters could be instance variables until now.
Any guidance would be appreciated.