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

Code Challenge: Incrementing HALP

Hi guys! I am stuck on this challenge for a day now and have tried all the solutions I found in earlier questions, but they are not working for me. I even asked a developer friend for help and he gave me some code:

int factorial(n) { if (n == 1) return 1; // because 1! = 1 else return n * factorial(n-1); }

But it didn't work either.

I'm afraid I'm stuck in this pickle! Can someone please help me so I can continue with the lessons?

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() {
    boolean wasCharged = false;
          if (!isFullyCharged()) {
              mBarsCount--;
              wasCharged = true;
              }
    while (!isFullyCharged()) {
    mBarsCount++;
    }

 {
    }
    return mBarsCount == MAX_ENERGY_BARS;

  }

}

3 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Brie,

you are soooo close ...

In this challenge you don´t need to modificate the isFullyCharge method at all because this method is already perfect. This method returns true if the GoKart is full charged or false if not. Use it inside the charge method to proof the charge state of your GoKart.

Before you charge your phone ... you are asking yourself ... is my phone charged or not ? .... Do I need to charge it ? .... you can apply the same logic here.

Inside your charge method you can create a while loop. Inside the parenthesis you need a condition that is "true"

while (!isFullyCharged()) // while GoKart is not fully charged the condition is true (mBarsCount == MAX_ENERGY_BARS is not equal)

please increment the mBarCount

Look at this code:

public void charge() {
    while (!isFullyCharged()) { 
// while not fully charged please increment the mBarsCount
//
      mBarsCount++;
    }
  }

You can delete "mBarsCount = MAX_ENERGY_BARS" inside charge() because you set the mBarsCount if your GoKart is not FullyCharged :)

Let us know if you need more help and don´t give up

Grigorij

Hi Grigorjj!!

Thank you so much for your help! I appreciate it very much. Unfortunately I am still having trouble with this challenge. I keep getting an error, so I will keep trying.

Watch this space, lol.

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey Brie,

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

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

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

}

Grigorij

Thank you!! You are a good teacher. :)

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

You are very welcome :)

Hit Best Answer, so other can see that your question was answered.

Thx

Greg

Christiaan Quyn
Christiaan Quyn
14,706 Points

Hi I basically encountered the same problem .. but what I don't understand is why does the 'while' loop appear after 'public void'. Why do I place it there and why not anywhere else ?

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

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Christiaan,

you place it inside the charge method (inside {} of the charge () method), so when you call the charge method the while loop will be executed