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

Stuck at code :(

Hey guys,

I'm really trying to get rid of this challenge, but it keeps stucking! Sometimes it says my code took too long to run, sometimes just a simple try again... please somebody help me where I went wrong :(

GoKart.java
public class GoKart {
  public static final int MAX_ENERGY_BARS = 8;
  private String mColor;
  private static int mBarsCount;
  public static boolean isFullyCharged;
  public static boolean wasCharged;


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

  public String getColor() {
    return mColor;
  }


   public static boolean isFullyCharged() {
     boolean wasCharged = false;
    if (!isFullyCharged()) {
     mBarsCount++;
      wasCharged = true;
    }
    return wasCharged;
  }

  public void charge() {
    while(!GoKart.isFullyCharged){
    }
  }


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


}

3 Answers

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey there Krisztian.

You pretty much have the right idea, you're just working on the wrong method.

In this challenge they are wanting you to change the code inside your isFullyCharged method, go ahead and change it back to the below, which is what it was originally set to.

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

Now it's time to work on your charge method.

Within your charge method, and change it with your while and increment statement, so it should look like

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

Thanks!

Let me know if this doesn't work and I'll look further into it.

Thank you very much, this was the first what I tried but I forgot about the little things like the () after the isFullyCharged in the while loop....gotta practice more!! :D

Philip Enchin
seal-mask
.a{fill-rule:evenodd;}techdegree
Philip Enchin
Full Stack JavaScript Techdegree Student 24,726 Points

It looks like your charge() method has a loop that checks a condition that never changes. Inside the while loop you should have some code that will eventually make your condition (!GoKart.isFullyCharged) false. Right now, nothing takes place inside the loop to alter that condition, so the loop will run forever, and the Challenge engine will get impatient and cut out early.

Use the following snippet of code as your example to follow in changing the implementation details of the charge method:

public boolean dispense() {
    boolean wasDispensed = false;
    if (!isEmpty()) {
      mPezCount--;
      wasDispensed = true;
    }
    return wasDispensed;
  }
  • Instead of if, use while
  • increment, rather than decrement
  • The charge method doesn't need to return a boolean, so ignore all of the code involving the boolean wasDispensed

Keep isFullyCharged as it was from the previous challenge.

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