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

Dov Brodkin
Dov Brodkin
2,695 Points

While java loops

I do not understand what is wrong with this code, I get about 9 errors, does anyone know what is wrong?

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

}
Dov Brodkin
Dov Brodkin
2,695 Points

Does it automatically return the method?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Dov Brodkin the charge method calls/invokes/executes the isFullyCharged method. That method then returns a boolean that tells us whether or not it's fully charged. If it's not fully charged, our while loop will run and charge it :smiley:

Dov Brodkin
Dov Brodkin
2,695 Points

Thank you very much.

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I'm going to give a hint here, because by the looks of it you've got this pretty well under control otherwise. That while loop should not be in the isFullyCharged method. It's supposed to be in the charge method. We're first checking to see if it's fully charged. If it isn't, then we're going to charge it. Hope this helps! :sparkles:

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Simon Coates is correct. That while loop isn't even inside the isFullyCharged method. I didn't see the closed braces above it. In fact, it's sort of just floating around your class :smiley:

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Because the challenge explicitly says it needs to go in the charge method. Here are the relevant instructions.

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.

But currently, your code isn't in any method whatsoever. :smiley:

Dov Brodkin
Dov Brodkin
2,695 Points

When I write this: while (!isFullyCharged), do I need to write it like this : while(!isFulllyCharged())

Simon Coates
Simon Coates
28,694 Points

you need the () as the isFUllyCharged is a method call.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Your code is perfectly correct as it was. It's just misplaced. It simply wants you to change how the charge method functions. Take a look:

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

:sparkles:

Simon Coates
Simon Coates
28,694 Points

your while loop is outside the method body. And you should probably get rid of the second return statement inside the isFullyCharged method.

Dov Brodkin
Dov Brodkin
2,695 Points

Why does it need to be in the method body, why can't it be a new method.

Simon Coates
Simon Coates
28,694 Points

The second return statement? Unless i'm missing something it will never execute, and if it executed is recursive.