Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,307 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,307 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.