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

Matt Kuzovkin
Matt Kuzovkin
1,653 Points

i can not handle this task, maybe you could give me a solution so i could learn. Thank you!

Here is what i have so far:

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 boolean isBatteryEmpty() { return mBarsCount == 0; }

public void charge() { mBarsCount = MAX_BARS; }

}

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 boolean isBatteryEmpty() {
   return mBarsCount == 0; 
  }

  public void charge() {
    mBarsCount = MAX_BARS;
  }



  isFullyCharged();
  if (MAX_BARS) {
    return isFullyCharged;
  }
}

1 Answer

Chase Marchione
Chase Marchione
155,055 Points

Hi Matt,

  • We'll need to create a new method called isFullyCharged(). It will return a boolean value, so I will designate boolean as the method's return type.
  • You're right to include MAX_BARS in your if statement, but what we want to do is check if mBarsCount is equal in value to MAX_BARS: this is how we would know if the bar count as at total/maximum capacity, which is when we want the return value to be true (since that would mean that the GoKart is fully charged.)

Here's how I just did it:

  public boolean isFullyCharged() {
    if (mBarsCount == MAX_BARS) {
      return true; 
    }
    else {
      return false; 
    }
  }

Hope this helps.