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

Sohaib Rashid
Sohaib Rashid
627 Points

I am lost and require assistance

If anyone can help walk me through this example i would highly appreciate it! The task is : Okay, so let's use our new isFullyCharged helper method to change our implementation details of the charge method. Let's make it so it will only charge until the battery reports being fully charged. Let's use the ! symbol and a while loop. Inside the loop increment mBarsCount.

Thank you!

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 (!isBatteryEmpty()) {
    mBarsCount ++;
   isFullyCharged = false;   
  }
    return mBarsCount == MAX_ENERGY_BARS;
  }


}

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Sohaib,

You're on the right track but have put your code in the wrong method, instead of putting it within isFullyCharged it should be inside charge as that's where our static code is.

See the below.

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

Happy coding!

Sohaib Rashid
Sohaib Rashid
627 Points

Oh i see my mistake now, thank you for your help! This may be a dumb question on my end but i am having trouble understanding the crucial role the static code plays, if you can help I would highly appreciate it! Happy coding!

Chris Shaw
Chris Shaw
26,676 Points

I'm no sure what you mean by static code plays, could you please elaborate.