Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Learn some math shortcuts that integers provide
Learn more
-
0:00
We are finally to the point of writing our objects,
-
0:03
arguably most important method ever, dispense.
-
0:07
Like I was saying earlier, if we wanted to go overboard,
-
0:09
we could actually make one of these Pez objects.
-
0:12
Nothing's stopping us, right?
-
0:13
You could totally imagine it.
-
0:15
Our new Pez class would define some state, like flavor.
-
0:18
Maybe it could even store the time that it was inserted into the dispenser.
-
0:22
We could determine if it was stale.
-
0:24
When we call the dispense method we're about to write,
-
0:27
we could actually return one of these Pez objects.
-
0:30
Or that's going a little overboard, let's not do that just yet.
-
0:34
Now, I was being a little silly there.
-
0:36
But this is actually a pretty hard thing that you're going to grapple with.
-
0:40
How do you know when to stop building objects,
-
0:43
because like we talked about before, everything can be modeled as an object.
-
0:48
I struggle with this all the time.
-
0:51
A common acronym that you'll probably start hearing a lot is YAGNI.
-
0:55
You ain't gonna need it.
-
0:57
In fact, I keep a sticky on my laptop and
-
1:00
it helps me to remember not to overengineer before you actually need it.
-
1:04
Now there's another one that's pretty common sense, but boy,
-
1:07
do we forget it the more we start programming.
-
1:09
And that's this, KISS.
-
1:12
Keep it simple, smarty pants or something more rude than that.
-
1:16
So, with that said, let's keep it simple and assume that there is only one flavor.
-
1:22
What we'll do is, as long as there is a Pez to be dispensed, we will return true.
-
1:26
Of course, if it is in fact empty, we'll return false.
-
1:29
Sound good?
-
1:30
Let's do this.
-
1:32
Okay, so I have JShell open again and
-
1:34
I wanna show off a couple of cool tricks that we can do with integers.
-
1:38
Okay, so first off we know that we can create one, right?
-
1:41
So we're gonna say, int example = 1.
-
1:47
And if we wanted to just add one to it, we could use the variable itself.
-
1:52
So we can say something like, example = example + 1, right?
-
1:57
So that's gonna return 2.
-
1:59
So, we've set, now example is 2, right?
-
2:03
So, since that's so common, there is a special shorthand for it, and
-
2:07
it looks like this.
-
2:08
So you say example += 1 and basically it's this.
-
2:14
It does exactly this, it's just shorthand for it.
-
2:18
Cool, so that, return 3 and also updated example to be 3.
-
2:25
Okay, finally, because we end up adding one a lot,
-
2:31
there's an even shorter form and it's called incrementing.
-
2:35
And it looks like this.
-
2:37
So we say example++.
-
2:39
Now this one's a little bit dangerous.
-
2:41
It actually returns first and then changes the value.
-
2:45
Because this is what is known as post incrementing.
-
2:47
So if we do this, it's gonna go ahead and it's stored 3 in 5.
-
2:52
Now remember, example was 3.
-
2:53
But if I look at example now, it's 4.
-
2:58
So it actually did example plus one.
-
3:01
But it returned, the expression itself returned what the value used to be,
-
3:05
much like that swap head that we were looking at earlier.
-
3:08
But you can actually do the other side too, you can do pre increment.
-
3:12
So you can say example, ++example.
-
3:16
And that returns 5 and example is 5.
-
3:22
And that does the reverse, right?
-
3:24
It increments and then it returns the value.
-
3:27
Now the dangerous thing about this is that you can use these in a very
-
3:30
complex statement.
-
3:32
Now if you can't do the incrementing on a line by itself,
-
3:36
I highly suggest you don't use these at all.
-
3:39
In other words, I highly recommend that you don't use the value returned from
-
3:43
either the pre or post increment.
-
3:45
As you can probably guess by now, the same works with subtracting.
-
3:50
So I'm gonna clear here and we have, example's 5 so
-
3:54
we say example = example- 1.
-
3:57
We'll see that that example is 4 now, and
-
4:02
you can also do example -=1 and example's now 3.
-
4:08
And then, finally, as the other one is called incrementing, with the pluses,
-
4:12
this is called decrementing.
-
4:14
So we can say example-- and we did it on the other side, so
-
4:19
it's post but now example's actually 2.
-
4:24
Well, decrementing sounds like exactly what we need to do
-
4:27
when we want to dispense.
-
4:29
Again, so the dispense method that we're gonna write should work like this.
-
4:32
Every time that we dispense, we should decrement the Pez count,
-
4:36
as long as there's some left.
-
4:38
And we should let the caller know that we dispensed something, cool.
-
4:44
So, in PezDispenser, let's move this down here.
-
4:50
Let's go ahead and we'll make a new method.
-
4:54
It's going to return a true or false, right?
-
4:57
And it will have it be called dispense, and it won't take anything.
-
5:01
So, we'll store the default value here, we'll say boolean
-
5:06
wasDispensed, and we'll set the default value to false.
-
5:11
Okay, so we know that we can say if it's not empty, right?
-
5:19
So, let's look at this code block.
-
5:20
So if it's not empty we want to decrement the Pez count.
-
5:23
So we do that as such, we say pezCount--, so
-
5:27
that whatever is in there moves down one.
-
5:30
And we're gonna say wasDispensed, we're gonna set that equal to true,
-
5:35
because it was actually.
-
5:36
And then finally, we're going to return what was dispensed.
-
5:41
So it will start out as false, if there's anything in there it will go through.
-
5:45
If it's not it will never come in here and it will just say false and
-
5:48
the number won't go down.
-
5:50
Makes sense?
-
5:51
Okay, so let's flip over to our Example.java.
-
5:55
And I'm going to drop out of the JShell here if we do Ctrl+B.
-
6:00
And I will say clear, all right.
-
6:03
So in Example.java here, let's just go ahead and
-
6:08
empty the whole thing in one sitting.
-
6:11
We're probably all guilty of, I know I am.
-
6:14
So, this should be fairly easy.
-
6:16
All we need to do is just call the dispense method 12 times, right?
-
6:20
Well, that worked, but this sounds like a job for a loop.
-
6:24
We want to keep dispensing as long as we get something back.
-
6:28
So, while there's something dispensed, eat it.
-
6:31
Sound good?
-
6:32
Sounds like a while loop right?
-
6:34
So, just a quick reminder, so we'll say while dispenser.dispense().
-
6:42
Well that's happening, let's just write out chomp I guess, right?
-
6:51
And then finally, if it is actually empty,
-
6:54
which it should be, cuz we're gonna eat them all.
-
7:00
We'll say system.out.println(Ate all the PEZ).
-
7:15
Something's not lining up, look, I forgot a quote here.
-
7:20
There we go.
-
7:22
All right, so this is gonna return true or false.
-
7:27
And as long as it's returning true, it's gonna keep on chomping.
-
7:31
Sound good, let's take a look.
-
7:37
Clear && javac Example.java && java Example.
-
7:46
Here we go, chomp, chomp, chomp, chomp, chomp.
-
7:50
We ate all the Pez, cool.
-
7:51
So we should have 12 there, chomp, chomp, chomp, chomp, chomp, chomp, chomp,
-
7:54
chomp, chomp.
-
7:55
Ate all the Pez, awesome.
-
7:57
Good job, we can now load and dispense our Pez.
-
8:00
We are getting close to completing this model.
-
8:03
Now that we're removing Pez, we should probably get more specific around
-
8:07
the handling of what it means to fill.
-
8:10
In the real world, if we try to load a dispenser that
-
8:13
already has some Pez in there, we'd add Pez until it was is in fact full.
-
8:17
We wouldn't just add the maximum amount available, because that could be
-
8:20
potentially overfilling and wasting the remainder, we don't wanna waste.
-
8:26
Let's practice incrementing and decrementing.
You need to sign up for Treehouse in order to download course files.
Sign up