Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?

Nicolas Hampton
Nicolas Hampton
44,626 Points

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

1 Answer

Nicolas Hampton
Nicolas Hampton
44,626 Points

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