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

Andrew Seager
seal-mask
.a{fill-rule:evenodd;}techdegree
Andrew Seager
Full Stack JavaScript Techdegree Student 1,308 Points

java incrementing exercise, I am not sure how to do the while loop they are asking for in the exercise.

in the java incrementing excercise they are asking for a while loop, the wording of the exercise is throwing me off, how do i do what they are asking for?

What exercise are you referring to? What does the code look like? What lesson are you on in which course?

1 Answer

Ok, so the wording through me off for a second as well. What it's saying is that we already made a method for determining if the GoKart is fully charged, the isFullyCharged method, so instead of rewriting that code, we want to keep our code DRY, or Don't Repeat Ourselves. The way we can do this is by using a method we've written in the class inside a new method we're writing for the class, the charge method. so we'll write a while loop that uses the isFullyCharged test instead of automatically filling it up to the max. Why, you ask? Well, maybe we wanted to make a representation of the time it takes to charge the GoKart, in order to make our objects behave more like they do in real life. For the sake of demonstration, I'll do this to purpose this could have down the road. What they want want you to do is this:

public boolean isFullyCharged() {

    return mBarsCount == MAX_ENERGY_BARS;
  }

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

this seems pointless when we can just set the value to the MAX_ENERGY_BARS, like it is at the beginning, but if we do this:

public boolean isFullyCharged() {

    return mBarsCount == MAX_ENERGY_BARS;
  }

   public void charge() {
    while(isFullyCharged()) {
      mBarsCount++;
      System.out.println("Charging...");
      Thread.sleep(1000);
    }
  }

We can make the output slow down and mimic the actual action of charging a battery. The Thread.sleep() method, by the way, is more advanced than this exercise and allows us to delay the program, but it throws an InterruptedException error. Handling error messages is something you haven't gotten to I think, so don't worry about it yet, but that's a real world application of why this question is phrased the way it is. Remember, keep the code DRY, don't repeat code you've already written, and may your methods small enough in scope that they can be used to make other methods. I hope this helped you understand a little of the why is this exercise. Happy coding!

Nicolas