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

Michael LaCroix
Michael LaCroix
5,828 Points

Frustrated

First, I've gotta be honest. I'm really frustrated. This stuff doesn't feel very straight forward and I don't feel like this is anything like a beginner course. I already reviewed the Intro Java course once and I'm still not sure I've got a decent grasp of it. I read the directions for this coding challenge and I'm not even sure I know where to start. I have no idea if my code is anywhere close to being correct - I doubt it is.

Your time estimates for each course are laughable. 110 mins for the Java Objects course...I've been stumbling through it for 3 hours and I'm not even halfway done.

Am I going through this wrong or something?

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 (!MAX_ENGERGY_BARS) {
      mBarsCount++;
    }
  }



}

2 Answers

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hello,

we've unfortuately all been there, when I finished this course I felt like I had no grasp on it, so I started answering on the forums and using what I learned in my own project, once I had to use it again in the context of another project I was able to get it sorted out, so stick with it. It's a great feeling when it starts to stick with you as well.

Hope the below helps.

  1. )What needs to be done first is this while loop needs to be implemted in the charge() method, the isFullyCharged is there to use as a condition.

2 ) in your while loop check to make sure the conidtions isFullyCharged is not met, if it is not have them increment mBarsCount.

This is pretty much exactly what you did already, with a few adjustments, be sure to delete the line

mBarsCount = MAX_ENERGY_BARS;

In your charge method and replace it with

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

Like I said you were close, were just working on the wrong method.

Let me know if this helps, or if there's anything I didn't quite explain correctly and I'll do my best to get it sorted out.

Use the following snippet of code as your example to follow in changing the implementation details of the charge method:

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;
  }