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

Daniel Schwemmlein
Daniel Schwemmlein
4,960 Points

Stuck at the GoKart Java challenge

Hey there. I'm still stuck at this challenge where i was supposed to fill in a code that will prevent my goKart here from being over charged. Unfortunately for me I'm not able to do it. I even asked this question before but that answer did not help really

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

}

3 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Daniel;

Let's have a look:

Task 1

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.

It looks like you are working inside the isFullyCharged() method and not the charge() method like the challenge asks, but let's work through this as well.

We need our while loop, again I don't see that in your code, to see if our battery is fully charged, and if it is not we charge it, right?

Something like:

public void charge() {
    while (!isFullyCharged()) {

      mBarsCount++;
    }
  }

This will utilize the isFullyCharged() method, check to see if it is fully charged, and if not charge it until it is. Notice that we don't need to return anything with the charge() method, it just does it's magic and then lets the program go about it's merry way.

Please post back if you are still stuck.

Happy coding,

Ken

isFullyCharged am stuck there

Hi Daniel,

Have a look at a previous response I wrote in this to see if that helps.

https://teamtreehouse.com/forum/i-dont-understand-the-code-challenge-incrementing-and-decrementing

Cheers,

Steve.

Use the following snippet of code as your example to follow:

public boolean dispense() {
    boolean wasDispensed = false;
    if (!isEmpty()) {
      mPezCount--;
      wasDispensed = true;
    }
    return wasDispensed;
  }
  • Instead of if, use while
  • increment, rather than decrement
  • The charge method doesn't need to return a boolean, so ignore all of the code involving the boolean wasDispensed

Keep isFullyCharged as it was from the previous challenge.

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