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 Incrementing

Okay, so let's use our new isFullyCharged helper method to change our implementation details of the charge method.

i'm getting the error message "reached end of file while parsing". how can i fix it?

GoKart.java
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() {
    while (!isFullyCharged()) {
      mBarsCount++;
}
  }

9 Answers

Andrew Brotherton
Andrew Brotherton
7,515 Points

I figured it out, the while loop should have been used in the charge method that we created earlier which would be actually charging the kart, then I should keep the return statement. I was looking it seems at the wrong method the entire time.

I think you are missing a curly brace at the end.

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++;
}
    mBarsCount = MAX_ENERGY_BARS;
 /*You must assign the value of bars count to MAX_ENERGY_BARS 
in order to prove that it is fully charged 
and exit the loop. This will check within the isFullyCharged() method 
and prove it to be true, 
thus the != true portion is false and you can 
exit your while loop. */
 }

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

  public boolean isFullyCharged() {
    return mBarsCount == MAX_ENERGY_BARS;
  }
}
Nicholes Gomez
Nicholes Gomez
4,135 Points

public boolean isBatteryEmpty() { return mBarsCount == 0; } public boolean isFullyCharged() { while (!isFullyCharged()) { mBarsCount++; _ }_ } }

You needed one more curly bracket where the underscores are.

Sorry couldn't figure out how to put it in the code block style.

Craig Dennis
Craig Dennis
Treehouse Teacher

If you do 3 backticks followed by the word java and then close with 3 backticks it will format properly.

Andrew Brotherton
Andrew Brotherton
7,515 Points

I'm getting still missing a return statement on mine?

Andrew Brotherton
Andrew Brotherton
7,515 Points

./GoKart.java:27: error: missing return statement } ^ 1 error

Andrew Brotherton
Andrew Brotherton
7,515 Points
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() {
    while(!isFullyCharged()) {
   mBarsCount++;
    }
    return mBarsCount == MAX_ENERGY_BARS;
  }

}
Andrew Brotherton
Andrew Brotherton
7,515 Points

This is what I have listed, I'm incrementing mBarsCount in the while loop, I'm returning mBarsCount equal to MAX_ENERGY_BARS, I'm implementing a while loop and I have all of my closing braces- what am I missing?

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

isFullyCharged is used to check if the battery is fully charged, it shouldn't actually charge the battery. That is the job of the method named charge. You should loop in the charge method checking to see if the battery is not isFullyCharged.

That make more sense?

Andrew Brotherton
Andrew Brotherton
7,515 Points
public boolean isFullyCharged() {
    while(mBarsCount != MAX_ENERGY_BARS()) {
   mBarsCount++;
    }
    return mBarsCount == MAX_ENERGY_BARS;
  }

}
Sean McKeown
Sean McKeown
23,267 Points

I think I just coded isFullyCharged like this:

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

Hope that helps