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

Marc Cortez
Marc Cortez
2,977 Points

Am I supposed to add the "!" sign in a if-Statement or should it contain in the while-loop?

Please help! I'm stuck in this one :/

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 (!MAX_ENERGY_BARS) {
      mBarsCount++;
  }

}

3 Answers

L B
L B
28,323 Points

It wants you to use the isFullyCharged() method to charge.

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

Read as: While it is not fully charged { Increase the charge bars. }

Michael Hess
Michael Hess
24,512 Points

This line is not needed:

mBarsCount = MAX_ENERGY_BARS;
Michael Hess
Michael Hess
24,512 Points

Hi Marc Cortez,

In this exercise you'll want to use the while loop. The exclamation mark is a logical operator that means "not" in Java. The isFullyCharged() method is what's known as a helper method. A helper method is a method that helps another method to perform it's task. These are typically used when a method has to perform a complicated task that is composed of several smaller tasks.

Here is some code to give you a hint. While the bars are not fully charged we want to increment the bars by one bar:

while(!isFullyCharged()){

    }

If you have any further questions feel free to ask. Happy Coding!

Marc Cortez
Marc Cortez
2,977 Points

Thank you for the immediate help, guys! That helped me a lot :)