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
Marcus Schumacher
16,616 PointsAnother Java Problem :(
Here is the prompt:
Now that you've created the GoKart class. Please create a new GoKart object using the GoKart class, or blueprint. As you know it takes a single parameter, color when being built.
Here is the code of mine that does not work:
public class Example {
public static void main(String[] args) {
System.out.println("We are going to create a GoKart");
public class GoKart {
public GoKart(String color){
}
}
}
}
And just for reference, here is the code when you reset it to the start of the challenge:
public class Example {
public static void main(String[] args) {
System.out.println("We are going to create a GoKart");
}
}
2 Answers
Matthew Francis
6,967 PointsHere is how you do it:
First off, you made a crucial mistake, the main method must be inside of a class.
class Main{
public static void main(String[] args) {}
}
To create a GoKart class with a different constructor, you need to do:
class Main{
public static void main(String[] args) {}
GoKart a = new GoKart("red");
}
Blake Larson
13,014 PointsSo you are gonna want to use the GoKart kart = new GoKart('red"); declaration. <--- This declaration in the main method will create a new object reference and save it to the variable kart. The "new" keyword is what triggers the constructor on your GoKart class and gives the GoKart it's state from the color passed in the parameter.
You can delete the
public class GoKart {
public GoKart(String color){
}
}
Blake Larson
13,014 PointsBlake Larson
13,014 PointsHe has main in class example.