0

I have an assignment in Java, where I have to calculate the BMI of a particular user, in an array of users, an array of heights and an array of weights.

Below is the assignment:

  String[] names; // users names
                String[] familyNames; // familyNames
                int[] weight; // users weights in kilograms (kg)
                double[] height; // users height in meter (m)


                names = new String[] { "Alex", "Maria", "Anna", "Adam", "Sara", "Johan", "Frederik"};
                familyNames = new String[] {"Andersson", "Johansson", "Nordin", "Holmgren", "Svensson"};
                weight = new int[] {70, 50, 60, 50, 60, 87, 130 };
                height = new double[] { 1.80, 1.70, 1.57, 1.80, 1.69, 1.85, 1.85 };

        public static void calculateBMI(String name, String[] names, int[] weight, double[] height) {


                /*
                 * This method should be changed
                 * 
                 * Calculate and print out BMI (body mass index) for each user 
                 * BMI = weight in kg/height in meter * height in meter 
                 * 
                 * Check if the user is Underweight, Normal, Overweightor or Obese based on the BMI
                 * Underweight, when bmi is less than 18.5 
                 * Normal, when bmi is between 18.5 and 24.9 
                 * Overweight, when bmi is between 25 and 29.9
                 * Obese, when bmi is more than 30
                 * 
                 * 
                 * To check if a string is equal to another string use:
                 * stringVariable.equalsIgnoreCase(anotherStringVariable)
                 * 
                 */

            }

public static void calculateWeight(String name, String[] names, int[] weight, double[] height) {

        /*
         * This method should be changed
         * 
         * Calculate and print out the weight that the user has to loose or gain
         * 
         * Let's consider that the formula for ideal body weight is (Broca Index:):
         * Ideal Body Weight (kg) = (Height (cm) - 100) - ((Height (cm) - 100) x 10%)
         * 
         * 1 meter = 100 cm
         * 
         * 
         */
        //Your code starts here


    }// end calculateWeightLoss

I have tried doing this:

double userBMI = 0;
   for(int i = 0; i < weight.length; i++){
                for(int j = 0; j < height.length; j++){
                    userBMI = weight[i]/height[j] * height[j];
                }
            }

But I feel I am not on track, because the method when in use will be:

calculateBMI("Frederik",names, weight,height); // calculate user BMI
        calculateWeight("Frederik", names, weight, height); // calculate the weight loss or gain for the user
        System.out.println("");

So I have to find for example "frederik" in the array of names, and calculate his BMI. Any ideas will be aappreciated.

user8107351
  • 367
  • 4
  • 20
  • First of all userBMI = weight[i]/height[j] * height[j]; is not a correct way to calculate BMI – Rafał Sokalski Nov 07 '18 at 07:48
  • What exactly should do method calculateBMI because it says that it should calculate BMI for each user but you pass name to this method ? – Rafał Sokalski Nov 07 '18 at 07:54
  • You should study [Classes and Objects](https://docs.oracle.com/javase/tutorial/java/javaOO/index.html) from the Java Lessons. You should not put all these data into a bunch of arrays, but use a class like `Person` instead. See [László Stahorszki's answer](https://stackoverflow.com/a/53185522/507738). It looks like the `calculateWeight` method is defined by the assignment, but it's bad design. – MC Emperor Nov 07 '18 at 08:35

3 Answers3

0

It print the BMI when passed to the method name match with the name in "names" array

public static void calculateBMI(String name, String[] names, int[] weight, double[] height) {

    for (int i = 0; i < names.length; i++) {
        if(names[i].equalsIgnoreCase(name))
            System.out.println(weight[i] / height[i] / height[i]);
    }
}

But it is risky because if the length of names array is bigger than weight or height it will throw ArrayIndexOutOfBoundsException but I assume that arrays have always properly length. If there is possibility that the length of these arrays might be different I suggest to add some conditions

And the second method:

public static void calculateWeight(String name, String[] names, int[] weight, double[] height) {

    for (int i = 0; i < names.length; i++) {
        if (names[i].equalsIgnoreCase(name)) {
            double perfectWeight = ((height[i] * 100) - 100) - (((height[i] * 100) - 100) / 10);
            System.out.println("Perfect weight for that height is : " + perfectWeight);
            if(weight[i] == perfectWeight)
                System.out.println("Person has perfect weight");
            else if(weight[i] > perfectWeight)
                System.out.println("Person should lose: " + (weight[i] - perfectWeight));
            else
                System.out.println("Person should gain: " + (perfectWeight - weight[i]));
        }

    }
}

First it calculate the perfect weight and then it is compared to Person weight and print if the person should lose or gain some kg

Rafał Sokalski
  • 1,817
  • 2
  • 17
  • 29
0

I'd suggest you to write a class, let's call it Person. In each instance of this class, you can store the name, the height and weight of a given person.

class Person {
    private String name;
    private String familyName;
    private int weight;
    private double height;
}

In this class, it's really simple, to write a method which returns the BMI. To create these instances, you can use a method similar to the answer above, however, I'd recommend you do addiditonal checks, whether the arrays are of the same size.

László Stahorszki
  • 1,102
  • 7
  • 23
0

First, I'm not going to write that code for you, I think you should eventually do it on your own.

Here are some hints:

  • The assignment is not quite accurate. It says "calculate and print out BMI (body mass index) for each user", but then it requires you to implement a method accepting a specific name as the first argument. The fact that the description mentions the equalsIgnoreCase method, suggests that you need to display the BMI only of that given user.
  • So you need a sort of loop, perhaps a for loop. Within the loop, check if the name is correct. You don't need a nested loop, why do you want to use a nested loop?
  • You may assert that all given arrays are equal of length, since the assignment does not mention array lengths. You might want to re-check the length of familyNames, since it's shorter than the other arrays.
  • You might want to create a new method which does the actual BMI calculation, and refer to it from the calculateBMI method:

    private static double calculateActualBmi(int weigth, double height) {
        return ...; // perform calculation
    }
    

    And separate the actual calculation from describing the BMI.

    private static String describeBmi(double bmi) {
        if (bmi < 18.5) {
            return "Underweight";
        }
        else if ...
            ...
        else {
            return "Obese";
        }
    }
    
  • The other method kind of works the same way.


Further notes

  • I'm surprised that the assignment requires you to write the method calculateBMI(String name, String[] names, int[] weight, double[] height) like this. This design misses the whole point of object-oriented programming. See László Stahorszki's answer.

  • You could declare and initialize the arrays at once, i.e.

    String[] names = new String[] { "Alex", ..., "Frederik" };
    
MC Emperor
  • 22,334
  • 15
  • 80
  • 130