Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
See how I solve the problem in this video!
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, did you figure it out?
0:00
Here's my solution.
0:02
First, we need to create
an array of good cow names.
0:04
So I'll start by creating a new
string array called names.
0:07
And setting it equal to
a few names that I like.
0:13
Remember, to create an array
literal we can use the brackets and
0:16
add the names in the middle.
0:20
I'll go with Larry,
0:22
Zelda, and Myrtle.
0:28
Awesome, next,
0:34
we need to create an array of cows that's
the same length as our names array.
0:36
So create an array of cows called cows and
0:41
set it equal to new Cow.
0:47
Then we need to pass in an argument for
how long the array is.
0:50
We could just type in 3 but
we can also type in names.length.
0:55
Next, we need to use a for
loop to populate our new cows array
1:02
with new cows using the names
from the names array.
1:08
Let's start by just creating a for loop,
1:13
for int i = 0, and we'll loop
while i is less than names.length.
1:19
We could also use 3 or
cows.length, if we wanted to.
1:27
Then we need to increment i by 1 and add
the body of our for loop with brackets.
1:34
Inside the for loop, we need to set cows
1:41
at position i equal to
a new Cow with the name
1:46
from the names array at the ith position,
perfect.
1:51
Finally, we want to use an enhanced for
1:58
loop or foreach loop to
print the name of each cow.
2:00
For Cow and we'll name it cow.
2:05
So a cow in the cows array, and
for each cow in the cows array,
2:10
let's print out the name of the cow.
2:17
So we'll do console.printf.
2:21
Then we'll use %s for our placeholder and
%n to add a new line.
2:25
Then we need to fill in our placeholder
with cow.getName to retrieve the name.
2:31
And semi colon to finish it off.
2:38
And last but not least,
we just need to run it.
2:39
Write a command to clear the workspace.
2:44
Compile our arrays.java.
2:49
And run it using the java command.
2:55
And there we go, we've got cows
named Larry, Zelda, and Myrtle.
3:03
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