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 Creating New Objects

Saurabh B
PLUS
Saurabh B
Courses Plus Student 2,880 Points

Print out using System.out.printf the color of the new object, using the getColor method. Please help me

My code is:

public class Example {

public static void main(String[] args) {
    System.out.println("We are going to create a GoKart");
   GoKart GooKart = new GoKart("blue");
  private String x = GooKart.getColor();
  System.out.println(x);
}

}

// What is wrong with this?

Example.java
public class Example {

    public static void main(String[] args) {
        System.out.println("We are going to create a GoKart");
    }
}
luis martinez
luis martinez
2,480 Points

hear is the answer change the goo-kart to color because is a little confusing.

public class Example {

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


   /* The way i got this is first i put the name after 
   the GoKart method that name is Color or what ever name you have. 
    later just put the .getColor method...... remember when you 
    use the getColor method put () after you use it.
    if you just do System.out.printf(Color.getColor); 
    it wont work. so remember to put () after the getColor method.  
       -------- sorry if I'm not good at explaining
      I'm just learning too.*/
}

}

3 Answers

Try this:

Example.java
public class Example {

    public static void main(String[] args) {
      GoKart mColor = new GoKart("red");
        System.out.println("We are going to create a new GoKart.");
      System.out.printf("The color of the GoKart is %s.", mColor.getColor());
    }
}
Saurabh B
Saurabh B
Courses Plus Student 2,880 Points

I understood all of your points but the way I declared string then passed the color value to it then printed it using system.out.println function. What is wrong with this way as we are only considered with the output. Is my systax wrong?

Saurabh B
PLUS
Saurabh B
Courses Plus Student 2,880 Points

I understood all of your points but the way I declared string then passed the color value to it then printed it using system.out.println function. What is wrong with this way as we are only considered with the output. Is my systax wrong?

sorry i didnt look over my answer well enough before hand. So you are close but your syntax is slghtly off. to create the object all we have to do is specify the object and its color

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

is What how i did it

Saurabh B
Saurabh B
Courses Plus Student 2,880 Points

I understood all of your points but the way I declared string then passed the color value to it then printed it using system.out.println function. What is wrong with this way as we are only considered with the output. Is my systax wrong?

Broderic Crowe
Broderic Crowe
1,549 Points

LOL. 45 minutes of syntax refusal cause I didn't add the the '( )' at the end of getColor. I thought I needed to make a getColor string, forgot its a method already.