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 - Word Guessing Game!
You have completed Practice Java Objects - Word Guessing Game!
Preview
Let's use method overloading to accept a String as well as a char for our guess.
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
So we found a fatal
error when you don't enter a character.
0:00
Our code that attempts
to get the first character
0:04
out of the input blows up
because there's no character there.
0:06
We could fix this in the prompter class,
but other code using our game
0:10
class will likely need
the same functionality.
0:13
To prevent code duplication
and keep things dry, don't repeat
0:16
yourself, let's have our game accept
a string as well as a character.
0:20
Hey, why don't we use that
method signature trick
0:24
to allow our apply
guess to accept a string as well?
0:27
So we'll add a new method
with the same name,
0:32
apply guess, but it will expect a string
for its parameter.
0:34
So there will actually be two different
methods, one that takes a string
0:38
and one that takes a char.
0:41
This way, we are not only making our game
object more usable by other applications,
0:43
we are also handling
common problems in a single place.
0:48
Alright,
time to turn this bug into a feature.
0:52
Just kidding...
we're fixing it... let's go fix it.
0:55
Ok first, let's go to the game class
and create our overloaded apply
1:00
guess method that takes a string parameter.
1:03
So public boolean apply guess
1:09
and this time it's string
1:14
and we'll do letters, plural.
1:16
Now let's handle the bug
we saw when nothing is entered.
1:22
We're wanting to check
if there's nothing in this string, right?
1:25
So we can simply check
if the string's length equals 0.
1:28
If this is the case
and no letters were provided
1:36
we throw an IllegalArgumentException
with a clear message
1:38
So throw new IllegalArgumentException
1:42
No letter found.
1:49
Cool, so we should be covered on the empty
guess.
1:53
Now, after our conditional, we can safely
1:55
assume there's
at least one character in our string.
1:58
So we'll get the first character
and call our original ApplyGuess method.
2:01
This way, we reuse all the existing logic
here without duplicating code.
2:06
We can pass the result of charAt directly
without creating an extra variable.
2:11
Now in our
2:17
prompter class, we can simplify our code.
2:17
We'll remove the line
that extracts the character
2:20
and just pass the string directly to apply
guess.
2:22
We're now relying on the game
object's implementation
2:26
to handle the string properly.
2:29
This is much cleaner.
2:31
The character extraction code
we had here in prompter would likely
2:32
be duplicated everywhere
our game object was used.
2:36
Now we can just pass
in the string directly, whether it comes
2:39
from console input, web forms,
or anywhere else.
2:42
Let's test
to make sure everything still works.
2:45
First,
let's compile and run a normal guess.
2:48
Try T.
2:51
Good.
2:53
Now let's test
what happens when we enter nothing.
2:53
Perfect!
2:56
It uses the same exception handling
we had before.
2:57
This is done.
3:01
Booyah!
3:05
Alright, that's all fixed up now,
3:07
and we used method signatures
to keep our naming consistent.
3:09
We now support both strings and chars,
and no one will have to think about that
3:12
zero-length string problem again,
because we got them covered.
3:16
And hey, there's only one story left.
3:19
Let's wrap up this exercise
and then finish out our project.
3:22
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