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

stuck on getColor method

Ive gotten past the first question on this code challenge, which is to create a new object. Now i am unable to use the getColor method.

Example.java
public class Example {

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

2 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Brendan;

Welcome to Treehouse!

You are on the right track, but let's break this down a bit.

You created the new GoKart object correctly, stylistically I would have placed it after the text stating that a new GoKart will be created and would have named it differently, but you certainly got that concept down. For the next task...

Task 2

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

After creating our new object, we need to print out it's color that we assigned to it when we created the GoKart. If you remember from previous code challenges, there is a getColor() method that we can use. We are able to access it with the syntax ourCartObjectName.getColor(). Using that knowledge, along with our command of the System.out.printf() function, we would do something along the lines of:

public class Example {

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

If you notice in the above code, I named my GoKart object after the color. This will help to keep track of the objects later on instead of things like firstKart, secondKart, etc. The above code also follows a certain logical flow of what is happening, right? It prints out that we will be creating a GoKart, then it creates one. It is my opinion that it makes it easier to read and later on debug if the code reflects what is actually occurring.

Post back if you are still stuck and happy coding.

Ken

Thank you so much for your help!!!

Haig Sakouyan
Haig Sakouyan
409 Points

This helped me as well. Thanks!