-1

My first get method should return whether someone is underweight, normal weight, over weight or obese according to their BMI.

The second get method, i was hoping, would return the same variable 'category' from the first get method (so whatever the answer was, underweight, normal weight etc..), without needing to input a double in the parentheses when i called it in the main method.

Just some background, I've created people objects in main and given them info (name, height, weight, BMI, category), then put all those people in an array list. Which is what I'm calling from.

I can call everything besides my category, which returns 'null'. Im assuming its because my second get method does not have any data assigned to it. But I'm at a loss as how to get it to return the variable from the get method above, or if thats not the proper way to do it and i should be considering something else.

ANY help or suggestions would be very appreciated. If something doesn't make sense or you need more code let me know.

    public String getCategory(double bmi) {
    if (bmi < 18.5) {
        category = "UNDERWEIGHT";
    } else if ((bmi > 18.5) && (bmi < 25)) {
        category = "NORMAL WEIGHT";
    } else if ((bmi > 25) && (bmi < 30)) {
        category = "OVERWEIGHT";
    } else if (bmi > 30) {
        category = "OBESE";
    }

    return category;
}

public String getCategory() {
    return category;
}

My array list with my people objects info, in main..

            ArrayList<Person> allInfo = new ArrayList<Person>();
    {
        allInfo.add(andrewsInfo);
        allInfo.add(boydsInfo);
        allInfo.add(cathysInfo);
        allInfo.add(donnasInfo);
    }

Just to see if it worked i printed it...

    System.out.println(allInfo.get(0).getCategory());

Returned 'null'

4 Answers4

2

Two problems I can see.

  1. Your if-conditions do not take into account BMI == 18.5 or 25 or 30

  2. You have an overloaded method -- String getCategory() probably returns this.category, and getCategory(double bmi) which you have shown.

You are calling the first variant, which isn't really necessary. You don't need to store category as a field (which will be null if it is uninitialized, and that is what you are printing) because it is dynamically calculated based on the bmi value.

You should instead be storing the BMI as a double value in the Person class (if you aren't already), then you can define your Person class (roughly) like so

public class Person {

    private double bmi;

    public void setBmi(double bmi) {
        this.bmi = bmi;
    }

    public String getCategory() {
        if (bmi >= 30) {
            return "OBESE";
        } else if (bmi >= 25) {
            return "OVERWEIGHT";
        } else if (bmi >= 18.5) {
            return "NORMAL";
        } else {
            return "UNDERWEIGHT";
        }
    }
}

Create a Person

Person cricket_007 = new Person();
cricket_007.setBmi(20); // Maybe this is right? :)

Print it

System.out.println(cricket_007.getCategory()); // NORMAL
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
1

In order to return the category, the method needs to know the BMI. If you call the first method before the second method, then you have provided the BMI. But if you call the second method first, it has no way of knowing.

In general, it is considered to be bad form to have get methods that change an object's state. You might consider having a setBMI(double bmi) method which stores the BMI, and possibly calculates the category at the same time. After calling this, then the getCategory() method with no arguments would have something to work with.

Ken Clubok
  • 1,238
  • 6
  • 11
0

Your Person class should have a field called bmi

So, the objects andrewsInfo, boydsInfo etc will contains their distinct values.

Then in the Person class you can have the method

public String getCategory() {
    if (this.bmi <= 18.5) {
        category = "UNDERWEIGHT";
    } else if ((this.bmi > 18.5) && (this.bmi <= 25)) {
        category = "NORMAL WEIGHT";
    } else if ((this.bmi > 25) && (this.bmi <= 30)) {
        category = "OVERWEIGHT";
    } else if (this.bmi > 30) {
        category = "OBESE";
    }

    return category;
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
-2

use this snippet in your code instead of

public String getCategory()
   {
       return category;
   }

modified code--->

public String getCategory() {
this(bmi);
}
Akhil
  • 78
  • 12