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 Harnessing the Power of Objects Computed Properties

Bill Rebello
Bill Rebello
467 Points

Computer Properties Exercise task 2 ?

Get error Task 1 is no longer passing? By answer is below" Whats wrong?

public boolean isFullyCharged(){ if (barCount == 8){ return true;

GoKart.java
class GoKart {
  public static final int MAX_BARS = 8;
  private String color;
  private int barCount;

  public GoKart(String color) {
    this.color = color;
  }

  public String getColor() {
    return color;
  }

  public void charge() {
    barCount = MAX_BARS;
  }

 public boolean isBatteryEmpty(){
  if ( barCount == 0){
    return true;
}
 public boolean isFullyCharged(){
   if (barCount == MAX_BARS){
     return true;
   }
 }
 }
}
Bill Rebello
Bill Rebello
467 Points

Thanks for you comment. I tried >>> Same error?? public boolean isFullyCharged(){ if (barCount == 8) { return true; }

2 Answers

Craig Fender
Craig Fender
7,605 Points

One issue I see is that the variable barCount may not be initialized when the method isFullyCharged runs, i.e. you forget to call charge() before you call isFullyCharged(). In that case, since the conditional is false, there is no return on a false bool value.

Instead of doing an if-then statement and returning a true or false based on that condition, just return the equality test to begin with, like:

public boolean isFullyCharged() {
     return barCount == MAX_BARS;
}
Vladut Astalos
Vladut Astalos
11,246 Points

You have an extra closing curly bracket at the end. Remove one and it should work, I think.