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 Java Objects (Retired) Harnessing the Power of Objects Helper Methods

Can't figure out how to do "Create a helper method" challenge.

The method is at the bottom, and I have no clue what i'm doing wrong, as it seems to match what the video tells.

GoKart.java
public class GoKart {
  public static final int MAX_BARS = 8;
  private String mColor;
  private int mBarsCount;

  public GoKart(String color) {
    mColor = color;
    mBarsCount = 0;
  }

  public String getColor() {
    return mColor;
  }

  public void charge() {
    mBarsCount = MAX_BARS;
  }

  public Boolean isBatteryEmpty(){
    return mBarsCount == 0;
  }
}

2 Answers

Ryan Ruscett
Ryan Ruscett
23,309 Points

Hey,

So your use of == is not right.

What it is asking is for a method to return true if it needs to be charged.

Method isBatteryEmpty if (battery empty == 0) return true else return false

There is a shorter way but this is the easiest to follow.

public boolean isBatteryEmpty() {
    if (mBarsCount == 0) {
        return true;
    } else{ 
      return false;
    }
  }

The second part to this challenge the method looks almost identical. Except the if statment is comparing the bars to the CONSTANT value that is the max bars.

Let me know if this answers your question, feed back is appreciated.

Wow, I considered doing it like this, but thought that they wouldn't accept it done that way.

Do you know what he's talking about in the video with this?: return mBarsCount == 0;

Thanks for your help!

Ryan Ruscett
Ryan Ruscett
23,309 Points

Yeah, what you have will work. But you have a typo.

Boolean is not Boolean but boolean lowecase. So if you change that it will work too!

I would probably surround it in () but you don't have to.

return (mBarsCount == 0); OR return mBarsCount ==0;

You can do it either way, but visually this () tells me I evaluate for true or false and return it. Just a personal preference.

Ah, ok.

Thanks again, I think i'll start using that convention as well.