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
- Click here to download the TreeStory folder.
Additional Information
- Writing JavaDoc
- 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 IDs provide
is a way for teams to communicate
0:00
about the current state
of the code in a project.
0:04
There are built-in version control tools
that allow you to use a program like Git
0:08
to push and pull changes
to a shared repository.
0:12
So I've been working on that tree
story project we started way back when,
0:16
making it more dynamic using the tricks
we picked up throughout our journey.
0:21
And I'm really excited
to show you where it's heading.
0:25
I'm not quite done with it yet,
and I was wondering
0:28
if maybe you can help me wrap it up?
0:31
I've included
0:34
the Tree Story project in the zip file
we worked with earlier in the course.
0:35
I've linked to it in the teacher's notes
if you need to download it again.
0:39
Just like before, I've downloaded that zip
file and unzipped it.
0:43
Since we'll be working with the Tree Story
project this time, I'll open the Tree
0:47
Story folder and unzip the Tree Story 2
zip file.
0:51
Awesome.
0:55
There's the source code
for our TreeStory project.
0:56
All right.
0:59
Let's get the project imported
into IntelliJ.
1:00
Choose File, then Open.
1:03
I'll point to where I extracted
the folder, which is in Downloads,
1:06
then the Local Development Environment
folder, TreeStory
1:11
folder, and lastly, the TreeStory2 folder.
1:14
Then click Open.
1:18
Let's just use this window.
1:20
All right,
I'm getting an IntelliJ Security pop-up
1:23
asking if I want to trust and open
Project TreeStory.
1:27
As I know this project is safe, I'll check
1:31
the Trust
All Projects in TreeStory folder box.
1:33
Then click the Trust TreeStory
folder button.
1:37
Now everything is set up.
1:41
Let's open up the main file.
1:43
This is where we'll run things.
1:45
Using the tool we've learned so far,
I was able to get things
1:47
pretty dynamic,
but not quite finished yet.
1:50
Let's take a quick walkthrough
of where I got to.
1:54
In main, you'll see I haven't
really written much prompting code yet.
1:57
I built a statically coded prototype
that needs some finishing touches.
2:01
Maybe we can work together.
2:06
I added notes in the code
where I'd like enhancements.
2:08
Let's quickly
walk through some of the code and to-dos.
2:11
So the first to do is to instantiate
a new prompter
2:15
object and prompt for the story template.
2:19
The story template is a string
with double underscores wherever we want
2:22
users input. Inside the string, the text
2:27
between the underscores is the word
we want to ask for.
2:30
It's like a fill-in-the-blank style story.
2:34
It's just a static string at the moment,
but it would be great
2:38
if we could instead prompt
the user for the story template.
2:41
Next, we pass that
2:45
story template
into the template constructor.
2:47
I had generated some fake input results,
2:51
a static list of words
to fill in the story template blank,
2:53
friends, talented,
Java programmer, high five.
2:57
These fake results are used to render
the final story output.
3:01
Ideally, this should really happen
in the prompt.run method,
3:06
so I have also added a to-do for that.
3:10
And then we take the fake results
and pass them to the render method.
3:14
Lastly,
we print the final result of the story.
3:19
Since we imported the project
all the configurations is already there
3:23
There is a main method ready
as the entry point for
3:27
run Let run it and make sure it works
3:31
Great The output is correct
3:35
Pretty awesome right
3:38
It static right now though
It would be cool if instead we made it
3:40
so that you can prompt for the story,
then prompt for each placeholder.
3:44
We would have a fully dynamic tree
story app.
3:48
No more hard coding.
3:52
But I'm getting pulled off of the project
to work on something else.
3:54
I did my best to document things for you,
and I left to-dos throughout the code.
3:58
I use the format to-do-lc, as
those are my initials.
4:03
This way, you'll know it's me talking,
4:06
and you can come find me
if something doesn't make sense.
4:09
This is a great feature
for tracking work left to do on a project.
4:13
IntelliJ has a neat feature
to view all to-dos in project.
4:17
If you click view, tool windows,
4:22
then to do,
it will open up this to do tool window.
4:26
Here you'll see all the to do's listed.
4:30
You can click on any
to do to jump straight to it.
4:33
Pretty handy
for team communication, right?
4:37
Another thing
I did was write java.comments.
4:39
If you highlight
a method like render here on line 16,
4:43
then press F1,
it shows you documentation that I wrote.
4:47
Given a list of values
replaces the fill in the blanks in order.
4:52
And the values are the replacements
for the fill in the blank.
4:56
And it returns the filled out tree story.
5:00
Awesome.
5:03
Let's use the command B shortcut
we used earlier
5:04
to navigate
to the definition of the render method.
5:07
This jumps straight
to where the method is defined.
5:10
Great.
5:13
It brought us to the template.java file
where the render method is defined.
5:14
Neat, right?
5:19
Up here on lines 40 to 45 is why
5:20
I added the comments for the render
method in the Java doc format.
5:24
Java doc is the same format used
for the official
5:28
Java documentation you've seen online.
5:31
If you follow the format,
IntelliJ can even generate
5:35
full website style docs for your code
automatically.
5:38
It's a little out of the scope
for this course,
5:42
but I'll leave a link in the teacher's
notes if you're interested.
5:44
Okay,
5:48
so the random method is taking values
that are passed in an array
5:48
and calling string.format
on a thing called mCompiled.
5:53
Let's do the command B shortcut
on mCompiled so we can see what it is.
5:58
All right,
so mCompiled is a private string.
6:03
I'm going to press command B
one more time.
6:07
Okay, this time it shows us two places.
6:10
We had just come
from the instance on line 48,
6:13
so let's click
the first one to pop down to line 28.
6:16
Okay, it's being used inside
of the template constructor.
6:20
Let's go ahead and lock this code
in the debugger.
6:25
I'm going to drop a breakpoint
on the first line
6:28
of the code in the constructor, line 21.
6:31
I'm going to kick off the debugger,
which remember, that was control D.
6:34
If we look at the stack over here in main,
6:39
we passed in the story
to a new instance of the template class,
6:42
which is a story with our double
underscore surrounded word placeholders.
6:46
Let's go back to the template
where our breakpoint is set at.
6:51
All right So to accomplish
the finding of the placeholders
6:55
I decided to use a regular expression
So here we looking for a pattern
6:59
anything that has two underscores
7:04
followed by any character
that not two underscores
7:07
one or more of them
and then another two underscores.
7:11
We then use the pattern object
to create a regular expression
7:16
in the helper class called a matcher,
which is here.
7:20
Let's go ahead and use
step into in the debugger.
7:24
We have gone to the matcher.
7:28
Let's step again.
7:30
Okay, so it's got all of these tools
down here to keep track of things.
7:32
It also has a method called find,
which will find the next matching pattern.
7:37
Let's step into that loop
by clicking step into twice.
7:42
Now we've entered the while loop,
and we know that
7:46
the method of find was true
because we're in the while loop.
7:49
We now know that we found a pattern match.
7:54
And there is a method called group.
7:57
So matcher.group is called.
7:59
Let's look at that a little bit deeper.
8:02
The way that grouping works
in regular expressions
8:05
is that you can mark
certain parts that you want to extract.
8:07
So we want to extract the name
and not the double underscore.
8:11
That's what these parentheses are about.
8:15
We're saying this is a group that we want.
8:17
We want the value inside of here.
8:20
Anything that is not two underscores.
8:23
So the way that this works is group
0 is the whole thing.
8:26
Group 1 is the first set of parentheses.
8:31
Let's go ahead and use that
little REPL tool with option F8.
8:34
Let's evaluate matcher.group 0.
8:39
When we evaluate it,
we'll see that it returns
8:43
name with the double underscores
as a result.
8:46
If we do matcher.group1,
8:50
it's just the name value,
which is what we're after, right?
8:53
So zeroth is the first one,
and one is the first parence there.
8:57
So if that seems intense, it definitely
is, and you shouldn't feel intimidated.
9:02
Make sure that you check the teacher's
9:08
notes
to learn more about regular expressions.
9:09
They're very powerful
and definitely worth exploring more.
9:12
Let's use step over
9:17
by pressing F8
and go through the loops a few times.
9:18
So the first time through,
the label is set to name.
9:22
And the next time through,
9:26
it's set to adjective.
9:29
What we're doing is we're populating this
9:32
M placeholders, which is an array list.
9:34
We're populating in with the label
each time through the loop.
9:38
Nice.
9:42
It's showing 0 equals
name and 1 equals adjective so far.
9:43
Let's have the debugger complete
out the loop iteration for us.
9:49
We'll right-click on the word
matcher here on line 28,
9:53
then choose run to cursor.
9:56
Awesome.
9:59
Now mPlaceholders
has all four of our labels.
10:00
It's going to call a replaceAll
method on matcher,
10:05
which is going to replace all the matches,
all of those double underscores.
10:08
All right, let's step from here
10:14
so that we can see what happens after that
replace all is run.
10:15
Okay, cool, it's a string format
that we've seen before, right?
10:20
So it's the %s and it's just waiting
to have some value put in there.
10:24
Let remove that breakpoint
10:29
Now let go back down to the render
10:32
method Here that render method
and let drop a breakpoint
10:34
on the return statement
Then do a run to cursor for it.
10:39
Okay, so mCompiled
is the values from the outside.
10:45
Let's see what the values are.
10:50
Friend, talented,
Java programmer, high five.
10:51
So there are values,
and they go right into those format spots.
10:55
So as long as we prompted for what
those placeholders were,
11:00
we can just stick in those values
that the people wrote in.
11:04
Great, that's how it's working
currently with the static words,
11:07
but I'd love to integrate
that prompter object a little bit better.
11:12
First,
I'm gonna stop and close the debugger.
11:16
Then let's pop into the prompter.java file
and take a look at the constructor.
11:19
All right, so in the constructor,
we're building a reader.
11:25
then it calls this method called
loadCensoredWords.
11:28
Let's take a look at the loadCensoredWords
method here on line 22.
11:33
All right, it's using Path.get
to read the contents
11:37
of the censored_words
text file in the resource folder.
11:42
Let's see
what's in that censored_word file.
11:46
There's the censored words
that we remembered from before.
11:49
Okay, let's head back to the method.
11:53
Cool.
11:55
So it pulls the censored words
from the file
11:56
and puts them into a set
named mCensoredWords.
11:59
It's the mCensoredWords set that has
the contains method that we saw earlier.
12:04
So we could see if the words
actually exist in there.
12:09
All right.
12:13
Sorry to drop this project on
you like this, but I really have something
12:14
to do.
12:17
I think I've given you
all the info that you need.
12:18
And it's like I said,
there's to-dos left in the files.
12:20
So if you look in the prompter file,
there's two to-dos.
12:24
So let's see.
12:27
The first to-do here on line
44 is print out
12:28
the results that were gathered here
by rendering the template.
12:32
Okay.
12:35
All right.
12:36
So the second to-do is prompt
the user for the response to the phrase.
12:37
Make sure the word is censored.
12:41
Loop until you get a good response.
12:44
I really got to get running.
12:47
I know you can implement
these to-dos features.
12:49
I wanted you to experience something here.
12:52
Even though you might not have understood
exactly how I wrote this code,
12:55
you did understand what it was doing
after stepping through it.
13:00
The better you get at this,
the more you'll be able
13:05
to dive into projects
and find your way around.
13:07
This is an important skill to hone
as a software developer.
13:11
Many times
you'll be thrust into a code base
13:15
and you'll have to figure out
how things are working.
13:17
The more comfortable
you start feeling around
13:21
exploring, the quicker
you'll pick things up.
13:23
Using the debugger to step through
things is a great way to learn what's up.
13:27
Wow, another course under your belt.
13:33
You've got amazing new superpowers,
and knocking
13:36
that last challenge out really proved
how talented you are.
13:39
Now, like I said,
13:43
obviously, there's a lot more available
to you on your local machine
13:45
and many new tools in your IDE
that we didn't have time to cover.
13:49
If you'd like to see something more,
please make suggestions to help us guide
13:53
where you and your fellow classmates'
learnings are headed.
13:57
I've had a really great time hanging out,
and I hope you have too.
14:01
I'll see you soon.
14:05
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