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 (Retired) Meet Objects Constructors

Meet objects quiz - add a public constructor

Hi guys, back with another question. I am currently working through the quiz on adding a constructor. Below is my code.. can any one help? and also explain what is wrong with my code if possible.

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.

public class GoKart { private String mColor;

public GoKart(String Color) { mColor = Color; }

public String getColor() { return mColor; } }

Thanks

5 Answers

Well as for what I'm guessing you need to create a new object using the new keyword

So in your main method use

className any_name_for_object = new className();

the className() is the default constructor. But since you created one with a parameter it will become

className any_name_for_object = new className(String parameter);

This is just the skeleton, try replacing with the values and check if it works. If you get any errors let us know.

Ahh okay, I done it like this - GoKart gokart = new Gokart(); . With that being said do I just have to add the string parameter in the () ?

Exactly this way is the first way of doing it. Like I mentioned in the comment

Since you created a constructor with a parameter it should read

GoKart gokart = new Gokart("Yellow");

I've put yellow but it can be any string value

Remember the format should match

public GoKart(String Color) -- pay attention to String color, this is what you pass in.

Giancarlo M.
Giancarlo M.
1,502 Points

OMG, thank you so much! The skeleton really helped! I wish there more of them!

Go*Kart gokart = new Gok*art("red");

GoKart is spelt with a capital K on the left and with a small k on the right. :D ..

Be careful with typos in Java.

done it man.. thanks!

Great!

Everything seems fine.

Though I believe the question asks you to use color and not Color as a variable. I know it shouldn't make a difference but it does in these quizzes .

Also in the question its private private String mColor = "red";

Omitting that will also cause an error. Again these have nothing to do with actual programming outside these quizzes but to get through the quiz you will have to pay attention to the casing of variables

Hope this helps

Hi mate, thanks for the reply. Any chance you would be able to help in the second part? Please create a new GoKart object. As you know it takes a single parameter, color. ?

Ahh it still doesn't work.. my code is shown in the below: public class Example {

public static void main(String[] args) {
 GoKart gokart = new Gokart("red");
    System.out.println("We are going to create a GoKart");
}

}

Instead of writing "red" between parentheses, you should define new string as a "red" then write the string name between parentheses without using "".

For example, private String mColor = "red"; GoKart gokart = new GoKart(mColor);

public class Example {

public static void main(String[] args) { System.out.println("We are going to create a GoKart"); GoKart gokart = new GoKart("red"); } }