1
00:00:00,144 --> 00:00:08,472
[BLANK_AUDIO]

2
00:00:08,472 --> 00:00:11,288
Last time we talked about to use C sharp

3
00:00:11,288 --> 00:00:16,080
constructors to instantiate a class to
create a new object.

4
00:00:16,080 --> 00:00:17,940
And we also talked about how we could

5
00:00:17,940 --> 00:00:21,110
access properties of the object that we
created.

6
00:00:21,110 --> 00:00:24,395
This time we're going to talk about how we
use methods

7
00:00:24,395 --> 00:00:28,210
to get at the behavior that the object
provides to us.

8
00:00:28,210 --> 00:00:31,039
Before we start talking about the details,
here

9
00:00:31,039 --> 00:00:34,075
is a reminder of the module learning
objectives

10
00:00:34,075 --> 00:00:35,410
for this module.

11
00:00:35,410 --> 00:00:39,193
We're still working on the first one,
we'll handle the second one next time.

12
00:00:39,193 --> 00:00:44,208
[BLANK_AUDIO]

13
00:00:44,208 --> 00:00:48,300
We'll immediately start with an in-lecture
quiz, so you can tell me whether

14
00:00:48,300 --> 00:00:52,000
or not you remember how we actually access
the behavior of an object.

15
00:00:57,590 --> 00:01:02,260
To figure out which methods we want to
use, we read the documentation.

16
00:01:02,260 --> 00:01:05,240
So we're going to continue working with
our deck class.

17
00:01:05,240 --> 00:01:08,879
And as we look at this documentation, we
see that there's a list of.

18
00:01:10,290 --> 00:01:14,250
Four different methods with the
description of what those methods do.

19
00:01:14,250 --> 00:01:17,960
So we're going to interact with three of
those methods.

20
00:01:17,960 --> 00:01:20,690
We won't worry about the print method
today,

21
00:01:20,690 --> 00:01:22,790
but we'll try the other three to get

22
00:01:22,790 --> 00:01:26,920
some practice in how we actually call a
method on a particular object.

23
00:01:31,760 --> 00:01:37,490
We'll start with the shuffle method, so we
can tell the deck to shuffle itself.

24
00:01:37,490 --> 00:01:41,150
So let's talk about what this
documentation tells us.

25
00:01:41,150 --> 00:01:44,250
First of all, we know the method is
public.

26
00:01:44,250 --> 00:01:47,250
And all the methods we're going to look at
are public.

27
00:01:48,410 --> 00:01:53,650
So we will be accessing them from external
to the deck class.

28
00:01:53,650 --> 00:01:57,907
The next thing is we have this indicator

29
00:01:57,907 --> 00:02:02,610
of what data type The method actually
returns.

30
00:02:02,610 --> 00:02:05,530
So sometimes a method will return
something to us.

31
00:02:05,530 --> 00:02:08,340
And we'll see that before we finish the
lecture.

32
00:02:08,340 --> 00:02:09,670
But sometimes it doesn't.

33
00:02:09,670 --> 00:02:12,130
Sometimes it just, you know, the deck
shuffles

34
00:02:12,130 --> 00:02:14,640
itself, but what would it give back to us?

35
00:02:14,640 --> 00:02:16,370
There's nothing to get back.

36
00:02:16,370 --> 00:02:19,108
And there's a special word that we use in
C#

37
00:02:19,108 --> 00:02:23,700
to say, this method really doesn't return
anything to us.

38
00:02:23,700 --> 00:02:26,180
And that word is, void.

39
00:02:26,180 --> 00:02:29,830
So the shuffle method doesn't return
anything to us.

40
00:02:29,830 --> 00:02:33,800
Then next thing we see, I won't bother
underlining it, is a method name.

41
00:02:33,800 --> 00:02:36,700
So the method name for shuffling the deck
is shuffle.

42
00:02:36,700 --> 00:02:39,740
And then finally, just as for the
constructor.

43
00:02:39,740 --> 00:02:42,256
We have parentheses that say what kind of

44
00:02:42,256 --> 00:02:45,840
information needs to be provided to the
method.

45
00:02:45,840 --> 00:02:48,864
And for the shuffle method, there is no
information that

46
00:02:48,864 --> 00:02:53,579
needs to be provided.
So let's go to our environment.

47
00:02:57,070 --> 00:03:01,780
Now, I basically saved our project from
last time and we'll build on it this time.

48
00:03:01,780 --> 00:03:05,146
Sometimes when you open up a visual studio
project, you'll

49
00:03:05,146 --> 00:03:08,380
see something like this where there are no
classes open and

50
00:03:08,380 --> 00:03:11,482
you have a big blank, blue screen,
especially if I've

51
00:03:11,482 --> 00:03:15,260
provided some code to you, you might see
it this way.

52
00:03:15,260 --> 00:03:16,830
The easy thing is.

53
00:03:16,830 --> 00:03:19,618
All along the right-hand side here, we

54
00:03:19,618 --> 00:03:22,910
have all the classes included in our
solution.

55
00:03:22,910 --> 00:03:28,220
So for in console application, program.cs,
we double-click it,

56
00:03:28,220 --> 00:03:31,040
it opens up our C# file, we zoom in.

57
00:03:32,230 --> 00:03:34,389
And so this is where we were working
before.

58
00:03:37,090 --> 00:03:41,875
After printing the deck empty information,
let's tell the deck to shuffle itself.

59
00:03:41,875 --> 00:03:42,855
[NOISE]

60
00:03:42,855 --> 00:03:49,519
And of course, we don't start comments

61
00:03:49,519 --> 00:03:54,699
with single quotes.
We start them with slash slash

62
00:03:57,360 --> 00:04:01,058
Now.
Just as here, when we wanted to find out

63
00:04:01,058 --> 00:04:05,762
whether a particular deck was empty we had
to first provide

64
00:04:05,762 --> 00:04:10,410
the object, the variable, for the deck we
cared about.

65
00:04:11,570 --> 00:04:17,470
So, we do the same exact thing

66
00:04:17,470 --> 00:04:22,788
with a method call.
So the first

67
00:04:22,788 --> 00:04:30,475
thing I type is deck, and then I put dot,
and then I put the method name.

68
00:04:30,475 --> 00:04:34,201
In this case, in this case it's shuffle,
now methods, the way

69
00:04:34,201 --> 00:04:38,396
that we call methods, is different than
the way we access properties.

70
00:04:38,396 --> 00:04:41,592
Because we always put parentheses just
like we did with the constructor.

71
00:04:41,592 --> 00:04:43,542
When we called the constructor up here, we
had

72
00:04:43,542 --> 00:04:47,370
to provide parentheses even though we
didn't provide any information.

73
00:04:47,370 --> 00:04:48,110
Down here,

74
00:04:48,110 --> 00:04:49,000
same thing.

75
00:04:49,000 --> 00:04:52,170
Calling a method, we always have
parentheses.

76
00:04:52,170 --> 00:04:53,580
Accessing a property.

77
00:04:53,580 --> 00:04:55,950
We never have parantheses, that's the
rule.

78
00:04:57,060 --> 00:05:04,490
Okay, so I will Ctrl+F5, even though
there's nothing really interesting here.

79
00:05:04,490 --> 00:05:07,910
So in fact, I'm going to I won't say I

80
00:05:07,910 --> 00:05:13,141
lied, but let's actually print the deck,
both before And

81
00:05:13,141 --> 00:05:16,760
after the shuffle just to see that there
was a shuffle.

82
00:05:16,760 --> 00:05:19,064
And we'll even add a blank line between
the

83
00:05:19,064 --> 00:05:22,490
two so we can easily see where that
shuffle occurred.

84
00:05:22,490 --> 00:05:26,190
We won't necessarily keep this code
throughout the whole lesson.

85
00:05:26,190 --> 00:05:30,260
But we'll use it for now as sort of
temporary testing code.

86
00:05:30,260 --> 00:05:33,120
So we'll tell the deck to print itself.

87
00:05:33,120 --> 00:05:38,000
And you'll notice I called this method in
exactly the same way I called shuffle.

88
00:05:38,000 --> 00:05:38,240
I just

89
00:05:38,240 --> 00:05:39,315
changed the method name.

90
00:05:39,315 --> 00:05:45,800
And then we'll shuffle it, and then we'll
tell the deck to print itself again.

91
00:05:47,100 --> 00:05:50,148
And in fact we better add that blank line
I promised you.

92
00:05:50,148 --> 00:05:52,043
[SOUND]

93
00:05:52,043 --> 00:05:58,670
Like so.
So when I Ctrl+F5, I've created the deck.

94
00:05:58,670 --> 00:06:05,290
I'm going to scroll up in this window, and
we'll see here's the empty access.

95
00:06:05,290 --> 00:06:10,180
And then we see the deck is printed out in
order by rank, and then by suit.

96
00:06:10,180 --> 00:06:13,450
We look for the blank line, there it is.

97
00:06:13,450 --> 00:06:17,230
And now we look at the contents of the
deck from top to bottom, and we see that

98
00:06:17,230 --> 00:06:18,890
it's shuffled.

99
00:06:18,890 --> 00:06:24,890
So we have assurance that I have in fact
shuffled the deck properly.

100
00:06:24,890 --> 00:06:30,790
Now this was temporary testing code so I'm
highlighting or selecting this line.

101
00:06:30,790 --> 00:06:33,805
And clicking up here, that says commented
out, I'm

102
00:06:33,805 --> 00:06:37,320
going to select these lines, and say
comment them out.

103
00:06:37,320 --> 00:06:40,129
So that's a really useful development
environment thing to be

104
00:06:40,129 --> 00:06:42,249
able to add code and comment it out when
we don't

105
00:06:42,249 --> 00:06:43,450
want it anymore.

106
00:06:43,450 --> 00:06:46,790
Eventually, we would get rid of this code,
but we'll leave it there for now.

107
00:06:48,130 --> 00:06:49,390
And that's it.

108
00:06:49,390 --> 00:06:52,978
For telling the deck to shuffle itself,
our first call

109
00:06:52,978 --> 00:06:57,850
to a method to actually make the deck
object do something.

110
00:06:57,850 --> 00:07:02,180
The next method we're going to look at, is
cut.

111
00:07:03,290 --> 00:07:07,558
Because that's slightly more complicated
in terms

112
00:07:07,558 --> 00:07:11,130
of the.
The way we call the method.

113
00:07:11,130 --> 00:07:15,200
So let's move on to the cut and here's
what's different.

114
00:07:15,200 --> 00:07:21,750
It's still public, it's still void, the
method name is different as you'd expect.

115
00:07:21,750 --> 00:07:24,820
But the big difference is right here in
the documentation.

116
00:07:26,700 --> 00:07:28,860
For the first time we're seeing something
that

117
00:07:28,860 --> 00:07:31,730
we call that has something between those
parentheses.

118
00:07:31,730 --> 00:07:32,653
Something that

119
00:07:32,653 --> 00:07:35,600
we're suppose to provide to the method.

120
00:07:35,600 --> 00:07:39,660
And, to this particular case it say's the
location at which to cut the deck.

121
00:07:39,660 --> 00:07:42,320
That's what we're suppose to provide.

122
00:07:42,320 --> 00:07:46,890
Now, from the methods perspective this
thing.

123
00:07:46,890 --> 00:07:49,330
This piece of information is called a
parameter.

124
00:07:50,500 --> 00:07:53,236
From the calling side, the side where
we're

125
00:07:53,236 --> 00:07:56,310
going to call it from, it's called an
argument.

126
00:07:57,460 --> 00:08:00,001
The good news is, the only thing that we

127
00:08:00,001 --> 00:08:03,380
really care about is not the name of the
parameter.

128
00:08:04,380 --> 00:08:08,700
But we do care that it's supposed to be an
it.

129
00:08:08,700 --> 00:08:12,210
So when we call the cut method.

130
00:08:12,210 --> 00:08:15,750
We're going to provide an int as an
argument.

131
00:08:15,750 --> 00:08:18,090
So let's actually do that.

132
00:08:18,090 --> 00:08:22,535
Let's put it right, well, that testing

133
00:08:22,535 --> 00:08:24,885
code is handy for us.

134
00:08:24,885 --> 00:08:25,230
[NOISE]

135
00:08:25,230 --> 00:08:29,416
Let's put it right here.
We'll say cut the deck.

136
00:08:29,416 --> 00:08:30,790
[NOISE]

137
00:08:30,790 --> 00:08:32,770
And we're going to tell the deck to cut
itself.

138
00:08:34,390 --> 00:08:37,450
And so we'll say cut.

139
00:08:37,450 --> 00:08:42,347
But when we go here, we open the
parenthesis and it actually tells us in

140
00:08:42,347 --> 00:08:48,640
this little pop up box, it tells us,
you're supposed to provide a location.

141
00:08:48,640 --> 00:08:50,600
And so let's cut the deck right in the
middle.

142
00:08:51,790 --> 00:08:53,190
We'll do it at location 26.

143
00:08:53,190 --> 00:08:55,887
And the reason I said it's nice saved

144
00:08:55,887 --> 00:09:01,430
that testing code, is that I'm actually
going to uncomment this again.

145
00:09:01,430 --> 00:09:06,800
And I'm actually going to comment the
shuffle, as well.

146
00:09:06,800 --> 00:09:08,490
Why am I commenting the shuffle?

147
00:09:08,490 --> 00:09:12,956
Because the shuffle adds a, sort of
randomizes the order of the cards

148
00:09:12,956 --> 00:09:18,190
in the deck, and I'd rather just see that
the cut occurred properly.

149
00:09:18,190 --> 00:09:18,690
So.

150
00:09:19,710 --> 00:09:21,268
I meant to un-comment

151
00:09:21,268 --> 00:09:26,070
this, so I'll click that twice.
In fact, I'll do this one again, too.

152
00:09:26,070 --> 00:09:29,085
So, basically we're going to print the
deck in order as

153
00:09:29,085 --> 00:09:32,280
the deck was produced by the deck factory,
if you will.

154
00:09:32,280 --> 00:09:34,210
And then we're going to cut the deck.

155
00:09:34,210 --> 00:09:37,030
We've gotten rid of, we've commented out
the shuffle.

156
00:09:37,030 --> 00:09:39,028
And then we're going to print the deck
again

157
00:09:39,028 --> 00:09:41,710
so that we can confirm that it actually
got cut.

158
00:09:41,710 --> 00:09:45,950
So Ctrl+F5, and we'll scroll up to the
top,

159
00:09:45,950 --> 00:09:50,402
just to remind ourselves that it starts
with clubs

160
00:09:50,402 --> 00:09:55,570
and diamonds, and then goes to hearts and
spades.

161
00:09:55,570 --> 00:10:00,670
But after the space, we see that the
hearts and spades are on top.

162
00:10:00,670 --> 00:10:02,680
And then the clubs and diamonds are down
below.

163
00:10:02,680 --> 00:10:06,330
So we've cut the deck properly, great.

164
00:10:06,330 --> 00:10:08,876
So, this debugging code that we added just
to

165
00:10:08,876 --> 00:10:11,020
make sure or testing code if you will to

166
00:10:11,020 --> 00:10:14,980
make sure it's working is coming pretty
handy so far.

167
00:10:14,980 --> 00:10:19,930
So now we've done two different methods,
both of them returned void.

168
00:10:19,930 --> 00:10:22,450
So the first one returned nothing and we
didn't

169
00:10:22,450 --> 00:10:25,980
have to provide anything and it just
shuffled the deck.

170
00:10:25,980 --> 00:10:28,640
The second one it returns nothing, and we

171
00:10:28,640 --> 00:10:32,240
had to provide one piece of information to
it.

172
00:10:32,240 --> 00:10:36,630
So let's look at one more example of how
we call methods.

173
00:10:40,420 --> 00:10:43,460
The next method that we want to take a
look at will

174
00:10:43,460 --> 00:10:47,868
be take top card, because it's fairly
common for us to actually want

175
00:10:47,868 --> 00:10:51,972
to talk the top card from a deck, whether
we're flipping it over

176
00:10:51,972 --> 00:10:56,440
into a pile or handing it to a player, or
something like that.

177
00:10:56,440 --> 00:10:58,450
So, we want to take the top card.

178
00:10:59,850 --> 00:11:05,420
Again, it's a public method.
This time though, we can see here,

179
00:11:05,420 --> 00:11:12,380
that it doesn't return nothing.
It returns a card object.

180
00:11:12,380 --> 00:11:14,030
So that makes sense, right?

181
00:11:14,030 --> 00:11:17,600
If we're going to take the top card from
the deck, we want to.

182
00:11:17,600 --> 00:11:20,880
Put it somewhere, or print it, or
something,

183
00:11:20,880 --> 00:11:24,070
and so we would want to take that card.

184
00:11:24,070 --> 00:11:28,180
So, let's actually comment out our testing
code one more time.

185
00:11:30,430 --> 00:11:32,862
I'll even comment out the cutting the deck
so

186
00:11:32,862 --> 00:11:35,970
that we know which card we really expect
to get.

187
00:11:35,970 --> 00:11:38,699
We should get the ace of clubs when we
take the top card.

188
00:11:39,850 --> 00:11:46,660
So let's actually take the top card, take
top card and print info.

189
00:11:48,750 --> 00:11:53,262
So this time, the method actually returns
something to

190
00:11:53,262 --> 00:11:56,880
us, and we want to hold it in a variable.

191
00:11:56,880 --> 00:11:59,220
We know it returns a card, so this
variable

192
00:11:59,220 --> 00:12:01,500
we hold it in, needs to be of, type card.

193
00:12:02,770 --> 00:12:04,680
I'll just call the variable card.

194
00:12:04,680 --> 00:12:12,630
And then we tell the deck that we want to
take the top card, like so.

195
00:12:13,820 --> 00:12:15,767
And then we're going to print the rank and

196
00:12:15,767 --> 00:12:18,068
suit of the card by accessing the
properties of

197
00:12:18,068 --> 00:12:20,310
the card, just like we accessed the
property

198
00:12:20,310 --> 00:12:22,570
of the deck to see if it was empty.

199
00:12:22,570 --> 00:12:23,990
So we'll do a console right line.

200
00:12:24,995 --> 00:12:25,760
[NOISE]

201
00:12:25,760 --> 00:12:29,475
And we'll access the rank.

202
00:12:29,475 --> 00:12:30,140
[NOISE]

203
00:12:30,140 --> 00:12:37,721
And then we'll concatenate a string that
says of with spaces on both

204
00:12:37,721 --> 00:12:43,930
side and then we'll concatenate the suit.
So now when I run the code.

205
00:12:45,290 --> 00:12:48,950
The deck is not empty, and we get the king
of spades.

206
00:12:48,950 --> 00:12:51,326
So I lied to you, when we print the deck,
we

207
00:12:51,326 --> 00:12:55,370
actually print it from bottom to top, not
from top to bottom.

208
00:12:55,370 --> 00:12:58,080
So we got the top card, which is the king
of spades.

209
00:12:58,080 --> 00:13:00,445
And actually if you take a look at the
book, it talks

210
00:13:00,445 --> 00:13:03,380
about why, for efficiency's sake, we want
to do it that way.

211
00:13:04,630 --> 00:13:09,750
Okay, so I've got the king of spades, Now,
a caution for you.

212
00:13:10,760 --> 00:13:15,632
It is in fact possible, comment out this
code, it

213
00:13:15,632 --> 00:13:20,504
is possible, even though the take top card
method

214
00:13:20,504 --> 00:13:27,116
returns a card object, we can still call
the method without putting

215
00:13:27,116 --> 00:13:34,033
the result into a variable Basically what
happens is, we call the method,

216
00:13:34,033 --> 00:13:38,930
it hands an object back to us, and
conceptually anyway, we just throw

217
00:13:38,930 --> 00:13:44,190
it on the ground and say, I don't need
that card after all.

218
00:13:44,190 --> 00:13:45,708
The problem is of course,

219
00:13:45,708 --> 00:13:50,510
that if I've done that now I can't print
out information about the card.

220
00:13:50,510 --> 00:13:52,980
So this is useful sometimes being able to
call

221
00:13:52,980 --> 00:13:57,340
a method that returns something without
storing what it returns.

222
00:13:57,340 --> 00:14:00,270
But, it is not generally useful.

223
00:14:00,270 --> 00:14:02,854
Usually, when we call a method that's
going to

224
00:14:02,854 --> 00:14:06,900
return something we want to store whatever
it returns.

225
00:14:06,900 --> 00:14:10,771
And that's it for the C# calling three
different kinds of

226
00:14:10,771 --> 00:14:13,000
methods on the deck class.

227
00:14:13,000 --> 00:14:17,484
One that doesn't return anything and
doesn't have any parameters,

228
00:14:17,484 --> 00:14:21,664
one that doesn't return anything and does
have a parameter, one

229
00:14:21,664 --> 00:14:26,084
argument from the calling side that cut
Method and one that

230
00:14:26,084 --> 00:14:30,558
does return something but doesn't require
us to provide any information.

231
00:14:30,558 --> 00:14:36,950
So, to recap we learned how to call
various methods.

232
00:14:36,950 --> 00:14:43,350
And that completes our how do we handle
the C# things with classes and objects.

233
00:14:43,350 --> 00:14:46,630
We now know how to create objects using
the constructor.

234
00:14:46,630 --> 00:14:50,250
We know how to access the state of the
objects using properties.

235
00:14:50,250 --> 00:14:54,740
And we know how to access the behavior of
those objects by calling on their methods.

236
00:14:54,740 --> 00:14:58,540
Next time we'll finally get to that second
module learning

237
00:14:58,540 --> 00:15:02,264
objective where we will in fact go through
a design process

238
00:15:02,264 --> 00:15:03,110
for a class.