This course will be retired on July 14, 2025.
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 Kotlin for Java Developers!
You have completed Kotlin for Java Developers!
Preview
In this video we'll see how we can print out the suits just like any other character!
Unicode Suits
diamonds -> "\u2666"
clubs -> "\u2663"
hearts -> "\u2665"
spades -> "\u2660"
Related Links
Project Files
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
All right on to the suparameter.
0:00
Did you know that each of
the suits is included in Unicode?
0:03
Meaning that each suit is
a character that we can type out.
0:07
Let's start by replacing our suit and
0:11
our print string with a dollar sign and
then brackets.
0:13
Inside the brackets, let's call
a function that doesn't exist yet
0:18
named getSuitChar and pass in the suit.
0:22
Then let's use Alt+Enter
to create the function.
0:28
And after it's created,
let's delete the brackets and
0:36
then set this function equal to
when suit and then add brackets.
0:40
And inside our when statement,
0:47
let's start by checking if
the suit is equal to diamonds.
0:49
And if it is,
Then let's return the Unicode
0:53
character for diamonds, which is \u2666.
1:00
Next hit command or
Ctrl+D three times to duplicate this line.
1:06
And then let's change these extra
1:14
diamonds to clubs, hearts and spades.
1:19
And let's change the Unicode
characters to u2663 for
1:24
clubs, u2665 for hearts and u2660 for
1:30
spades, these are all in
the teacher's notes as well.
1:35
Finally, since we're counting on this
when statement to always return something
1:42
let's add an else.
1:47
And if we get anything else,
1:49
let's return a string that says
incorrect suit then let's run the app.
1:52
And sweet, I got the ten of spades.
2:00
My favorite.
2:02
Last but not least,
let's deal with the face up property.
2:03
If a card is face up,
we should be able to see it.
2:07
But if it's face down, we shouldn't
be able to tell what card it is.
2:10
So really, I shouldn't be able to tell
that this card is a ten of spades.
2:14
To fix this, let's first take the face
up property out of our print string.
2:20
Then let's add a line above the return and
type if faceUp.
2:29
And then put our return
statement inside the brackets.
2:37
Next let's add an else and if it's
not face up, let's return three Xs.
2:41
Great, but before we move on, let's make
this two string function a lot smaller.
2:51
To do that, I first need to tell you
about the ternary operator and column.
2:56
Remember the Ternary Operator?
3:01
It's these guys.
3:03
They let us do a conditional
statement on only one line.
3:04
See, the thing is,
Kotlin doesn't have a Ternary Operator.
3:07
Instead, with Kotlin,
an if statement returns a value.
3:11
So instead of using some weird syntax,
we can just use if and
3:16
else like we're used to.
3:20
So to get this down to one line,
3:22
let's first delete the outside
brackets and then add an equal sign.
3:24
Then, since if statements already return
values, let's delete the return keywords.
3:30
And finally, let's get rid of the brackets
and put everything on one line.
3:41
Awesome, our two string
function is ready for action.
3:59
Now let's give it some exercise by
printing out the entire game board instead
4:03
of just the top card in our deck.
4:07
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