Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Practice Java Objects!
You have completed Practice Java Objects!
Preview
See how I solve the challenge!
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
All right.
0:00
How did you do?
0:00
Don't worry.
If everything didn't click right away.
0:02
That's totally normal.
0:04
And it certainly happened to me
when I was learning Java.
0:05
So here's how I solve the problem.
0:09
First, we created a new class named cow.
0:12
Then we need to add a closing bracket
down at the bottom.
0:17
Next we'll create a private
0:21
string variable called name.
0:23
Don't forget the semicolon at the end.
0:27
And to make sure
that we will never be able to change it.
0:29
Once it's been said,
we'll also make it final.
0:33
For step three,
we need to add a constructor
0:38
that takes in a string argument
0:41
and use this string argument
to populate the named variable.
0:43
So let's type public cow.
0:48
We'll add the string parameter name
and then the brackets.
0:51
And inside the body we'll set this dot
name to name.
0:55
In this case this dot name
refers to the name on our cow object.
1:01
A name here refers the name parameter.
1:06
You can use different names
if you like, but
1:09
this is a fairly normal way of doing it.
1:11
So step four we need to create a getter
to return the name of the cow.
1:14
This function will return a string,
so we'll start with a return value
1:20
and then the name of the function,
which would be
1:25
get followed by the name of
the variable, which is name.
1:28
Inside this
1:32
function,
all we need to do is return the name.
1:33
Then let's save the file
and move over to Main.java.
1:37
Over in Main.java let's start
by creating a new cow named Larry.
1:41
Cow Cow equals new Cow
1:47
And for the name argument,
I'll pass in the string of Larry.
1:51
For the last step, we just need
to print out the name of our cow object.
1:56
For this, I'll use system dot out
2:00
and use the printf function
to print a formatted string.
2:03
And while we could just print the name,
I'll go ahead and use a complete sentence.
2:08
The cow is named and we'll use percent s
for our placeholder.
2:13
Then add a period.
2:19
I'll also add percent end
to give us a new line in our console.
2:21
That way
it will be a little easier to read.
2:25
Then let's add a closing quotation mark
2:28
and for the parameter
to fill in our percent s,
2:31
let's use cow.getName
to retrieve the name of our cow.
2:34
Finally, we just need to run the program.
2:40
I'll start by clearing the screen.
2:43
Then you need to compile it
2:45
and use the Java command to run it.
2:48
And there we go.
2:53
The cow is named Larry.
2:54
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up