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 trialBrandyn lee murtagh
432 PointsHarnessing the power of objects challenge
So, could anyone walk me through the question :
Finally, let's add a method named charge. It should be public and return nothing. When called it should set mBarsCount to the value of the constant you declared in Task 1.
Please? I only need guidance on the last part of the question.
public class GoKart {
private String mColor;
public static final int MAX_BAT = 8;
private int mBarsCount;
public int charge;
public GoKart(String color) {
mColor = color;
mBarsCount = 0;
}
public String getColor() {
return mColor;
}
}
2 Answers
Mikael Enarsson
7,056 PointsThe question is asking you to make a method called charge, but you have made a field.
The difference is that a field is something that holds a value, for example:
private String mColor;
private int mBarsCount;
public static final int MAX_CHARGE = 8;
are all fields, holding a String, an int and an int respectively.
A method, on the other hand, is a behavior, that is, a piece of code that allows you to see or manipulate data, such as:
public String getColor() { //this is the method name.
//You call the method using [object name].[method name]()
//for non-static methods
return mColor; //this methods returns the value of mColor
}
and
public void setColor(String color) {
mColor = color; //this method sets the value of mColor to the provided value
}
get and set methods are especially important for encapsulation, I find.
I hope that this is enough information to help you through, but if you have any other questions, don't hesitate to ask ^^
Craig Dennis
Treehouse TeacherYour charge
method has an uppercase C. And you are missing a semi-colon. Other than that, looks good!
Brandyn lee murtagh
432 Pointshey thanks for your reply.. still doesn't work though and I don't know why :/
UPDATE: I have done it! I accidentally deleted a '}'
Mikael Enarsson
7,056 PointsI just tried it and passed. Could you post your current code?
Brandyn lee murtagh
432 PointsBrandyn lee murtagh
432 PointsHi mate i've been stuck on this for ages and its still really annoying me.. I understand a lot more due to that response but I still cant seem to get it!
This is my code so far:
Mikael Enarsson
7,056 PointsMikael Enarsson
7,056 PointsOh, sorry, I didn't see that you had updated the comment with the code! It's as Craig says, otherwise everything looks good!