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 Incrementing GoKart Challenge; Missing return challenge, need help...

I need help with my GoKart challenge because I get the error message "missing return statement { “ I'm not really sure what to change ....

public class GoKart {
  public static final int MAX_ENERGY_BARS = 8;
  private String mColor;
  private int mBarsCount;

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

  public String getColor() {
    return mColor;
  }

  public void charge() {
    while(!isFullyCharged()){mBarsCount++;}
  }

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

  public boolean isFullyCharged() {
    boolean isFullyCharged= false;
    while(!this.isFullyCharged()) {
      mBarsCount++;
      return mBarsCount == MAX_ENERGY_BARS;
  }
  }

}

Error message Missing return statement { line 29

3 Answers

You just have add the while loop to the charge method. Like this:

public class GoKart {
  public static final int MAX_ENERGY_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_ENERGY_BARS;
    while (!isFullyCharged()) {
       mBarsCount++; 
    }
  }

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

  public boolean isFullyCharged() {
    return mBarsCount == MAX_ENERGY_BARS;
  }

}

Let me know if it works for you!

You should write it like this:

public boolean isFullyCharged() {
    boolean isFullyCharged= false;
    while(!this.isFullyCharged()) {
      mBarsCount++;
    }
return mBarsCount == MAX_ENERGY_BARS;
}

In your soultion you will always recieve the same result because after incrementing mBarsCount you always break the while loop with return statement. Second thing is that It is possibility that the program won't enter the loop and then there would be no return statement at the end of the method.

Regards

So I just tried it the way you suggested but it still isn't working ....

public class GoKart {
  public static final int MAX_ENERGY_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_ENERGY_BARS;
  }

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

  public boolean isFullyCharged() {
    boolean isFullyCharged= false;
    while(!this.isFullyCharged()) {
      mBarsCount++;
    }
    return mBarsCount == MAX_ENERGY_BARS;
  }
}

what am I doing wrong ?

Try this. I've change methods charge() and isFullyCharged().

public class GoKart {
  public static final int MAX_ENERGY_BARS = 8;
  private String mColor;
  private int mBarsCount;

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

  public String getColor() {
    return mColor;
  }

  public void charge() {
    while(!this.isFullyCharged()) {
      mBarsCount++;
    }
  }

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

  public boolean isFullyCharged() {
    if(mBarsCount < MAX_ENERGY_BARS){
      return false;
    } else {
      return true;
    }
  }
}

Thanks now it's working :)