2
\$\begingroup\$

I'm learning java this week. Please reviews my code for BMI calculator using java, need some your feedback. Thanks before!

This Assignment:

BMI Calculator and expected output

My code

public class bmiCalculator extends ConsoleProgram {
public void run() {
    println("Person 1's information: ");
    bmiCalculation(70.0, 194.25);
    
    println("");
    
    println("Person 2's information: ");
    bmiCalculation(62.5, 130.5);
    
    println("");
    
    print("Have a nice day!");
    
    }
public void bmiCalculation(double height, double weight) {
    
    // bmi algo = BMI = weight / height ** 2 * 703
    
    double newHeight = 1;
    double bmi = 0;
    for (int i = 0; i < 2; i ++) {
        newHeight *= height;
    }
    bmi = weight / newHeight * 703;
    println(bmi);
    classifications(bmi);
    
}
public void classifications(double bmi) {
    if(bmi <= 18) {
        println("class 1");
    }
    else  if(bmi <= 24.9) {
        println("class 2");
    }
    else if(bmi <= 29.9) {
        println("class 3");
    }
    else {
        println("class 4");
    }
}

}

\$\endgroup\$
2
  • 3
    \$\begingroup\$ You've hard coded the people's information, rather than prompting the user for it from the console. Since it's in the problem description it's probably part of what you're being assessed on. \$\endgroup\$
    – forsvarir
    Nov 5 '21 at 9:46
  • 1
    \$\begingroup\$ Welcome to Code Review! Your image of text isn't very helpful. It can't be read aloud or copied into an editor, and it doesn't index very well. Please edit your post to incorporate the relevant text directly (preferably using copy+paste to avoid transcription errors). \$\endgroup\$ Nov 5 '21 at 10:36
4
\$\begingroup\$
public void bmiCalculation(double height, double weight) {

  // bmi algo = BMI = weight / height ** 2 * 703

  double newHeight = 1;
  double bmi = 0;
  for (int i = 0; i < 2; i ++) {
      newHeight *= height;
  }
  bmi = weight / newHeight * 703;
  println(bmi);
  classifications(bmi);
}

Seems overly complicated to me. You can one line it (and maybe move it to its own method):

bmi = (weight * 703) / (height * height);

Your output seems off with the assignments table, it mentions < 18.5:

public void classifications(double bmi) {
    if(bmi <= 18) {
        println("class 1");
    }
\$\endgroup\$
0
\$\begingroup\$

In addition to other comments, I would suggest a change in the way you structure this program. Your functions that perform calculations and then print the output will be difficult to write automated tests for. As a first pass, I would modify your classifications function to return a String, and then perform the println from the calling function. You can then go one step further, and refactor to move all of the println calls into the run method, leaving you with two pure functions that take arguments and return a calculated result, without any side-effect behaviour.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.