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