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

Carlos Quevedo
Carlos Quevedo
5,911 Points

Where to put the do/while loop?

I'm kind of stumped on where to put the do/while loop in this exercise. In the tutorial, we put it in Example.java but here, it wants me to write it in the class? This is the last half of what I have written so far and it's returning syntax errors (failure clap! yay! <--- not sarcasm!). Sorry for the messy code formatting:

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 boolean chargeKart() {
    boolean wasCharged = true;
    if (!isFullyCharged()) {
        mBarsCount++);

    while wasCharged = false;
    }
    return wasCharged;
    }

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

}

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

In task 1, you're asked to make the charge() method only charge while the GoKart is "not fully charged". The while loop goes inside the charge() method.

  // Task 1 of 1
  // 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.

  public void charge() {
    // while the GoKart is not fully charged...
    while (!isFullyCharged()) {
      // increment the current charge
      mBarsCount++;
    }
  }
Carlos Quevedo
Carlos Quevedo
5,911 Points

Ah! I get it now. I thought I had to add a new method for a second. Didn't see that I had to replace the code within the '''charge()''' method with the loop. This makes much more sense. Thanks!

Hmm. It didn't work for me. Instead, it gave me 8 errors:

./GoKart.java:25: error: class, interface, or enum expected
  public boolean isBatteryEmpty() {
         ^
./GoKart.java:27: error: class, interface, or enum expected
  }
  ^
./GoKart.java:29: error: class, interface, or enum expected
  public boolean isFullyCharged() {
         ^
./GoKart.java:31: error: class, interface, or enum expected
  }
  ^
JavaTester.java:81: error: cannot find symbol
if (!kart.isBatteryEmpty() || kart.isFullyCharged()) {
         ^
  symbol:   method isBatteryEmpty()
  location: variable kart of type GoKart
JavaTester.java:81: error: cannot find symbol
if (!kart.isBatteryEmpty() || kart.isFullyCharged()) {
                                  ^
  symbol:   method isFullyCharged()
  location: variable kart of type GoKart
JavaTester.java:86: error: cannot find symbol
if (!kart.isFullyCharged()) {
         ^
  symbol:   method isFullyCharged()
  location: variable kart of type GoKart
./GoKart.java:17: error: cannot find symbol
    while (!isFullyCharged()) {
            ^
  symbol:   method isFullyCharged()
  location: class GoKart
8 errors

Ack.