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

please just tell me what I'm doing wrong

please

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;
    while (!GoKart.isFullyCharged()) {
      System.out.println("It is charging");
    }
      if (GoKart.isFullyCharged) {
        System.out.println("It is done charging");
      }
  }


}

1 Answer

Christopher Augg
Christopher Augg
21,223 Points

Emily,

Very close. Good job! Let's go over the challenge and see where the issue is at. First, let us go through the instructions and make sure we know exactly what we need to do.

"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 like to always break the instructions down into smaller directions to get a better understanding. Maybe this will help others as well. The things that stand out to me are:

1) "...use our new isFullyCharged helper method" - This method was already declared. We do not need to change it. We only need to call it in our code? Where though?

2) "...change our implementation details of the charge method" - We will only need to change how the charge method works.

3) "...so it will only charge until the battery reports being fully charged." - need a loop inside the charge method for charging until mBarsCount is equal to the max amount.

4) "Let's use the ! symbol and a while loop. Inside the loop increment mBarsCount. " Confirms need for while loop, incrementing mBarsCount on each iteration of the loop, and tells us that we need to break loop when !isFullyCharged ( Is not fully charged).

Therefore, we will only need to change the implementation of the charge method. Inside the charge method, we will write a while loop that tests !isFullyCharged. Every time this test passes, we increment mBarsCount.

I recommend restarting the challenge and only changing how the charge method works:

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

I hope this helps.

Regards,

Chris

Lee Reynolds Jr.
Lee Reynolds Jr.
5,160 Points

Thank you for that. I wasn't properly breaking down the instructions and I was adding code to the wrong method and wondering why it wasn't working. Thank you so much for the clarification.