Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Java meet the objects question

on the exercise 2/2 have we learned about getMethods and returns in order to complete the challenge?

Ken Alger
Ken Alger
Treehouse Teacher

Wayne;

Which challenge are you wording on as it is a bit unclear in your post.

Ken

4 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Wayne;

Let's break this entire challenge down as I think that helps see where we need to be for Task 2.

Task 1

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

Prompting code:

Task1PromptingCode.java
public class Example {

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

First we need to create a new GoKart object, which would follow the convention of:

className any_name_for_object = new className();

Our new goKart object takes a parameter of color, so let's make our new GoKart red.

Task1Solution.java
public class Example {

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

Task 2

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

To access the object's color we would use goKart.getColor();. If you put that inside the print code like:

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

It will print the color we passed to it during the object creation. Let's say that we create another GoKart object, fastGoKart and make it Blue. We could get that color by fastGoKart.getColor(). Make sense?

The code for the Task 2 challenge then looks like:

Task2Solution.java
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());
    }        
}

Post back if you are still stuck.

Ken

Hi Ken the question was.

Challenge Task 2 of 2

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

public class Example {

    public static void main(String[] args) {

     GoKart gokart = new GoKart("color");
        System.out.println("We are going to create a GoKart");
    }
}

Hi Ken

Ahhh I see I was looking at the task wrong cheers for that it was most appreciated.

Ken Alger
Ken Alger
Treehouse Teacher

Wayne;

One thing to consider with this is that you could put the object creation before the printing of the data and everything would appear to be the same from a user's standpoint. It would probably even pass the challenge. However, from a readability standpoint, I like the code to read... We are going to make an object... create the object... tell about the object.

Just a thought.

Ken

I will keep it in mind as I progress.

Cheers