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

Temitope Isaac Akintunde
Temitope Isaac Akintunde
556 Points

Please i am lost with the question after the lesson on Incrementing and Decrementing.

I don't seem to understand how to go about with the solution. 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() {
    while (!isFullyCharged){
      mBarsCount++;
    }
    return mBarsCount == MAX_ENERGY_BARS;
  }

}

2 Answers

Hi there,

For this part ofthe challenge, you are asked:

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.

So, we want to amend the exiting charge method. At the moment, this method just sets mBarsCount to be MAX_ENERGY_BARS. The clue to what the new method should do is given as: Let's use the ! symbol and a while loop. Inside the loop increment mBarsCount. So now we know why the idea is behind the amendment. We want to increment mBarsCount while the helper method, isFullyCharged() is reporting that that battery is not fully charged.

Let's consider the contents of the while loop. The clue says to increment mBarsCount, so let's start with that:

while (//something){
  mBarsCount++;
}

So far, so good! But what are we testing inside the loop condition? We want to know if the battery is NOT full. But we only have one helper method. That tells us when the battery is full - we want the opposite, right? That means we can negate the output of the isFullyCharged() method using the ! operator. We just need to insert that test into the while loop:

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

I hope that helps,

Steve.

Temitope Isaac Akintunde
Temitope Isaac Akintunde
556 Points

Thank you so much steve This is so very helpful and your detailed explanation gave me a better and broad understanding Thanks again

No problem! :+1: :smile:

Shreyas Ananth
Shreyas Ananth
861 Points

please elaborate your question... which part of code didnt you understand.. what are you trying to achieve?