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
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 do not enter a character our code
0:00
that attempts to get the first character
out of input actually blows up.
0:04
That's because there's
not a character there.
0:08
So we could just simply fix that code
there in the prompter class, but
0:10
I've got a hunch that there will be
other code that uses our game class that
0:14
might need to get the first
character of the string as well.
0:17
In an effort to help prevent
possible code duplication and
0:20
keep things dry, let's allow for
a probably common use case.
0:23
We should probably just have our game
accept a string as well as a character.
0:28
Hey, why don't we use that method
signature trick that we learned about
0:33
earlier to allow our apply guess
to accept a string as well.
0:36
So we'll add a new method,
with the same name, applyGuess.
0:41
But it will expect a String for
its parameter, so
0:45
there will actually be
two different methods.
0:48
One that takes a string,
and one that takes a char.
0:50
This way, we are not only making our game
object more usable by other applications,
0:53
but we are also handling common
problems in a single place.
0:58
All right let's squash this bug.
1:02
Okay, so first let's go to game and
1:07
let's make a new apply guess and
1:11
we will say it's a public Boolean
applyGuess same name except for
1:15
the signature String letters
that make sense right.
1:23
Okay, so
1:32
first let's take care of that bug that we
just saw if nothing is entered, right.
1:33
So strings so
1:37
if the letters.length is == to 0.
1:40
Then we better tell them about it,
so we'll,
1:47
throw new IllegalArgumentExceptions.
1:52
We all spell bad.
1:57
No letter found, cool.
1:59
And now we'll pull out our first char.
2:04
So first letter = letters.charAt 0 and
2:07
then we'll call our original method cuz
that takes a char right, and this way
2:14
we're getting all the logic from here and
we don't have to duplicate it here right.
2:20
So method overloading right.
2:24
So here we go.
2:27
So we'll say return, apply, guess.
2:27
First letter but
you know what come to think of it.
2:33
Let's go ahead and let's just pass
in the results of this method call
2:36
without even making a variable.
2:41
Why would we make a variable.
2:43
Here we go, beautiful.
2:44
Now the cool thing is
in our prompter object,
2:47
before we were doing that,
we could just leave guessInput as it is.
2:49
Let's get rid of this line, okay?
2:54
So guessInput is a string.
2:57
We don't need that character anymore and
we'll just switch this to use guessInput.
2:58
There we go.
3:02
And there is no grabbing
the char out of this.
3:03
We're relying on
the GameObject's implementation.
3:06
So much nicer, right?
3:09
That code that we had written there where
we were pulling out the first char would
3:10
probably be duplicated in every single
place our GameObject would ever be used,
3:13
that I can think of, right?
3:17
So even on a web page if it came through,
3:18
you'd have to get the string
that came from the web page and
3:20
you get the first character out of it,
but now we can just pass in this string.
3:23
It's kind of nice.
3:26
We've saved a ton of lines of code for
future devs.
3:27
But first, before we get too cocky,
let's make sure that it still works.
3:31
Clear Javac.
3:35
Cool, so let's make sure it works there,
it does.
3:45
What happens if we guess nothing?
3:47
Here it goes.
3:49
No letter found.
3:50
Please try again.
3:51
Awesome, and it used the same exception
loop that we were using before.
3:52
It works, I'm feeling pretty good.
3:55
Let's move this to done.
3:58
All right, that's all fixed up now.
4:02
And we use method signatures
to keep our naming consistent.
4:04
We now support both strings and chars.
4:07
And no one will have to think about
that zero length string problem again,
4:10
because we've got them covered.
4:13
Whoa, there's only one story left.
4:15
Let's wrap up this exercise and
then finish out our project.
4:17
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