java - Should I be using static or instance methods? -


i'm confused concept of static vs instance methods. have created bmr calculator. i've seperated gui calculations using different classes.

public class calculations {  /**  * if user input correct, method calculate bmr value of user given input , measurement choices.  *   * @param userage, userheight, userweight  * @return bmr value string  */ public static int calcbmr(int age, string gender, double height, double weight) {      // body of calculations - different formulas used depending on gender. conversions kg , cm done earlier no conversions needed here.     if (gender.equals("m")) { // uses male bmr formula if gendermale radiobutton selected         return (int) (math.round((10 * weight) + (6.25 * height) - (5 * age) + 5)); // miffin st-jeor formula, calculations done in cm/kg     } else { // else gender.equals("f") - there 2 options gender, m or f.         return (int) (math.round((10 * weight) + (6.25 * height) - (5 * age) - 161));     } }   /**  * if user selects tdee option, method executed after calcbmr() method.   * value calcbmr() method passed down method, , multiplied  * activity level parameter passed method.  *   * @param selectedactivitylevel  * @return tdee value (as string)  */ public static int calctdee(double activitymultiplier, int bmr) {     system.out.println(activitymultiplier);     return (int) math.round(bmr * activitymultiplier); } 

}

as can see, methods static, variables being passed through (to both methods) instance variables.

i calling these methods through following lines:

            bmrvalue = calculations.calcbmr(userage, usergender, userheight, userweight);             bmrlabel.settext("<html><br /><font size=4>you have <i><font color=#ce0000>bmr</font></i> of: " + "<font color=#59af0e>" +  bmrvalue + "</font></html>");             if (tdeeyes.isselected()) {                 useractivitylevel = activitymap.get(activitylevelbox.getselecteditem());                 // looks selected item combo box, key. looks value key map - value tdee multiplier.                 tdeelabel.settext("<html><br /><font size=4>you have <i><font color=#ce0000>tdee</font></i> of: " + "<font color=#59af0e>" + calculations.calctdee(useractivitylevel, bmrvalue) + "</font></html>");                 } 

the variables 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 using static/instance variables correctly? earlier had parameter variables static, know static methods can access static variables. didn't know parameters instance variables until now.

any guidance appreciated.

for starters, difference between static , instance variables that, 1 static variable exists instances of class, whereas instance variable exists every instance of class.

now, when talking methods, in cases need make method static when trying call static method (such main).

in general practice wrong oop, , chances should rethink way program structured.

if provide more details code use call these methods, able more details on how fix this.

edit: in light of new info provided:

1) believe bmrmain,bmrmain , calculations can merged 1 class. constructor of new class should adjusted read input (as getters , setters each variable added flexibility - optional)

2) regarding calculation of bmr part, there many ways tackle this, go ahead , suggest 1 best in opinion. add button text "click calculate" or similar, , implement actionlistener. action listener in turn call (whenever button clicked) method calculates everything, , change text on jlabel.

sample code action listener:

jbutton button = new jbutton("text button"); button.addactionlistener(new actionlistener() {   public void actionperformed(actionevent e)   {     //call methods calculation     // bmr , tdee temporary variables store results methods     //handle them properly, or remove them if not needed (i wrote sample)     int bmr = calcbmr(...); //pass correct variables arguments here     int tdee;     if(userhaspickedtdee) {  tdee = calctdee(...); }  //and here     label.settext(....);    } });  

these 2 steps take care of "static" problem.

i recommend research on event-driven programming

and here , more in-depth reading on action listeners

if need further help, or clarifications let me know :)

edit2:

in general, yes practice separate classes in order keep code manageable. in case think bit superfluous create new class 2 methods. nevertheless if keep current structure, way fix "static" is: 1) remove static 2 calculation methods. 2)line 332 should

calculations c = new calculations(); bmrvalue = c.calcbmr(userage, usergender, userheight, userweight); 

final_edit:

well, these questions can transferred thread of own. here useful link quick google search did, demistify static keyword:

static keyword in java


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -