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 trialJohn Westlen
Courses Plus Student 1,611 PointsWhat do i do?
I don't understand this part, this guy just runs code without explaining much in great detail,]
public class GoKart {
private String mColor = "red";
public String getColor() {
return mColor;
}
public (String Color){
mColor=Color;
}
}
Oshedhe Munasinghe
8,108 Pointsaha I think I understand your question. You don't understand about this code right? OK let me explain you with little bit Fantasy. Imagine YOU wanna make a GoKart and it has to be red.
OK you go to java and create a file GoKart.java In that file it was written " public class Gokart {} " - That means you have created blueprint, invisible GoKart. Perfect!
So you put so called Instans Variable " private String mColor = "red"; " mColor is a variable, remember that. Always.
Perfect, but you not finished yet because that Car is still invisible and java doesnt see that car is red because you just put variable String to red.. so what shall you do? Well you have to make "body" right? and thats call method. In java it has get/set method. think like this get - method " GET THAT (METHOD) IN RED" set method - "SET THAT DAMN (METHOD) IN INT 200" :D
Alright so you made a getColor and inside mColor, and what is it returning? YES red! :D
Well done :D
ok, You might think "what the heck, how can he call GoKart to get that color in GoKart.java?! I cant see that in compiler stupid" well calm down, first of all pass this quiz and then soon you will know how to call that method ;)
Don't give up. The more you fail in java the more expert you will be.
I hope you understand I know bad English grammar.. :P I wish teamtree has English course LOL :v
1 Answer
Kevin Frostad
3,483 Points"Add a public constructor to the GoKart.java class that allows the parameter color to be passed in. In the constructor store the argument color in the private color field."
A constructor is the kind of a method that gets called on when ever you make an instance that class, a new object of GoKart. Think of the construcor as the initialization method of the class. So if you which to launch a method or edit some instance variables/class field objects, at initialization, just throw it in the constructor. As there is no need for a constructor to return a type (like strin, int etc..), or have an access type (public, private, etc..), it doesn't have those. The constructor is defined by the class name only, in this case GoKart(){}. You can also give the constructor parameters, like this:
GoKart(String strPara, int intPara)
{
classVariableStr = strPara;
classVariableInt = intPara;
methodGoKartDrive();
}
So if the private classVariableStr field was null, it now contains strPala. strPala is what ever you made it to be when you made a new object of the class. A parameter variable needs input, so you could give it what ever String you want.
Oshedhe Munasinghe
8,108 PointsOshedhe Munasinghe
8,108 Pointsmay I know the question then I can help you out!