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

CHIRAG SARAOGI
CHIRAG SARAOGI
1,280 Points

I have put a while loop with mBarsCount++. The while is within the isFullyCharged. I do not know how to move ahead

Do we have to put the while loop outside the isFullyCharged function or inside. When we call the function we have to using an object created. I do not remember the name of the object that i created initially!!

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(!gokart.isFullyCharged())
    {
     mBarsCount++; 
    }
    return mBarsCount == MAX_ENERGY_BARS;
  }

}

1 Answer

Michael Hess
Michael Hess
24,512 Points

Hi Chirag,

The while loop goes inside the charge() method. The isFullyCharged() method is what's know as a helper method. A helper method is a method that helps another method to perform it's task. These are typically used when a method has to perform a complicated task that is composed of several smaller tasks. The smaller tasks are often performed by helper methods.

Please see the code below:

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() {
     // when battery is not fully charged mBarsCount is incremented by one bar
    while (!isFullyCharged()) {
        mBarsCount++;
      }
  }

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

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

  }

}

I hope this helps! If you have any questions I'll do my best to answer them.