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 Methods and Constants

Robert Walsh
Robert Walsh
802 Points

Stuck on this spot

Stuck on the last part of this challenge any help would be appreciated.

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

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

  public String getColor() {
    return mColor;
  } 
}

2 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Robert;

Let's take a look.

Task 3

Finally, let's add a method named charge. It should be public and return nothing. When called it should set mBarsCount to the value of the constant you declared in Task 1.

Wow, that's quite a bit. If we take it bit by bit perhaps it won't be so daunting.

1) Let's add (create) a method named charge that is public and returns nothing. Our basic method layout would be:

public void charge() {}

I find that a good place to put that method is below the getColor() method and before the closing curly bracket.

2) Great! Now we have created our method but it isn't doing anything. Our instructions are to set mBarsCount to our constant from Task 1. It looks like you are calling it BATTERY, so let's use that.

public void charge() {
   mBarsCount = BATTERY;
}

Hopefully that made some sense, if you are still lost on this please post back or watch the videos again as this is an important concept.

Happy coding,

Ken

Robert Walsh
Robert Walsh
802 Points

That made perfect sense thank you!

If someone isn't rewriting the code, and has the same issue,

public void charge() {
    mBardsCount = MAX_BARS;
}

it's MAX_BARS, not BATTERY. It was only "BATTERY" because OP didn't follow directions; which is fine, but it was confusing to me so posting to help since this was the correct answer-ish.

To understand how this challenge is connected to the lesson, you could rewatch the video, as suggested. Then, look at this code for your example to follow:

public class PezDispenser {
  public static final int MAX_PEZ = 12;
  private String mCharacterName;
  private int mPezCount;

  public PezDispenser(String characterName) {
    mCharacterName = characterName;
    mPezCount = 0;
  }

  public void load() {
    mPezCount = MAX_PEZ; 
  }

  public String getCharacterName() {
    return mCharacterName;
  }
}
  • Instead of MAX_PEZ, you are creating MAX_BARS
  • Instead of mPezCount, you are creating mBarsCount
  • Instead of a load method, you are creating a charge method