1 00:00:01,450 --> 00:00:04,300 One nice thing that we can do as proactive developers, 2 00:00:04,300 --> 00:00:06,790 is to add methods that provide functionality or 3 00:00:06,790 --> 00:00:11,120 information that we expect might be a common question about our object. 4 00:00:11,120 --> 00:00:15,090 By gathering common functionality, we can help to reduce code duplication for 5 00:00:15,090 --> 00:00:17,240 ourselves, and for consumers of our code. 6 00:00:18,450 --> 00:00:21,470 We can use methods to provide conceptual state 7 00:00:21,470 --> 00:00:23,670 about our instance that other developers, and 8 00:00:23,670 --> 00:00:27,540 us included, don't actually need to worry about when using the object. 9 00:00:27,540 --> 00:00:30,280 So more specifically, if we try and 10 00:00:30,280 --> 00:00:33,740 visualize how people are going to use our Pez dispenser object, 11 00:00:33,740 --> 00:00:38,120 one common question I could foresee happening would be, is this thing empty? 12 00:00:38,120 --> 00:00:41,640 Now I'll check, and I'll say no, because there is indeed some in there. 13 00:00:41,640 --> 00:00:44,690 Because that is probably going to be asked of the object, 14 00:00:44,690 --> 00:00:49,640 we should probably provide the answer to that common question to our users. 15 00:00:49,640 --> 00:00:52,680 What we don't want to do is force the user to understand how our 16 00:00:52,680 --> 00:00:54,420 object works internally. 17 00:00:54,420 --> 00:00:57,240 We just want them to be able to ask, is it empty? 18 00:00:57,240 --> 00:00:59,920 And our object can respond with yep or nope. 19 00:00:59,920 --> 00:01:01,120 So let's do that. 20 00:01:01,120 --> 00:01:04,920 Let's create that method but before we create that method, let's jump back into 21 00:01:04,920 --> 00:01:07,930 Jshell and add some more powerful tools to the expressions we write. 22 00:01:09,060 --> 00:01:13,410 Okay, so go ahead and open up JShell, and let's walk through a couple things. 23 00:01:13,410 --> 00:01:18,360 So first, we know how to see if a number is greater or 24 00:01:18,360 --> 00:01:19,580 less than another number, right? 25 00:01:19,580 --> 00:01:24,020 So we can do that, we can say 5 is greater than 4, and we use the sign like that. 26 00:01:24,020 --> 00:01:28,570 And we can also say that 6 is less than 7. 27 00:01:28,570 --> 00:01:32,880 Now one thing that might not be too clear is equality of numbers. 28 00:01:32,880 --> 00:01:37,542 So, let's go ahead and let's say that five is equal to five. 29 00:01:37,542 --> 00:01:40,070 Now, there's a little thing that you need to be careful of here. 30 00:01:40,070 --> 00:01:43,070 When you're testing equality, you need to use double equals. 31 00:01:43,070 --> 00:01:45,880 So you say equal equal five. 32 00:01:45,880 --> 00:01:48,890 And that is because, as you know, 33 00:01:48,890 --> 00:01:54,110 the equal sign has already been used for variable assignments, like this, right. 34 00:01:54,110 --> 00:01:57,110 So, if we said the age is equal to 19, 35 00:01:57,110 --> 00:02:00,610 that's how we set the age for variable assignment. 36 00:02:00,610 --> 00:02:03,850 But, if we wanted to check the age, we would need to do double equals. 37 00:02:03,850 --> 00:02:05,920 Otherwise, we'd just be overwriting it each time. 38 00:02:05,920 --> 00:02:08,230 Common mistake, pay attention to that one. 39 00:02:08,230 --> 00:02:12,590 Just want to make sure that was out there, so we have this now we can check equality. 40 00:02:12,590 --> 00:02:15,720 So, we should be able to make the is empty method, right? 41 00:02:15,720 --> 00:02:19,370 Since we don't want anybody else to have to write the double equals code 42 00:02:19,370 --> 00:02:20,600 double equals zero. 43 00:02:20,600 --> 00:02:24,240 We definitely know that we want the method to be public, 44 00:02:24,240 --> 00:02:25,530 because we want people to use this. 45 00:02:25,530 --> 00:02:30,940 So we'll say public, and next we want it to return a true or 46 00:02:30,940 --> 00:02:34,480 false value, which means we'll be expecting a boolean. 47 00:02:34,480 --> 00:02:38,030 Booleans are a special case, when you name property methods. 48 00:02:38,030 --> 00:02:42,130 So the standard, you know, for getters like we have before, we have a get. 49 00:02:42,130 --> 00:02:45,470 If you have one that is a boolean, you want it to be is, 50 00:02:45,470 --> 00:02:48,240 because it kind of reads better that way, and you'll see here a second. 51 00:02:48,240 --> 00:02:50,790 So we'll say IS empty, right? 52 00:02:52,840 --> 00:02:55,130 Okay, so we'll do that, we'll open it, we'll close it. 53 00:02:55,130 --> 00:02:57,220 So, what should we do? 54 00:02:57,220 --> 00:03:01,002 Let's think, we could create a brand new private boolean field and 55 00:03:01,002 --> 00:03:02,375 store it up here, right? 56 00:03:02,375 --> 00:03:06,905 But with our current code, whenever somebody does this fill method here, 57 00:03:06,905 --> 00:03:09,255 we'd have to set it to true or false. 58 00:03:09,255 --> 00:03:12,095 And it seems like we'd have to like always keep state in that way. 59 00:03:12,095 --> 00:03:14,615 It seems awfully brittle doesn't it? 60 00:03:14,615 --> 00:03:16,095 It's a whole lot to remember and if we forget, 61 00:03:16,095 --> 00:03:18,235 it could actually end up doing more harm than good. 62 00:03:19,680 --> 00:03:24,800 So a nice thing about objects is that we can actually compute our property. 63 00:03:24,800 --> 00:03:26,350 Now, we know how to do it, right. 64 00:03:26,350 --> 00:03:27,130 We just talked about it. 65 00:03:27,130 --> 00:03:31,690 If the Pez count is zero, then it's empty. 66 00:03:31,690 --> 00:03:33,630 So we could do something like this, right. 67 00:03:33,630 --> 00:03:34,540 So this code is going to run. 68 00:03:34,540 --> 00:03:36,060 So we can make a new variable here, 69 00:03:36,060 --> 00:03:39,540 and we'll name it something different, is actually empty. 70 00:03:40,650 --> 00:03:43,280 And we'll do just like we did down here in the Jshell. 71 00:03:43,280 --> 00:03:48,470 We'll say if the Pez count is double equal to zero, 72 00:03:48,470 --> 00:03:51,190 the Pez count is equal to zero, then it is empty, right? 73 00:03:51,190 --> 00:03:56,270 So, we'll return this variable here is actually empty. 74 00:03:56,270 --> 00:03:56,880 That works. 75 00:03:57,930 --> 00:03:58,610 But wait a second. 76 00:03:59,730 --> 00:04:02,260 Notice how we're creating a variable here, 77 00:04:02,260 --> 00:04:04,430 in the just immediately returning its value. 78 00:04:04,430 --> 00:04:06,050 Well, we can actually clean this up. 79 00:04:06,050 --> 00:04:07,970 We can actually return this expression. 80 00:04:07,970 --> 00:04:11,440 So I'm gonna copy that and I'm going to paste it here. 81 00:04:11,440 --> 00:04:13,180 And then I want to get rid of this line here and so 82 00:04:13,180 --> 00:04:16,230 it just has this has pezCount is double equal to zero. 83 00:04:16,230 --> 00:04:19,900 Because this is going to return a true, and then our method is going to return 84 00:04:19,900 --> 00:04:23,020 that value and it's gonna be a boolean, or a true or a false. 85 00:04:23,020 --> 00:04:24,450 Cool. So I have saved that and 86 00:04:24,450 --> 00:04:25,520 it looks like it's working. 87 00:04:25,520 --> 00:04:26,250 I wanna go ahead. 88 00:04:26,250 --> 00:04:27,190 Let's make sure it is. 89 00:04:27,190 --> 00:04:29,550 So we're gonna explore. 90 00:04:29,550 --> 00:04:31,350 We'll open PezDispenser to that Java. 91 00:04:34,000 --> 00:04:35,390 We'll make a new PezDispenser. 92 00:04:37,340 --> 00:04:42,990 You know use that poor naming convention there of PD, we'll say new PezDispenser. 93 00:04:45,150 --> 00:04:48,820 Cool so we've got a new one, 94 00:04:48,820 --> 00:04:51,850 I'm going to press control L, get a new screen here, all right. 95 00:04:51,850 --> 00:04:53,620 So it was just created. 96 00:04:53,620 --> 00:04:55,820 So isEmpty best return true, right? 97 00:04:55,820 --> 00:04:58,540 Because the pezCount comes in, it's set to zero. 98 00:04:58,540 --> 00:04:59,477 So let's do that. 99 00:05:01,440 --> 00:05:04,718 Cool, so it is empty, and then, if we fill it. 100 00:05:07,877 --> 00:05:10,060 So, that's going to set this max_pez on the pezCount. 101 00:05:10,060 --> 00:05:13,000 So now the pezCount should be max_pez, which is 12. 102 00:05:13,000 --> 00:05:16,400 So lets see if pd.isEmpty, and it should return false. 103 00:05:19,670 --> 00:05:21,280 Cool. It's working. 104 00:05:21,280 --> 00:05:24,040 So since we've explored, and we know that it works, 105 00:05:24,040 --> 00:05:28,600 let's write out some code over here in our example class that uses it. 106 00:05:28,600 --> 00:05:33,000 So, let's test out some if statements. 107 00:05:33,000 --> 00:05:35,800 Now remember, if statements allow us to run certain code 108 00:05:35,800 --> 00:05:37,940 only if certain expressions are true. 109 00:05:37,940 --> 00:05:40,300 So, let's just play with it a little bit here. 110 00:05:40,300 --> 00:05:43,062 We'll say, 111 00:05:43,062 --> 00:05:48,085 if dispenser is empty, 112 00:05:48,085 --> 00:05:52,606 then we'll print out 113 00:05:52,606 --> 00:05:57,135 dispenser is empty. 114 00:06:02,655 --> 00:06:06,710 So here, let's show that the dispenser is no longer empty. 115 00:06:06,710 --> 00:06:09,480 So, we want to do the inverse of empty. 116 00:06:09,480 --> 00:06:13,850 Now, we don't need to write that method, because we have the capability to do that. 117 00:06:13,850 --> 00:06:15,640 We can use the not symbol. 118 00:06:15,640 --> 00:06:18,990 Not inverts the expression, or negates the value it follows. 119 00:06:18,990 --> 00:06:20,270 So, we do something like this. 120 00:06:20,270 --> 00:06:27,117 So if an exclamation point is not, 121 00:06:27,117 --> 00:06:33,256 if not the dispenser is empty, 122 00:06:33,256 --> 00:06:38,215 [SOUND] we'll print out, 123 00:06:38,215 --> 00:06:42,239 Dispenser is full. 124 00:06:42,239 --> 00:06:45,884 Cool, so let's drop out of JShell, 125 00:06:45,884 --> 00:06:50,480 and now if we compile and run this, here we go. 126 00:06:50,480 --> 00:06:51,940 So we see the dispenser is empty and 127 00:06:51,940 --> 00:06:55,710 we know that that happened because this will only print out if it was empty. 128 00:06:55,710 --> 00:06:59,550 And then, this will only print out if it's full, and on both sides it prints out. 129 00:06:59,550 --> 00:07:00,050 We did it. 130 00:07:01,870 --> 00:07:03,900 Awesome, way to be proactive. 131 00:07:03,900 --> 00:07:07,180 Did you see how by exposing that most likely common request about 132 00:07:07,180 --> 00:07:10,160 the state of our object, we have avoided the need for us and 133 00:07:10,160 --> 00:07:14,332 other developers to write that exact same equality check code again and again? 134 00:07:14,332 --> 00:07:17,760 We encapsulated how the check happens and 135 00:07:17,760 --> 00:07:21,340 we have avoided having to actually show how many Pez were available. 136 00:07:21,340 --> 00:07:23,940 Chalk that up as another added benefit to using objects. 137 00:07:25,090 --> 00:07:27,840 If you look at the code in example dot Java, 138 00:07:27,840 --> 00:07:30,430 you see how much it reads almost like English. 139 00:07:30,430 --> 00:07:33,170 If the dispenser is empty, fill it. 140 00:07:33,170 --> 00:07:37,540 In my opinion, this is the key to being a successful programmer. 141 00:07:37,540 --> 00:07:40,570 The more you can write your code so that someone can just look at it and 142 00:07:40,570 --> 00:07:44,910 understand what it's doing, the more you are mastering the art of programming. 143 00:07:44,910 --> 00:07:48,610 This will become increasingly more natural the more you practice. 144 00:07:48,610 --> 00:07:50,140 And the more you get to know your object. 145 00:07:50,140 --> 00:07:53,100 And you witness how it's being used, the more you'll be able to make 146 00:07:53,100 --> 00:07:55,770 sure that the methods you provide are clear and readable. 147 00:07:56,890 --> 00:08:00,760 The other important thing that I'd really like to emphasize here is this. 148 00:08:00,760 --> 00:08:03,970 Try not to take offense if someone doesn't understand what your code is 149 00:08:03,970 --> 00:08:05,510 trying to accomplish. 150 00:08:05,510 --> 00:08:08,330 Always try to take that as an opportunity to further 151 00:08:08,330 --> 00:08:10,145 improve your code for readability. 152 00:08:10,145 --> 00:08:11,130 Now, chances are, 153 00:08:11,130 --> 00:08:15,600 if that person didn't understand your code, others might have the same problem. 154 00:08:15,600 --> 00:08:19,280 After all, the main reason that we're writing code is to express our thoughts. 155 00:08:19,280 --> 00:08:21,640 We definitely want them to be understood. 156 00:08:21,640 --> 00:08:25,500 The more legible your code is, the more it can be reused and maintained. 157 00:08:25,500 --> 00:08:27,050 Always strive for clarity. 158 00:08:27,050 --> 00:08:30,050 The time spent is well worth it, I promise. 159 00:08:30,050 --> 00:08:31,690 All right, so after this next exercise, 160 00:08:31,690 --> 00:08:33,600 let's start getting some candy out of this.