Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Andy Stevens
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Andy Stevens
Front End Web Development Techdegree Graduate 20,417 Points

Incorrect output returned.

Hi all,

I am playing around developing a fitness application, at present I am trying to return a users BMI. However, my output returns 0 and I'm not entirely sure where I'm going wrong, I think my constructor is correct and assume it's my getter that's letting me down?

BodyMass.java

public class BodyMass {
private int weight;
private int height;

public BodyMass(){
    this.weight = weight; // Weight in KG
    this.height = height; // Height in meter squared
}

public int getBodyMass(int weight, int height){
    height = height * 2;
    return weight/height;
}

}

FitnessApplication.java

public class FitnessApplication {

public static void main(String[] args) {
// write your code here
    BodyMass bmi = new BodyMass();
    System.out.println(bmi.getBodyMass(100, 180));
}
}

1 Answer

Steven Parker
Steven Parker
231,007 Points

It's just the formula and the arguments. If you pass in 100 and 180 for "weight" and "height", then the return will be 100 / 360 converted into an integer, which is 0.

And while unrelated to this issue, the constructor looks like it's intended to store arguments but it isn't declared to take any.

Andy Stevens
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Andy Stevens
Front End Web Development Techdegree Graduate 20,417 Points

Of course, the value returned is actually a floating point number. I assume if I change the getter to return a double or float this will solve my issue.

Iā€™m missing something regarding your comment about my constructor. What have I done wrong?

Steven Parker
Steven Parker
231,007 Points

It looks like you intended for the constructor to take arguments, otherwise it doesn't really do anything:

public BodyMass(int weight, int height){

Happy coding!