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

Abe Daniels
PLUS
Abe Daniels
Courses Plus Student 2,781 Points

Not sure what I'm doing wrong, more importantly, how to implement a "While loop" in this question.

It's telling me I've got 14 errors. No idea why :(

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;
  }
  if (GoKart.isFullyCharged()) {
    System.out.println("The battery is fully charged");
  }
  if (!GoKart.isFullyCharged()) {
    charge++; 
  }


}
Grigorij Schleifer
Grigorij Schleifer
10,365 Points

You donΒ΄t need mBarsCount in your GoKart() constructor method, because you define String color here.

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

And here try this:

public boolean isFullyCharged() {
  if (GoKart.isFullyCharged()) {
    System.out.println("The battery is fully charged");
  } else {
      GoKart.charge();
  }

And i deleted one curly bracket infront of the first if statement :)

And you need one more curly bracket after the second if statement to end the isFullyCharged() method

I hope something of my code can help ... i am a very beginner :)

1 Answer

Michael Hess
Michael Hess
24,512 Points

Hi Abe,

In this challenge we need to increment mBarsCount when the battery is not fully charged.

The code has to be modified in the charge() method. We use the ! symbol which means "not" and the isFullyCharged() method and a while statement. The while statement is saying, While the battery is not fully charged add one bar to the barsCount until the bars are full -- make sense?

Please see the following solution:

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;
  }

// add the while loop to the charge method
  public void charge() {
    //set mBarsCount equalt to MAX_ENERGY_BARS
     mBarsCount = MAX_ENERGY_BARS;
     // when battery is not fully charged mBarsCount is incremented by one bar
    while (!isFullyCharged()) {
        mBarsCount++;  //increments by 1 bar
       }
  }

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

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

  }

}

If you have any questions I'll be happy to answer them.