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

Zak Hardy
Zak Hardy
1,077 Points

I really don't get one of the challenges.

Everything in the video seems to go straight through me and I don't understand it all.

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

1) How do I make an object? 2) ....wtf?

In Mo Kang
In Mo Kang
6,714 Points

To be honest, this lecture is quite bad, it does not explain things very well. the basic class was quite good though.

In Mo Kang
In Mo Kang
6,714 Points

To be honest, this lecture is quite bad, it does not explain things very well. the basic class was quite good though.

Zak I'm in complete agreeance, I felt like the lecture for this section didn't prepare us for this challenge at all and I feel lost.

6 Answers

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey there, so since the GoKart class has already been made for this for this object the code to create an object is as follows

Object objectName = new Object();

So in the case of wanting an new GoKart object we would call it

GoKart redKart = new GoKart("Red");

What this does is since the class GoKart is agian already defined we are first telling it that we are using the class GoKart, which is basically a blue print of the object, since we previously defined the GoKart class to take a String as a color, basically what this line of code is telling the compilier is that.

Use the class GoKart that I created earlier, I want to call the variable name redKart this is a new GoKart object that has the color of red.

Let's say we created class called soda that had the private member variable mBrand we would do something like this

public class Soda(String Brand) {
mBrand = Brand;
}

So we've successfully made our class, now to create an object of the soda class, such as Pepsi, we would do the following.

Soda pepsi = new Soda("Pepsi");

Thanks, let me know if this helps or not.

Zak Hardy
Zak Hardy
1,077 Points

Hi Rob,

Thanks for helping me out, I now know how to create an object, However, the next question asks me to print out the color using the .getColor method; although, I remeber watching it in the video I never really got it, could you possibly explain? or just point me in the right direction to know how to do it.

Thanks!

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey Zak,

No problem. So the next step would be the get color response. In the example above I called it redKart, however you may have called your kart something different.

In the event you called it red cart you'd use a statement as follows.

System.out.printf("The color of the kart is %s", redKart.getColor());

This prints a formated string, and in the place of %s, we pass it the value getColor() method on the kart that we created. So let's say you called your cart myKart. You'd edit this to.

System.out.printf("The color of the kart is %s", myKart.getColor());

Thanks, hope this helps as well!

Challenge Task 1 of 2 Now that you've created the GoKart class. Please create a new GoKart object using the GoKart class, or blueprint. As you know it takes a single parameter, color when being built.

//GoKart redKart = new GoKart("Red");//

and,

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

// System.out.printf("The color of the kart is %s", redKart.getColor());//

My Code:

    public class Example {
    public static void main(String[] args) {
    System.out.println("We are going to create a GoKart");
    GoKart redKart = new GoKart("Red");
    System.out.printf("The color of the kart is %s", redKart.getColor());}}

I echo Mao, this was a bad course it needs review. I still wish they'd give us the answer, it's a PITA to google for answers when the lesson isn't explaining things right.

Taebin You
Taebin You
4,786 Points

After a year, I guess the lecture stayed the same way