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 Local Development Environments!
You have completed Local Development Environments!
Preview
Help me wrap up this program I've been working on. I'll provide you with a TODO list.
Project Files
- Open the TreeStory folder in the Downloads >> Project Files
Additional Information
- Writing JavaDoc
- I did a workshop on Regular Expressions in Java
- Java Tutorial on Regular Expressions
- Introduction to Git course to learn more about version control.
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
Another great thing that
IDEs provides is a way for
0:00
teams to communicate about the current
state of the code in a project.
0:02
There are built in version
conversion control tools that allow
0:06
you to use a program like Get that push
and pull changes to a shared repository.
0:08
So I've been working on that tree story
project that we started way back when, and
0:13
making more dynamic using more of
the tricks that we picked up throughout
0:17
our journey, and I'm really excited
to show you where it's heading.
0:20
I'm not quite done with it yet,
0:24
and I was wondering if maybe you
could help me wrap some of it up.
0:25
The really nice thing that
IntelliJ offers is the ability to
0:28
import an existing project.
0:31
As we haven't yet
0:34
covered version control in our adventures,
we'll just use a zip file.
0:34
Don't worry though, we'll get
there soon enough, and when we do,
0:38
we'll work together through
a shared repository.
0:41
Until then, though,
0:43
I've added a link in the teacher's
notes that I'd like you to download.
0:44
So, go ahead and download it, and
0:47
then extract the zip file
on your local machine.
0:49
Okay, so let's choose Open,
so we'll choose File, Open.
0:52
And then I'm gonna point to where
that extracted folder is at.
0:56
So, that's in my Downloads, over here.
1:01
And I opened my local TreeStory, I am just
going to click Choose and of course we're
1:03
gonna say, do we want to have another
window open and keep both of these open?
1:10
I don't think so, let's just do this one,
so we'll say this window.
1:13
Put it up, cool.
1:18
Here's everything, the project SDK is
not defined, so let's go ahead and
1:19
click Setup SDK.
1:23
Is the.
1:25
All right, there, everything is all set.
1:27
So I'm going to open
up the main over here.
1:31
This is where we're going to run through,
okay.
1:34
So, using the tools that
we've learned thus far,
1:40
I was able to get things pretty dynamic.
1:43
I'm not quite done yet.
1:45
So, let's take a look at where I got.
1:46
In Main, right here, if we do a little
walk through of where I've gotten to,
1:49
I haven't really written too much
of the prompting code at all.
1:52
But I've walked through the prototype and
it needs some finishing touches.
1:55
Maybe we can work together.
1:58
So, I put notes in here of
what I'd like to enhance and
2:00
I hard coded some things here in Main
that, hopefully, we could just unwind.
2:02
I just wanted to show how it was working.
2:06
This first thing here, we want to
instantiate a new prompter object and
2:08
prompt for the story.
2:11
That's a to do here.
2:14
The story template.
2:16
So you'll see here that the story
template is a string and
2:17
all the places where we
wanted to have spaces,
2:21
I put double underscores to kinda fill
in the blanks, because that's the, and
2:23
the text that's inside of it is
the word that we wanna ask the person.
2:27
So, it kinda looks like a fill
in the blank thing, right?
2:30
So, you just write the story this way.
2:32
And we'll prompt for those things, okay?
2:35
So, on the next line here,
we have this template.
2:38
And to the constructor,
we pass the story, okay.
2:39
And so, let's look over here,
2:42
let's just make sure that I know what this
template is and open up these imports.
2:43
Okay, we'll look over here in the source.
2:49
Okay here, so here's template.
2:52
So that's probably what
that's talking about.
2:53
So we'll dive deeper into
template here in a bit.
2:55
Okay, and after that I made some
very obviously fake results,
2:58
I named them fake results,
and let's put these on here.
3:01
Okay so, it says friend, talented,
java programmer, high five.
3:10
So I'm pretty sure that
must fill up the story.
3:17
Yep, very talented, I owe you a high five.
3:19
Cool.
3:22
And then we take this fake result and
3:25
we call the render method,
which we'll take a look at.
3:27
And it returns results and
we'll print out the results, there.
3:29
And it says this should really
happen in the prompt or run method.
3:33
I'll take a look at that here.
3:36
Fix a little typo,
see the little green thing there.
3:38
All right.
3:41
Now, since this was imported,
all of the configuration's already there.
3:43
So see,
there's already a main method here.
3:47
So let's go ahead and let's run this and
just make sure that it works
3:49
it's gonna take the fake data in the story
and says, thanks for helping me out.
3:52
Your are really a talented java
programmer and I owe you a high five.
3:55
Cool.
Pretty awesome, right?
3:59
So, if we just made it so
that you could prompt for the story and
4:00
then for each of the placeholders, we'd
have a super dynamic application, right?
4:04
So, you prompt for the story,
prompt for the placeholders.
4:08
Bam!
We got a tree story and
4:11
it can be as dynamic as possible.
4:13
Wouldn't be as hardcoded as we did in
the original version of this, right?
4:15
So I'm getting pulled
off the project though.
4:18
I got to go work on something else.
4:21
I did the best that I
could to document things.
4:22
And I also left some to
do's throughout the code.
4:25
I used the format here.
4:28
You'll see i used TODO:csd.
4:29
That's my initials.
4:31
This way you know that it's me talking,
4:33
I was kind of leaving notes of what I
thought was left that needed to get done.
4:36
Because I initialed it,
4:40
you know to come talk to me if
these don't make any sense, right?
4:41
So this is a great feature that lets
you track to do's on a project.
4:44
And I want to show you a couple of things,
actually.
4:47
If I press Cmd, and then hold it down,
you see how this bar down here showed up?
4:49
I'm going to do that again.
4:55
I press Cmd and
I hold it down and there it is.
4:56
You can open up these different windows so
I'm going to open up to do's here.
4:58
This is a list of all of the to
do's that are in the application.
5:02
And if you click on each one of
them it will go in highlight those.
5:05
Okay, so this is a great place to kinda
have gathered left what needs to be done
5:09
in a project.
5:13
This is a great way for
teams to communicate with each other.
5:14
And obviously you're on my team, and
5:17
here we are communicating about
what is left that needs to be done.
5:19
So another thing that I
did was I wrote Javadoc.
5:23
So if you highlight methods,
if we take a look at this render here and
5:25
you click F1 on it just
like any other method.
5:29
Click render, it gives you
the help that I actually wrote.
5:32
And it says, given a list of values,
replaces the fill in the blanks in order.
5:36
And the values are the replacements for
the fill in the blanks, and
5:40
it returns the filled out TreeStory.
5:42
Awesome.
5:45
So, I'm going to show
you something else too.
5:46
If you wanted to see where this
was defined you can press Cmd+B.
5:49
Or Ctrl+B.
5:54
And that's navigate to declaration.
5:56
So I could have chosen Navigate,
Declaration, Cmd+B.
5:58
So see the documentation
above that formatting?
6:05
It's called Javadoc.
6:09
It's a little out of
the scope of this course, but
6:10
I just wanted to show you that it works.
6:12
And you could actually
generate this using the IDE.
6:14
And, it will generate
websites using this notation.
6:17
So, actually the documentation that we
were looking at in, all of the Javadocs
6:20
whenever we jumped over to the official
Java documentation is using this format.
6:24
So as long as you follow this,
the IDE will then help.
6:27
I'll do a future workshop on this, and
6:31
I'll drop some information in
the teacher notes if you're interested.
6:33
And vote up the workshop
if you're interested in it.
6:35
Okay, so it's taking values
that are passed in array and
6:38
calling format, calling String.format
on a thing called mCompiled.
6:42
Okay, let's see what sets this.
6:49
So I'm going to click this and
I'm going to press Cmd+B.
6:51
There we go.
6:54
Okay, so it's a private string, and
I'm going to press Cmd+B one more time and
6:55
you can see this is where it's been used.
7:00
So, mCompiled equals, so there,
it's getting set, so let's look at that.
7:02
The line below is the one we did, but
this one here is where it gets set.
7:06
Ok sweet.
7:10
So where is this.
7:10
This is inside of
the template constructor.
7:12
Well let's go ahead and
lock this code in the debugger.
7:16
I'm going to drop a break point on the
first line here on this pattern line here
7:21
and I'm going to kick off the debugger
remember that was control D.
7:24
Cool awesome so we're here already.
7:31
If we look at the stack
we're over here in main.
7:33
And we're passed in the story.
7:37
Which is that right there.
7:39
The story that thing's named for
helping me out.
7:40
Okay so let's go back in to
the template where we're at.
7:43
You'll see that it says init here that's
the way that it's saying that it's inside
7:47
of the constructor.
7:50
The initializer.
7:51
All right, so to accomplish the finding of
the place holders, you know, the blanks?
7:54
I decided to use a regular expression.
7:59
Now you've seen the little
taste tests of these before,
8:02
we use them to split up a string and
you might have seen a workshop on them,
8:04
but if you haven't,
don't let them overwhelm you.
8:08
They're a bit hard to grasp at first.
8:11
It's normal to be a little bit confused,
okay.
8:13
So here we're seeing a pattern and
it looks like this.
8:16
Anything that has two underscores,
followed by any character that's not
8:20
two underscores, one or more of them,
and then another two underscores.
8:24
So we're looking for that, that is
the pattern that we're looking for.
8:30
We then use the pattern object to create
a regular expression in the helper class
8:33
called a matcher, which is here.
8:37
So let's go ahead and let step, so
8:39
now we have the text that's in here,
we've gone to the matcher, okay?
8:43
So let's step again, okay, so
8:48
it's got all of these tools down
here to keep track of things.
8:51
It also has a method called find,
which will find the next matching pattern.
8:54
Let's step into that loop a bit here,
press F8.
8:59
Now we've entered the while loop, and
we know that method of find was true,
9:02
because we're in the while loop.
9:06
We know now that we found a thing,
and there's a method named group, so
9:09
matcher.group is called.
9:14
So let's look at that a little bit deeper.
9:17
The way that grouping works in regular
expressions is that you can mark certain
9:19
parts that you want to extract.
9:23
So we wanna extract the name and
not the double underscore, so
9:25
that's what these parentheses are about.
9:28
We're saying this is
the group that we want.
9:30
We want the value inside of here.
9:32
Anything that is not two underscores.
9:34
So, the way that this works is
group zero is the whole thing.
9:37
Group one is the first set of parentheses.
9:41
If you wanted more, you do one and
then two and then so forth.
9:43
Let's go ahead and
use that little REPL tool.
9:47
We'll say Evaluate Expression.
9:50
And right here, if we say matcher.group(0).
9:52
When we evaluate it,
we'll see that it returns name, but
9:55
if we do group 1, it's just name, right?
9:58
So it's 0th is the first one,
1 is the first parens there.
10:03
So if that seemed intense.
10:08
It definitely is.
10:11
It most definitely is and
you shouldn't feel intimidated.
10:12
Make sure that you check the teacher's
notes to learn more about regular
10:15
expressions.
10:18
They're very powerful and
definitely worth exploring more.
10:18
Okay, so let's loop each one of these.
10:22
Okay All right, so
the next label [INAUDIBLE] is adjective.
10:27
What we're doing is we're populating
this mPlaceHolders, which is a list.
10:34
Right, so let's take a look and
place holders here is an array list.
10:38
We're populating it each time.
10:44
Oh, we're populating each
one of the placeholders.
10:48
That makes sense.
10:50
Cool, so let's step outside of that.
10:52
Let's break on this line here.
10:53
We'll move to the cursor.
10:57
Cool.
So we got placeholders.
11:00
We have all the placeholders that
are in there and look, there's doubles.
11:01
It's going to call a replaceAll
method on matcher,
11:04
which is going to replace all the previous
matches, all those double underscores.
11:08
So, let's do that, let's see what happens.
11:12
Let's take a look of what incompiled is.
11:14
Okay, it's the string format
that we've seen before, right?
11:17
So, it's the %s and it's just
waiting to have some values put in there.
11:19
So now let's go back
to that render method.
11:24
Here's that render method, and
let's drop a break point here.
11:28
And we'll just run to that.
11:32
Okay, so mCompiled is that, and
that values from the outside.
11:35
What do we got in the values here?
11:40
So there is values, and
11:43
they go right into those format spots,
so as long as we prompt for
11:44
what those placeholders were, we can just
stick those values that the people answer.
11:48
Great, so there we go.
11:52
That's how it's working, but
11:54
I'd love to integrate that prompter
object a little bit better.
11:55
Let's take a quick look
at the prompter object.
11:58
I am going to stop the debugger.
12:01
I'm gonna click this little
arrow here to minimize it.
12:03
Let's take a look at the prompter object.
12:08
All right so in the constructor,
let's get up to the constructor here and
12:14
in the constructor we're
building a reader all right,
12:19
then it calls this method
called loadCensoredWords.
12:23
Let's press Cmd+B to
see how that's defined.
12:27
Right below it, so there's that, cool.
12:29
So it uses the NIO style of files and does
a paths dot get resources censored_words.
12:31
So resources censored_words, okay.
12:37
So it opens up that file.
12:42
There's the censored words
that we remember from before.
12:45
Let's go back there really quick.
12:48
Cool, so it pulls them all out and
it pulls them into a set.
12:51
Right mCensoredWords here is a set,
a string set.
12:56
Cool.
13:02
That has that contains
method that we saw earlier,
13:03
so we could see if the word
actually exists in there.
13:05
All right.
13:08
Sorry to drop the project on you like
this but I really got something to do.
13:09
I have to go set it up.
13:11
I think I've given you all
the info that you need and
13:13
like I said there's to
do's left in these files.
13:15
So if you look in the prompter
there's two here.
13:17
So let's see let's print out the results
that were gathered by rendering
13:19
the template inside of the run method and
prompter that makes sense and
13:23
this other one let's see.
13:27
Okay, so prompt for word.
13:29
Prompt the user for the response to the
phrase to make sure the word is censored.
13:30
Loop until you get a good response.
13:34
Okay?
13:36
Cool.
So go ahead and you'll get a passed in
13:37
phrase and
all this stuff's documented here.
13:40
I really got to get running.
13:42
Sorry about that, talk to you soon.
13:44
I wanted you to experience something here.
13:47
Even though you might not have understood
exactly how I wrote this code,
13:49
you did understand what it was doing,
after stepping through it.
13:52
The better you get at this, the more
you'll be able to dive into projects and
13:56
find your way around.
13:59
This is an important skill to
hone as a software developer.
14:00
Many times you'll be thrust
into a code base and
14:04
you'll have to figure out
how things are working.
14:06
The more comfortable you start
feeling around exploring,
14:08
the quicker you'll pick things up.
14:11
Using the debugger to step through things
is a great way to learn what is up.
14:13
All right.
14:17
I know you can do this.
14:18
Have fun.
14:19
And remember if you get stuck just use
the debugger to walk through things.
14:19
Hit the forum too if something's
confusing, your team will help you out.
14:23
I've gotta set this up,
I'll see you right after the exercise.
14:27
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