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
Daniel Silva
10,644 PointsNow create a method named charge that sets the new barCount field to the maximum amount of bars available for each GoKar
I don't know why the code isn't working....
class GoKart {
public static final int MAX_BARS = 8;
private String color;
private int barsCount;
public GoKart(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void charge(){
barsCount = MAX_BARS;
}
}
Moderator edited: markdown was added to the question to properly render the code.
5 Answers
Daniel Silva
10,644 PointsThe variable name is barCount. I had barsCount.....common error.
andren
28,558 PointsYou have two return types specified for the drive method (void and int), a method can only have one return type. Since the drive method does not return anything the return type should be void, so just remove int from the method declaration and your code should work.
Daniel Silva
10,644 PointsYes, I noticed that right after I posted the comment. I removed the "int" and it still isn't working...any other ideas?
Alex Bratkovskij
5,329 PointsHi Daniel,
Firstly your class doesnt have an access parameter, is it private, public, protected
Secondly there is no main method declared in your class. You have to declare it as well ;)
Hope that answered your questions and u managed to get it working, hit me up if you still struggling, I can pass you working code :)
-Alex
Donnie Turner
2,063 PointsHi,
I am also having a similar problem with the same exercise. below is my code.
Alex - the code is from an exercise, so the class access parameter was set this way by the creator of the exercise and I believe the main function of this class is GoKart which gets the color of the object. I have tried the method using both void and int but it still does not work. Any help would be appreciated.
class GoKart { public static final int MAX_BARS = 8; private String color; private int barCount = 0;
public GoKart(String color) { this.color = color; }
public String getColor() { return color; } }
public void Charge() { barCount = MAX_BARS; }
andren
28,558 PointsThere are two issues with your solution.
The first is that you have named the method Charge instead of charge, capitalization matters in programming, and in this instance the name should be all lowercase.
The second issue is that you have placed the charge method outside of the class' body. The class body is the area between the starting bracket { and ending bracket } of the class. The class' ending bracket comes right after the getColor method in your solution, so your charge method is just floating outside of it which is invalid.
If you correct the name and move it inside the class like this:
class GoKart {
public static final int MAX_BARS = 8;
private String color;
private int barCount = 0;
public GoKart(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void charge() {
barCount = MAX_BARS;
}
}
Then your code should work.
Prince Chinyadza
6,333 Pointsclass GoKart { public static final int MAX_BARS = 8; private String color; private int barCount; //note the field here
public GoKart(String color) { this.color = color; }
public String getColor() { return color; } }
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherHi there! Your markdown was very close to rendering the code correctly. Next time try putting a blank line between the backticks and the beginning of your code and blank line between the ending of your code and the closing backticks. Also note that there is a preview button in the lower right side so you can see if your markdown is being rendered properly!