1 00:00:00,000 --> 00:00:04,666 [MUSIC] 2 00:00:04,666 --> 00:00:08,670 So far we've only been modeling an object's attributes. 3 00:00:08,670 --> 00:00:12,470 But we also want to define what an object can do. 4 00:00:12,470 --> 00:00:14,455 Remember the Pez dispenser example. 5 00:00:14,455 --> 00:00:18,355 The Pez dispenser has both attributes and behaviors. 6 00:00:18,355 --> 00:00:23,555 Pez dispensers have attributes like color, and how many pieces of Pez are in it. 7 00:00:23,555 --> 00:00:26,035 Points and maps also have attributes. 8 00:00:26,035 --> 00:00:29,145 A point's attributes are its x and y coordinates. 9 00:00:29,145 --> 00:00:33,090 A map's attributes are its width and height dimensions. 10 00:00:33,090 --> 00:00:36,080 The Pez dispenser also has some behaviors. 11 00:00:36,080 --> 00:00:37,450 It can be refilled. 12 00:00:37,450 --> 00:00:39,280 It can dispense Pez. 13 00:00:39,280 --> 00:00:41,630 This is what the Pez dispenser can do. 14 00:00:41,630 --> 00:00:45,110 In code, objects' behaviors are defined using methods. 15 00:00:46,310 --> 00:00:48,890 One of the behaviors we want our map class to have 16 00:00:48,890 --> 00:00:52,550 is the ability to determine if the point is on the map. 17 00:00:52,550 --> 00:00:55,730 The best way to find that out is to ask the map 18 00:00:55,730 --> 00:00:59,570 because the map class is responsible for the map boundaries. 19 00:00:59,570 --> 00:01:02,850 To allow the map to answer this question, we need to write a method. 20 00:01:03,850 --> 00:01:05,850 Let's add some methods to our classes. 21 00:01:07,290 --> 00:01:10,560 I've opened up the Map.cs file in WorkSpaces. 22 00:01:10,560 --> 00:01:14,030 Let's add a method to the map class called OnMap. 23 00:01:14,030 --> 00:01:17,190 The OnMap method will take a point as a parameter and 24 00:01:17,190 --> 00:01:21,200 will return true if the point is on the map and false if it isn't. 25 00:01:22,520 --> 00:01:28,936 I'll say, public bool OnMap (Point point), 26 00:01:28,936 --> 00:01:34,420 open curly brace, closing curly brace. 27 00:01:35,910 --> 00:01:37,750 There we go. All right, 28 00:01:37,750 --> 00:01:40,800 as you can see inside the parentheses of the OnMap method, 29 00:01:40,800 --> 00:01:44,270 we have a parameter called point with a lowercase p. 30 00:01:44,270 --> 00:01:46,370 Which is of type Point with an uppercase P. 31 00:01:47,380 --> 00:01:51,630 This is very similar to how we specified parameters in the constructor. 32 00:01:51,630 --> 00:01:54,880 Only this time, the point isn't one of the types built into C#, 33 00:01:54,880 --> 00:01:57,060 it's one we created ourselves. 34 00:01:58,200 --> 00:02:00,890 This method will return a value. 35 00:02:00,890 --> 00:02:03,330 So we specified the return type as bool here. 36 00:02:04,350 --> 00:02:09,190 It's a Boolean because this method will return only true or false. 37 00:02:09,190 --> 00:02:12,780 Remember, we can write methods that don't return anything. 38 00:02:12,780 --> 00:02:16,080 If that was the case, we would have written void here. 39 00:02:16,080 --> 00:02:17,320 We'll see some of those later. 40 00:02:18,580 --> 00:02:21,090 Finally our method needs to be public, so 41 00:02:21,090 --> 00:02:24,280 that we can call it from outside of the Map class. 42 00:02:24,280 --> 00:02:26,470 Now let's write the body of the method. 43 00:02:26,470 --> 00:02:30,780 Given a point we need to determine if the point is within the bounds of the map. 44 00:02:31,800 --> 00:02:34,050 Let's look at an example map again. 45 00:02:34,050 --> 00:02:37,140 The map has a width of 8 and a height of 5. 46 00:02:37,140 --> 00:02:42,334 So all points on this map need to have an X value that's greater than or 47 00:02:42,334 --> 00:02:44,495 equal to 0 and less than 8. 48 00:02:44,495 --> 00:02:49,235 The Y value must be greater than or equal to 0 and less than 5. 49 00:02:50,510 --> 00:02:54,660 As you can see every point must pass these four conditions 50 00:02:54,660 --> 00:02:56,542 in order to be considered on the map. 51 00:02:56,542 --> 00:02:59,530 In code, let's start off 52 00:02:59,530 --> 00:03:03,870 by declaring the variable called inBounds to hold the return value of our method. 53 00:03:06,220 --> 00:03:10,840 Now we set this variable equal to the result of checking our four conditions. 54 00:03:10,840 --> 00:03:15,517 A point is inBounds if point.x is greater than or 55 00:03:15,517 --> 00:03:20,200 equal to 0, and if point.x is less than Width. 56 00:03:25,410 --> 00:03:28,839 And if point.y is greater than or equal to 0, and if point.y is less than Height. 57 00:03:28,839 --> 00:03:33,430 Finally we'll return the value stored in inBounds as a return value. 58 00:03:34,810 --> 00:03:39,220 See how I put two ampersands in between each of these four conditions? 59 00:03:39,220 --> 00:03:41,770 This is the conditional and operator. 60 00:03:41,770 --> 00:03:44,810 The result of an and operation is true if and 61 00:03:44,810 --> 00:03:50,130 only if both the left and the right side of the and operator is true. 62 00:03:50,130 --> 00:03:55,700 So this expression will only evaluate to true if X greater than 0 is true and 63 00:03:55,700 --> 00:03:57,610 X less than Width is true. 64 00:03:57,610 --> 00:04:03,120 And Y greater than or equal to 0 is true, and Y less than Height this true. 65 00:04:03,120 --> 00:04:05,530 If all four of these conditions are true, 66 00:04:05,530 --> 00:04:08,470 then the entire expression evaluates to true. 67 00:04:08,470 --> 00:04:10,230 And inBounds, will be set to true. 68 00:04:11,280 --> 00:04:14,520 If any of them are false, then inBounds will be false. 69 00:04:14,520 --> 00:04:16,090 That's what we want. 70 00:04:16,090 --> 00:04:19,300 In other words, if any one of these conditions are false, 71 00:04:19,300 --> 00:04:21,800 it means the point is outside the bounds of the map. 72 00:04:22,940 --> 00:04:27,750 Another conditional operator is or, which is written as a double bar. 73 00:04:27,750 --> 00:04:34,250 The result of an or operation is true if either the left or the right side is true. 74 00:04:34,250 --> 00:04:38,590 For example if we wanted to check if the point was out of bounds, 75 00:04:38,590 --> 00:04:40,730 then we could use the or operator like this. 76 00:04:42,730 --> 00:04:47,038 The point is outOfBounds if 77 00:04:47,038 --> 00:04:51,836 point.X is less than 0 or 78 00:04:51,836 --> 00:05:00,640 point.x is greater than or equal to Width. 79 00:05:00,640 --> 00:05:05,055 Or point.y is less than 0, or 80 00:05:05,055 --> 00:05:10,350 point.y is greater than or equal to Height. 81 00:05:13,100 --> 00:05:17,460 Pause the video, and take a minute to compare these two expressions. 82 00:05:17,460 --> 00:05:22,258 This will give you a good idea of how the or operator is different than the and 83 00:05:22,258 --> 00:05:23,032 operator. 84 00:05:27,450 --> 00:05:31,824 Then we could check if the point is inBounds by using the not operator. 85 00:05:36,663 --> 00:05:40,280 The exclamation mark gives us the opposite of a boolean. 86 00:05:41,290 --> 00:05:45,100 It's sometimes called the boolean negation operator. 87 00:05:45,100 --> 00:05:47,350 So after adding the not operator, 88 00:05:47,350 --> 00:05:50,890 if outOfBounds was true, then it becomes false. 89 00:05:50,890 --> 00:05:54,020 And if it was false, then it becomes true. 90 00:05:54,020 --> 00:05:58,054 We could also just negate this entire expression by wrapping it in parentheses 91 00:05:58,054 --> 00:06:00,877 and using the not operator to negate the entire thing. 92 00:06:20,190 --> 00:06:23,860 Now, both these lines are equivalent to each other. 93 00:06:23,860 --> 00:06:30,795 The difference is one's using and, and the other is using or with a not. 94 00:06:30,795 --> 00:06:35,137 You might want to pause the video again to compare these expressions and 95 00:06:35,137 --> 00:06:36,375 see why this works. 96 00:06:38,710 --> 00:06:42,694 In the case of the OnMap method, using the and operator to check of the point is 97 00:06:42,694 --> 00:06:46,017 inBounds is more straightforward, so that's what we'll do. 98 00:06:49,054 --> 00:06:53,050 We'll see more examples of using or, and and not in the future. 99 00:06:53,050 --> 00:06:55,240 But I recommend checking out the teacher's notes for 100 00:06:55,240 --> 00:06:58,590 more information about boolean logic in C#. 101 00:06:58,590 --> 00:07:01,230 Let's take a short break before we continue analyzing 102 00:07:01,230 --> 00:07:02,150 the rest of this method.