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!

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

Need help with challange task. (Learning java)

The website is asking me to do the following:


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.


I dont know how i would do that, I have tried writing while(!isFullyCharged()) { charge(); } but that does not work...

And this is my code:

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

}

can someone help me understand what to do...

1 Answer

You were pretty close: Going off of memory I believe your charge method should look like this:

public void charge(){
   //mBarsCount = MAX_ENERGY_BARS;  This line should be commented out or deleted
   while(!isFullyCharged()){
      mBarsCount++;
   }

}

Thanks! i realized what I was doing wrong, I was originally placing my while loop as a separate code like this:

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

while (!isFullyCharged()) {
  mBarsCount++;                    <---------------------------------
}

}


but you helped me realize that i must place it under my charge code like this:

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

}

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

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

}


Thanks!