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

kalyada leosrisook
PLUS
kalyada leosrisook
Courses Plus Student 17,855 Points

Could anyone help me get start on this task?

This is the task goal : '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've been stuck in this task for a while now. I do not know how to get started. Mainly because I don't understand the question. Could anyone help me to start this ?

Thank you :))

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

}

5 Answers

Blake Larson
Blake Larson
13,014 Points

Fernando.. You don't need the mBarsCount == 0; line. The helper method isFullyCharged() will check that variable for you which is what you are using in the while loop.

public void charge() {

while(!isFullyCharged()) {  ///  This loop is saying, as long as when you call isFullyCharged() and mBarsCount isn't full, enter the loop and increment.  When it increments it will check the while statement again, and so on.  When you increment it to it's full charge this loop will stop.
   mBarsCount++;
}
}
Blake Larson
Blake Larson
13,014 Points

You are gonna want to use the "while (!isFullyCharged()) { increment }" in the charge method.

Chris Tvedt
Chris Tvedt
3,795 Points

The task is to make it so that when you charge the battery, it will charge until it is full. So it will iterate over the battery variable until it is at MAX_BARS, then it will quit the loop and stop charging. So:

while (is not fullyCharged()) { increment battery value; }

If the battery is then not == to MAX_BARS it will increment the battery value until the battery value == MAX_BARS.

Tried answering this without giving away all of the answer, please let me know if you got it working :-)

while(!isFullyCharged()){ mBarsCount++; } return mBarsCount == MAX_ENERGY_BARS;

I only have an error that's say tray again

Blake Larson
Blake Larson
13,014 Points

Fernando.. The method is void. There is nothing to return.

Public void charge (){ mBarsCount == 0; while(!isFullyCharged()){ mBarsCount++; } } This is your answer ;D

yes, Blake you are wright I wrote from what I remember from that exercise.