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

Jennifer Kartchner
Jennifer Kartchner
1,844 Points

I'm not sure what they're asking for here

Not sure how to do what they are asking

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() {
    return mBarsCount == MAX_ENERGY_BARS;
  }

}

They want you to change your charge method so that instead of it just instantly charging to full, it uses the isFullyCharged method to check if it is full and if it isn't then to increment the mBarsCount member variable.

Is this enough of a help? I don't want to give the answer away for you!

1 Answer

Hi Jennifer,

In this task, you have already got a charge() method and also another called isFullyCharged(). The charge method doesn't do much right now and that's what we're going to change. The isFullyCharged() method returns true or false, as its name implies.

So, what we want to do is to while mBarsCount is not fullyCharged, increment mBarsCount until it isFullyCharged.

That look like:

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

This takes the existing charge method and starts a while loop. This loop will continue while !isFullyCharged() - so, when isFullyCharged returns "false" (the battery is not fully charged) the loop will continue as the ! symbol negates the false to a true. The loop will continue running when the test is producing a true result. When the battery is fully charged, the isFullyCharged() method returns true, negted to false by the ! and the loop stops!

I hope that makes sense!

Steve.

Victor Croner
Victor Croner
818 Points

Steve thanks a lot I was nearly throwing my pc here dont knowing what to do... Thanks a lot.