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

help

Please create a new GoKart object. As you know it takes a single parameter, color.

my code:

public class Example {

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

  public class GoKart {

private String mColor; }

}

}

http://teamtreehouse.com/library/java-objects/meet-objects/creating-new-objects

5 Answers

creating an object in java generally looks like this:

Classname objectName = new ClassName();

we need to create a GoKart object whose constructor takes a color as the parameter:

public class Example {

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

      GoKart goKart = new GoKart("red");


    }
}

Craig has an example of creating a new object using a constructor with an argument in this video at about 2:56

Challenge 2: code doesnt work

my code:

public class Example {

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

  GoKart goKart = new GoKart("red");
  System.out.printf("goKart,getColor");

}

}

http://teamtreehouse.com/library/java-objects/meet-objects/creating-new-objects

I've actually been having trouble solving this exercise as well. I have been trying to use the same format that Stone explained above, but perhaps I am doing something wrong. I have been trying to create a new GoKart object using this:

Example GoKart = new Example("color");

However, I get an error message that says:

./Example.java:6: error: constructor Example in class Example cannot be applied to given types; Example GoKart = new Example("color"); ^ required: no arguments found: String reason: actual and formal argument lists differ in length 1 error

I am not sure what I am doing wrong and I tried looking for the answer on StackOverflow but I didn't really understand. Could anyone help me/point me in the right direction?

Thanks so much in advance, Amin

the classname you want to use is the class of the object you are creating, not the class of the current file. since you are wanting to create a object from the GoKart class, you need to use GoKart, not Example:

GoKart goKartObject = new GoKart("color");

Thanks a lot Stone I really appreciate your help. I didn't think of this because I thought that I could only create an object from a class that has already been declared in the code (like Example had been). Is this not true or was GoKart already somewhere in the code just for this exercise?

Thanks again, Amin

you can assume that the GoKart class file exists for this excercise even though you cant see it. just pretend its there.

Yes in order to create an object a class file has to exist.

Lyric Abbott

Challenge 2: code doesnt work

my code:

public class Example {

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

GoKart goKart = new GoKart("red"); System.out.printf("goKart,getColor");

}

}

http://teamtreehouse.com/library/java-objects/meet-objects/creating-new-objects

If you want to print a variable/field you can't put it in "". If you want to call the method getColor you have to use a . instead of a ,

Does that help you?

doesnt work

If you watch the video at time index 3:25 it uses with the printf statement:

dispenser.getCharacterName()


//so I'm thinking the solution might be:

System.out.printf(goKart.getColor());