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 Java Objects!
You have completed Java Objects!
Preview
We will set up the initial state of our Game logic here. We will perform the first half of the user story: "As a guesser, I should be able to submit a guess, so that I can play the game."
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
Okay, so let's take a stab at
the first story which is this.
0:00
As a guesser, I should be able to submit
a guess, so that I can play the game.
0:03
Sometimes these beginning
stories are a little silly.
0:09
Now obviously a guesser is gonna guess,
but it needs to be stated so
0:11
that development will happen.
0:15
Okay, so let's think about this a bit.
0:16
Our game object currently
holds what the answer is.
0:19
And now we want to start
having it store what
0:23
guesses have been made
towards that answer.
0:26
So this story is requiring that the game
allows the submission of a guess.
0:28
Sounds like we might need to create
a method that accepts a single letter.
0:34
How about applyGuess.
0:37
So with that guess, we should be
able to tell if that guess is a hit,
0:39
like it's in the answer, or
if it's a miss if it isn't.
0:44
By storing this information
in the game instance,
0:48
others could then apply
guesses from whatever means.
0:50
Let's get it working real quick,
and then it should start clicking.
0:55
Here we go.
Okay,
0:57
so let's move our story into
the In Progress column.
0:59
So first things first,
I want to pop into JShell and
1:04
introduce you to a new primitive data type
that you might have met before, char.
1:07
So the char data type is used to
represent a single character, in fact,
1:12
char is short for a character.
1:17
Use a char as the data type, letter =, and
1:19
here I want you to note that
we're using single quotes.
1:23
Now other languages can use the double
quotes and single quote interchangeably.
1:27
But in Java double is for string literals
and single is for char literals.
1:31
So I've used this analogy
about strings before.
1:39
But I want you to imagine
a string as a group of characters
1:43
strung together like a banner at a party.
1:46
So there are a few ways to check and
1:49
see if a string has a specific
character in its string of characters.
1:51
Here's one that we'll use, so
let's make an example, so let's say,
1:56
string example = "Hello", and
1:59
of course, now pay more attention
though there's double quotes there.
2:04
Okay, and
there are a lots of ways to do this,
2:08
the one that we're going
to use is called indexOf.
2:11
So, we're gonna say, example.indexOf, and
I'm going to look for the character e.
2:14
It tells us that it is at 1.
2:23
Note how it returned one but it really
seems like it should be two, right?
2:25
One, two, I would have assumed that.
2:30
Now, this is because when indexing
in Java and many other languages,
2:32
counting always starts at zero.
2:36
Now, this seems a little bit weird.
2:38
And I will try to cover this quite a bit
cuz I know it's something that people
2:40
struggle with.
2:43
A way that I remember this is,
by thinking of babies ages.
2:44
When babies are born we often
talk about their first year.
2:48
Yet, when we're asked how old they are,
we always answer in months.
2:51
And that's because zero
years old sounds weird.
2:55
How old's your baby?
2:57
Zero years old.
2:58
We don't say that.
2:59
So after their first year we
start saying one year old and
3:00
then we say two year old but
zero just sounds weird, right?
3:03
Well this is true here too,
we often say that the first element, but
3:06
in reality it's really the zeroth index.
3:10
So if we look at example.indexOf and
we push in an H.
3:13
Character is, I'm gonna do Ctrl + L,
so that we're clean.
3:21
So example.indexOf('H') in Hello,
will be the zeroith.
3:25
So what happens when it's not found?
3:33
So if you say example.indexOf, and
3:34
z is definitely not in there, so
what happens when z is not there?
3:37
So as long as the index of the char is
greater or equal to 0, it's in there.
3:45
So you can kind of do that
expression like this.
3:50
So we can say, example.indexOf('o'),
which is in there, right?
3:52
If that's greater or equal to 0,
we know it'll return a true, right?
3:59
And the same is true if it's missing.
4:04
So example indexOf, and we do y,
4:07
we say greater than or equal to 0.
4:10
It's going to tell us that that's false.
4:15
So the other way to check, and
this is kind of more succinct actually,
4:17
if you say, example.indexOf('e'),
is not equal to -1, right?
4:21
Cuz -1 is saying that it's not in there.
4:30
So this is like basically
saying if it's not found.
4:32
It's actually saying
if it's not not found.
4:33
One thing you might not of come across yet
is string concatenation.
4:37
So you can combine strings
using the plus sign,
4:41
kind of like add this word to this word.
4:45
Like so, so if I say,
4:47
example = example + "Wor".
4:51
What's gonna happen it's gonna append
example, just like we saw before.
4:57
You take the variable and
another variable and apply the plus to it.
5:01
And you can also concatenate chars.
5:05
So we'll say example = example + 'l',
the single character l.
5:08
Cool, so there we go.
5:17
And that shortcut that we learned
about adding integers also works.
5:18
So if I say, example += 'd'.
5:21
It's going to add d to example.
5:25
So now example has been changed.
5:28
Cool, so
5:31
armed with that info, let's use it to
solve our user story of applying guesses.
5:32
So we want to be able to accept a guess
and store if it was a hit or a miss.
5:38
So why don't we make that state.
5:43
Since strings can be concatenated and
5:46
checked, why don't we store that
info in strings themselves.
5:48
So let's go to the game logic and
5:52
in here, let's say private,
cuz we always start private, right?
5:55
We're gonna make a new string,
and will store hits.
5:58
And then misses,
that's two separate strings.
6:03
And inside of our game constructor,
we will initialize this.
6:07
So we'll say hits and misses.
6:12
Cool, so now those are initialized and
they're private.
6:19
I'm gonna scroll this down a little bit.
6:23
So now let's add a method that will allow
us to apply a guess towards the answer.
6:25
So let's do this, we know it's public.
6:31
We want people to be able to do a public,
and let's return whether or
6:34
not it was a hit.
6:37
So we'll use a Boolean return value,
right?
6:38
We'll say, you apply the guess
you know if it matched or not.
6:41
And this way consumers of the objects
will know if they got the guess right.
6:44
So we'll call applyGuess.
6:48
And let's see, we wanna take in a letter.
6:51
So let's make that a char letter, right?
6:55
A single letter is what we want.
6:57
So we're gonna start out and
we'll say boolean is it a hit,
6:59
and what we will do is we'll
see if it's in the answer.
7:03
Is answer indexOf(letter) not equal to -1.
7:06
So is the letter in the answer, okay?
7:15
We'll store that in this hit, so we will
say, if it is a hit, this is why camel
7:19
casing is important,
hits += letter, right?
7:24
So, we're gonna concatenate
the letter onto the hits.
7:30
So hits is an empty string originally.
7:32
Here we go.
7:34
This might be the first time that you've
encountered this type of branching,
7:35
it's possible to perform another block
of code when the expression isn't met.
7:39
So in this case, we want to say else here.
7:43
So else is used to describe what
to do if our expression here,
7:47
this is hit, is false, it's not met.
7:51
So, in our case, we are interested in
knowing if it wasn't a hit, right?
7:55
We're gonna say that goes
in the misses string, so.
8:00
Okay, and look, this is closing that block
and this is closing that block, cool.
8:06
And then finally, what we need to
do is we need to return isHit.
8:11
Now do you notice I did
something a little bad here.
8:17
Look, so
this is not the closing brace here.
8:20
We need to close the actual, you need that
method, we need this to close a class.
8:24
So I was missing a brace.
8:31
Very common as we start with more and
more of these braces.
8:33
Okay, so let's go ahead and
let's save this, and
8:36
let's jump in back to jshell here.
8:40
And we're gonna say /open Game.java.
8:42
And we will make a new game, so
we'll say Game game = new Game.
8:46
And we're gonna pass in treehouse.
8:51
Cool, and now,
we can use our applyGuess method.
8:54
So it should let us know
if it's in there or not.
8:58
So let's say game.applyGuess,
so t is in there, right?
9:01
So it's in the puzzle.
9:10
There it is right there
game is in treehouse.
9:11
So let's do it, this should return true.
9:13
Perfect, and
let's try to see what happens if we miss.
9:16
Should return false, right?
9:19
Perfect, the logic portion of
the story is looking great.
9:21
Let's get something using it.
9:24
Great job.
9:27
We have exposed our applyGuess
method in the Game class.
9:28
Now anyone using our game
object can add a guess and
9:31
the object can tell if it
exists in answer or not.
9:34
So let's go take some input from
the console using our prompt object.
9:37
But first let's work out those string and
char muscles.
9:41
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