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

Lucas Alcoba
Lucas Alcoba
1,407 Points

Is this the best way to do this task?

The incrementing task was probably my most challenging one so far. in order to complete it i had to change both "mBarsCount" and "isFullyCharged" to static. i don't know if this was necessary or not. i'm just curious if there was a more simple way to complete this task. this was my code:

public class GoKart {
  public static final int MAX_ENERGY_BARS = 8;
  private String mColor;
  private static int mBarsCount;

  public GoKart(String color) {
    mColor = color;
    mBarsCount = 0;
  }

  public String getColor() {
    return mColor;
  }

  public void charge() {
     boolean isFullyCharged = false;
    if(!isFullyCharged()) {
    }
    while(!GoKart.isFullyCharged()){
      mBarsCount++;
    }
    mBarsCount = MAX_ENERGY_BARS;
  }

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

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

}

Like i said. i completed the task, but i don't think i did it correctly. let me know what i can do to make my code better! thanks alot :D

GoKart.java
public class GoKart {
  public static final int MAX_ENERGY_BARS = 8;
  private String mColor;
  private static int mBarsCount;

  public GoKart(String color) {
    mColor = color;
    mBarsCount = 0;
  }

  public String getColor() {
    return mColor;
  }

  public void charge() {
     boolean isFullyCharged = false;
    if(!isFullyCharged()) {
    }
    while(!GoKart.isFullyCharged()){
      mBarsCount++;
    }
    mBarsCount = MAX_ENERGY_BARS;
  }

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

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

}

2 Answers

You've fallen into the trap of overthinking things :) It's okay, I do it all the time.

Here's all you needed to do:

  public void charge() {
    while(!isFullyCharged()){
    //Notice we don't have to put the instance of a GoKart here...This is just checking if it's charged in general
      mBarsCount++;
    }
   //I also removed the old way of charging things, just setting the mBarsCount to max value.
  }

Let me know if that answered your question!

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,074 Points

From what I can see, you are not using the if statement inside of the charge "method" and you are not using the "boolean isFullyCharged = false;" inside of it either so you can "improve it" by omitting them. Like Shad said, you just need to increment the "mBarsCount" while is not fully charged like this:

while(!isFullyCharged()){
    mBarsCount++;
}

I hope that helps a little bit.