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 Changing State

Joakim Sjöstedt
Joakim Sjöstedt
1,975 Points

How do I get the GoKart filed and method to work?

The challange told me to declare a private variable named barCount, so I tried:

private int barCount=0;

and

private int barCount;

Treehouse said OK! on with the next challenge: Write a method that sets the barCount to MAX_BARS

so I tried:

public void charge(){ barCount = MAX_BARS }

as well as:

public void charge(){ do ( barCount ++ ) while (barCount < MAX_BARS)
}

Treehouse now tells me that the first task I did no longer works. Why? Any ideas?

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

  public void charge(){
  do (
      barCount ++
    ) while (barCount < MAX_BARS)    
    }

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

  public String getColor() {
    return color;
  }
}
Chaltu Oli
Chaltu Oli
4,915 Points

I think you have to subtract barCount from MAX_BARS

2 Answers

andren
andren
28,558 Points

The "Task 1 is no longer passing" message usually comes up because there is a problem in your code that causes the code checker to crash while evaluation your code.

In your case both of the solutions you came up while are correct logic wise, but they both have at least one code error within them.

Your first solution:

public void charge(){
    barCount = MAX_BARS // <- You are missing a semicolon ;
}

Is missing a semicolon to terminate the statement.

Your second solution:

public void charge() {
    do ( // <- The ( should be a {
        barCount ++ // <- You are missing a semicolon ;
    ) while (barCount < MAX_BARS) // <- The ) should be a } and you are missing a semicolon ;
}

Has three issues. You are meant to use curly braces {} for a do loop, not parenthesis (). You are also missing a semicolon after the barCount ++ line and the while statement.

If you fix the issues I outline above then either solution will work for passing the challenge. Though the first solution is the closest to the intended solution.

Joakim Sjöstedt
Joakim Sjöstedt
1,975 Points

Awesome, it worked! Got confused by it referring to task 1. Thanks for clearing that up! :)