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 on the increment problem

In the video, we used two classes for the pez dispenser but with this problem, we only use one. I am having a difficult time figuring out where to put the while loop and how/where to put the boolean for incrementing isFullyCharged with this one class. Can anyone help me understand how to piece it together?

GoKart.java
public class GoKart {
  public static final int MAX_ENERGY_BARS = 8;
  private String mColor;
  private int mBarsCount;
  BatteryCharger charger = new BatteryCharger();

  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;
  }
  public boolean isCharging() {
    boolean isCharged = false;
    if (!isFullyCharged()) {
      mBarsCount++;
      isCharged = true
    }
    return isCharged;
  }
  while (charger.isCharging()) {
    System.out.println("Charging");
  }
  if (charger.isFullyCharged()) {
    System.out.println("Charged");
  }
}

1 Answer

Logan R
Logan R
22,989 Points

So in this challenge, we are mainly editing the charge() method. We are provided with:

public void charge() {
    mBarsCount = MAX_ENERGY_BARS;
}

Our job is to slowly increment mBarsCount until it is fully charged using a while loop. So first, we will empty the charge method and add a while statement:

public void charge() {
    while(bars is not full) {
        // Add one to the bar count
    }
}

We should run our while loop until mBarsCount is fully charged. We could do this:

public void charge() {
    while(mBarsCount != MAX_ENERGY_BARS) {
        // Add one to the bar count
    }
}

The problem is that we need to use our new method isFullyCharged. In the description, it says to use a !. When you take a boolean and you put a ! in front, it makes it the opposite. !True = false and !false = true. We want our while loop to run while NOT isFullyCharged, so we would do

So to put this all together:

```Java
public void charge() {
    while(!isFullyCharged()) {
        // Add one to mBarsCount
    }
}

This is the only method you should need to change. You should not need to create or edit any other methods.

Hope this helps you out some!

Awesome, thank you!

Logan R
Logan R
22,989 Points

Yep! No problem.