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

joshuarodulfo
PLUS
joshuarodulfo
Courses Plus Student 454 Points

i have no idea how to print out the color using "getcolor method"

can someone show me how to use "getcolor method" maybe explain it if you can please

Example.java
public class Example {

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

2 Answers

andi mitre
STAFF
andi mitre
Treehouse Guest Teacher

Hey Joshua, this question is a bit confusing in itself because you have to assume some code which is not shown. For example the question asks to use the getColor method. Although not part of this challenge you can find that code pasted below or visit the challenge right before the one in question. Please see below.

// this is the code from the previous challenge
public class GoKart {
  private String mColor = "red";

  public String getColor() {
    return mColor;
  }
}

// this is the code for the challenge in question:
public class Example {

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

        // the line below creates a new GoKart object in this case named 'gokart' and in your code named 'kartColor'
        GoKart gokart = new GoKart("GoKart");

       // the line below uses the newly created object to call the getColor function shown in the previous challenge
       gokart.getColor();
    }
}

Hope that helps!

Cheers

joshuarodulfo
joshuarodulfo
Courses Plus Student 454 Points

Yes! thanks for explaining this I was forgetting to add parenthesis. But you also cleared some questions I had

public class Example { // This is the example class, so the GoKart class is elsewhere.
    public static void main(String[] args) {
        System.out.println("We are going to create a GoKart");
      GoKart kartColor = new GoKart("blue"); // Here you've created a new "blue" GoKart.
         System.out.println(kartColor.getColor()); // So now that you have created a GoKart, called kartColor, simply
          // use the existing getColor method (which they say is in the GoKart
         // class). The getColor method returns a String, so if you use this method
         // on kartColor, it will tell you the color of the cart, "blue".
    }
}

That might be a bit confusing, i'm not amazing at explaining things. Please let me know if there's something you still don't understand.