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

Haig Sakouyan
Haig Sakouyan
409 Points

Problems with the isBatteryEmpty exercise

Like many other exercises I'm stuck. Any help on how to handle the isBatteryEmpty true/false problem is greatly appreciated.

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() {
    if (mBarsCount >= 0);
    return true;
    else (mBarsCount < 1);
    return false;
  }

  public void charge() {
    mBarsCount = MAX_BARS;
  }
}

1 Answer

Hey Haig,

When you're making if, if/else, or if/else-if blocks, you should be using a { to open the block of code and a } to end the block of code. You don't use semicolons for any of these kinds of conditional statements.

Also, you have to remember to evaluate the logic of what you're proposing. mBarsCount represents the number of power left in the go kart battery and when it goes to 0, the battery is dead. The function you are creating isBatteryEmpty() should return true if the battery is empty and false if it is not. With that in mind, why would you want it to return true if it was above the 0/Empty mark? You should only want it to return true if the battery is dead i.e. if mBarsCount is 0. Else, you should return false, that it is not empty.

With all of the above taken into account, your code for the function should look very similar to this (as this passes the challenge):

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

Please do not just copy and paste the code but rather read over my text and think about what's happening here.