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 trialWayne Greenwood
15,907 PointsJava meet the objects question
on the exercise 2/2 have we learned about getMethods and returns in order to complete the challenge?
4 Answers
Ken Alger
Treehouse TeacherWayne;
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:
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.
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:
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
Wayne Greenwood
15,907 PointsHi 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");
}
}
Wayne Greenwood
15,907 PointsHi Ken
Ahhh I see I was looking at the task wrong cheers for that it was most appreciated.
Ken Alger
Treehouse TeacherWayne;
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
Wayne Greenwood
15,907 PointsI will keep it in mind as I progress.
Cheers
Ken Alger
Treehouse TeacherKen Alger
Treehouse TeacherWayne;
Which challenge are you wording on as it is a bit unclear in your post.
Ken