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

Devin Scheu
Devin Scheu
66,191 Points

Help With Java

Question: 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.

My Code:

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

  while(!isFullyCharged()) {
    return GoKar.charge();
  }

  public void charge() {
    mBarsCount = MAX_ENERGY_BARS;
  }

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

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

}

4 Answers

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

Hello,

What they are asking for is that you change the charge method. Go ahead and remove the code inside that method, and add your own. Start while a while loop, it's syntax is while (condition) { do stuff; }

What you want to check for is if the goKart is fully charged, since we have a helper method to check if that is true, we can easily check for the opposite by adding an exclamation point in front of it in the while condition, so that while the goKart is not fully charged you will be able to charge it. In the body of the while loop you construct you want to make sure that you are adding to the mBarsCount if your while loop determined it was not fully charged.

I see that you have the while loop established to check to see if it's fully charge, move that into the charge() method and take out what is there. You now need to increment mBarsCount inside the while method.

Let me know if this helps.

Thanks. Also why do so many people just paste the answers?

Devin Scheu
Devin Scheu
66,191 Points

Some people do, It is what you take from the answers that matters. You can easily take the answer and pass the challenge, but taking a few minutes to understand the code they gave you to pass really helps.

It's a really hard temptation to avoid. Sometimes someone expresses how frustrated they are from having spent hours or days on a problem and so, providing an answer feels like the better thing to do. Now that I'm a mod, I'll update this thread, giving Rob the best answer and deleting my explicit answer.

Helping others to discover and learn for themselves is a skill that takes practice. Thanks for pointing this out Martyn.

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

Robert is right, forum answering is hard to do, I've been guilty of both giving an answer when they didn't want one, and not supplying one when they did. I try my best to determine what they would like based on their question, however it can be very hard to get right. Sometimes I give an answer and it was the right thing to do, and other times they would have preferred an explanation.

It's never going to be a situation where you get it right 100% and you have to usually settle for an awkward middle of providing the answer and also trying to assist and explain it.

ok, thanks for the reply. also which OS is better for programming in general? Sorry for the off-topic question.

Whichever OS you are most comfortable working in - aka, there is no 'best' OS for programming. Anything you can do in Linux, can be done in Mac, can be done in Windows - with perhaps some slight caveats and edge cases, but nothing to be concerned about when just learning to program.

I use Windows and have programmed in Linux as well.

Devin Scheu
Devin Scheu
66,191 Points

Linux is generally better, but it can be done on windows also. It is mostly based on which OS your feel comfortable navigating.

ok, but which linux should i go for as i like opensuse, fedora and i like manjaro?

Public class GoKark{private String mColor; public Static final int MAX_BARS=8; private int mBarsCount; public GoKart(string color){mColor=color; mBarsCount=0;} public void charge(){mBarsCount=MAX_BARS;} public string getColor(){return mColor;}

i dont wats wrong with my code

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

Hey there, was this question for this challenge? can you give me a link for what challenge you are trying to complete? If it's for this challenge looks like a few lines are missing in the code you supplied.

Thanks.