简体   繁体   中英

Can't figure out arguments for method

I have a school task in which I have to create a code that creates and fills an array, then with those data. I have to calculate BMI, decide if their BMI is normal and the names of the people whose BMI is normal should be outputted to the console.

Where I'm stuck is when calling the method to output the names, I can't find out what would be the correct arguments. There might be other problems with it, correct me in that case as well.

Here is my code:

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

class TreatedPerson {

    String name = "";
    int age;
    int height;
    int weight;

    @Override
    public String toString() {
        return "Name: " + name + " \tAge: " + age + " \tHeight: " + height + " \tWeight: " + weight;
    }
}

public class B1 {

    static void fillTreatedPerson(ArrayList<TreatedPerson> treatedpeople, int db) {
        Random rnd = new Random();
        for (int i = 0; i < db; i++) {
            TreatedPerson tp = new TreatedPerson();
            tp.name = ("Szemely_" + (i + 1));
            tp.age = rnd.nextInt(99 - 10 + 1) + 10;
            tp.height = rnd.nextInt(200 - 150 + 1) + 150;
            tp.weight = rnd.nextInt(150 - 40 + 1) + 40;
            treatedpeople.add(tp);
        }
    }

    static void outputTreatedPerson(ArrayList<TreatedPerson> treatedpeople) {
        for (TreatedPerson tp : treatedpeople) {
            System.out.println(tp);
        }
    }

    static double BMI(TreatedPerson tp) {
        return tp.weight / (tp.height * tp.height);
    }

    static boolean normalBMI(double bmi) {
        boolean bmindex = false;
        if (bmi > 18.5 && bmi < 24.99) {
            return bmindex = true;

        }
        return bmindex;
    }

    static void ifnormalBMI(TreatedPerson tp, boolean normalBMI) {
        if (normalBMI) {
            System.out.println(tp.name);
        }
    }

    public static void main(String[] args) {
        ArrayList<TreatedPerson> treatedpeople = new ArrayList<>();
        fillTreatedPerson(treatedpeople, 5);
        outputTreatedPerson(treatedpeople);
        ifnormalBMI();
    }
}

Since ifnormalBMI takes one TreatedPerson and one boolean as arguments, you'll have to call it in a loop to print out the names of people with normal BMI. Something like this:

for (TreatedPerson tp : treatedpeople) {
    double bmi = BMI(tp);
    boolean normal = normalBMI(bmi);
    ifnormalBMI(tp, normal);
}

You could make this a method similar to outputTreatedPerson so that you only have to call it in one line in your main method.


One other problem that I see in your code is this method:

static boolean normalBMI(double bmi) {
    boolean bmindex = false;
    if (bmi > 18.5 && bmi < 24.99) {
        return bmindex = true;

    }
    return bmindex;
}

You're returning a boolean based on a condition, which is a boolean . You don't need to do an if statement there at all. That whole method can be reduced to

static boolean normalBMI(double bmi) {
    return (bmi > 18.5 && bmi < 24.99);
}

You should:

  1. Loop over all the people
  2. Calculate their BMI
  3. Determine whether the BMI is normal
  4. If so, print their name


for (TreatedPerson person : treatedpeople)
{
    double bmi = BMI(person);
    boolean isNormalBmi = normalBMI(bmi);
    ifnormalBMI(person, isNormalBmi);
}

You have another bug in your program in that BMI will always be calculated to be zero. That is because in the following method you are performing integer division :

static double BMI(TreatedPerson tp) {
    return tp.weight / (tp.height * tp.height);
}

In addition to this, BMI is calculated using height in meters. You appear to have stored height in centimeters. As such, all your BMI values come out very, very small (less than 1).

You should Instantiate object outside for loop and not inside Use getters for simplicity ( Google if u don't know about getters ) Pass arraylist to BMI method , Now use for loop to calculate bmi of every person . You are not calculating bmi of only one person . right? That's why for loop required. calculate bmi by formula. And store it in arraylist. Next, pass value of bmi to checkBMI method ( double bmi ) Again Create for loop ( int i =0 ; I< arraylist.size() ; i++ ) if ( arraylist.get ( i ).getBMI > 18.5 && if arraylist.get ( i ).getBMI < 24.99 ) System.out println( arraylist.get(i).getName );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM