1 00:00:00,210 --> 00:00:03,630 The solution is going to cover part three where we start setting up the game class. 2 00:00:05,320 --> 00:00:10,090 We'll start by defining the scoreboard field, a scoreboard needs to monitor the score of each team. 3 00:00:10,420 --> 00:00:14,670 So we're going to use a Hashmat because there's parity between team and integer data. 4 00:00:15,220 --> 00:00:16,360 Private Hashmat. 5 00:00:17,530 --> 00:00:22,180 The Hashmat is going to store team manager key value entries and it's going to be called scoreboard. 6 00:00:30,450 --> 00:00:34,830 The next step is to create a constructor, the constructor runs as soon as we create an object of the 7 00:00:34,830 --> 00:00:40,470 game class and when it runs, it will receive two parameters the home team and the away team. 8 00:00:41,800 --> 00:00:45,970 Now, inside, the constructor will set the scoreboard equal to a new object of the Hashmat class. 9 00:00:58,700 --> 00:01:01,280 And then I'm going to put two entries into the scoreboard. 10 00:01:05,000 --> 00:01:07,310 A home team that starts with a score of zero. 11 00:01:13,300 --> 00:01:19,270 And an away team that starts with a score of zero and now notice that I'm adding copies of each parameter 12 00:01:19,270 --> 00:01:20,050 to the Hashmat. 13 00:01:20,470 --> 00:01:25,720 That's because you don't want your keys to share the same reference as the outside variable that's being 14 00:01:25,720 --> 00:01:26,400 passed in. 15 00:01:26,440 --> 00:01:32,170 Otherwise, the set of each key can be affected if you change the outside variable, which is a security 16 00:01:32,170 --> 00:01:32,570 leak. 17 00:01:33,070 --> 00:01:33,670 You know what? 18 00:01:33,700 --> 00:01:38,440 Why don't I just show you really quickly instead of passing in copies, I'll pass in each parameter 19 00:01:38,440 --> 00:01:42,370 directly and I'll create a new object of the game class using the format. 20 00:01:42,370 --> 00:01:43,720 I left you all and learn the parts. 21 00:01:55,920 --> 00:01:57,400 OK, they're both of type team. 22 00:02:06,420 --> 00:02:09,270 Now we're going to pass into a new object of the game class. 23 00:02:17,570 --> 00:02:18,770 A lot of breakpoint. 24 00:02:27,310 --> 00:02:28,270 Step inside. 25 00:02:39,950 --> 00:02:45,260 And check it out, each key has the same reference as the outside variable that was passed in as a parameter. 26 00:02:52,350 --> 00:02:55,680 If I update one of the objects, I'll change the house name for whom? 27 00:03:03,080 --> 00:03:04,190 Restart the debugger. 28 00:03:14,370 --> 00:03:16,340 The state of the key should get affected as well. 29 00:03:22,250 --> 00:03:22,990 That's not good. 30 00:03:28,270 --> 00:03:33,430 But if the constructor creates copies of each parameter, we can rest assured that each key store is 31 00:03:33,430 --> 00:03:36,010 a unique reference that points to a team copy. 32 00:03:37,790 --> 00:03:42,200 You can feel free to test that out yourself and monitor the references, but I'm going to move on enough 33 00:03:42,200 --> 00:03:42,680 about that. 34 00:03:51,250 --> 00:03:54,490 Now we want to create two getters, we'll start with the Geter get score. 35 00:04:00,840 --> 00:04:02,280 Which receives one parameter. 36 00:04:04,310 --> 00:04:05,930 The team who score you want to get. 37 00:04:10,660 --> 00:04:13,020 We'll get the score for this particular team. 38 00:04:18,209 --> 00:04:24,960 And it's going to get the score value for this particular team or will it we'll come back to this now. 39 00:04:24,970 --> 00:04:27,780 We are a center that updates the score for a particular team. 40 00:04:28,140 --> 00:04:32,850 It's going to be called set score and receive two parameters, the team for which you want to update 41 00:04:32,850 --> 00:04:33,390 the score. 42 00:04:34,950 --> 00:04:36,060 And the score itself. 43 00:04:40,460 --> 00:04:42,170 Then you can say scoreboard put. 44 00:04:45,400 --> 00:04:47,200 The following key value pair. 45 00:04:52,370 --> 00:04:57,140 Now, we're going to quality control this center later, because put either can add this as another 46 00:04:57,140 --> 00:05:01,870 entry in the hash map or it can update the entry that already contains this time object. 47 00:05:02,750 --> 00:05:06,860 Obviously, we want to update because the hash map should only have two entries. 48 00:05:07,100 --> 00:05:08,360 So for now, let's move on. 49 00:05:10,560 --> 00:05:14,220 Now we're going to to get her called get team that receives a parameter string name. 50 00:05:16,940 --> 00:05:19,850 We'll come back to this in the next video for now, just return null. 51 00:05:26,480 --> 00:05:30,230 And now you might be asking, should there be a set team setter, no. 52 00:05:31,550 --> 00:05:37,520 It's impossible to update a key after you add it, you can remove a key and then add another one, but 53 00:05:37,520 --> 00:05:39,210 you cannot change a key. 54 00:05:39,260 --> 00:05:40,370 It's not possible. 55 00:05:42,260 --> 00:05:45,920 All right, we're going to wrap up this video by testing the getters and setters, I'll get the score 56 00:05:45,920 --> 00:05:46,670 for Gryffindor. 57 00:05:53,840 --> 00:05:55,790 I'll set the score for Gryffindor to 10. 58 00:05:59,530 --> 00:06:03,800 Spoiler alert, none of these should work, so here's why I'm going out to break points. 59 00:06:09,810 --> 00:06:11,220 I'll step in to get score. 60 00:06:12,820 --> 00:06:17,860 The parameter we passed in has this reference which doesn't match any of the references in the hash 61 00:06:17,860 --> 00:06:18,160 map. 62 00:06:20,310 --> 00:06:26,160 And unless you define an equals method, Java uses the default equals method to compare to objects, 63 00:06:26,520 --> 00:06:30,660 which is why I can't find anything in the Hashmat that matches the parameter and returns no. 64 00:06:31,630 --> 00:06:32,800 Is that really the issue? 65 00:06:33,580 --> 00:06:34,750 We'll find out soon enough. 66 00:06:39,100 --> 00:06:43,690 Now, what would happen here, once again, the reference being passed and doesn't match any of the 67 00:06:43,690 --> 00:06:44,950 references in the Hashmat. 68 00:06:48,330 --> 00:06:53,460 So instead of updating the entry that already contains this theme object, it's going to think it's 69 00:06:53,460 --> 00:06:57,960 a new key and add another entry to the hash map and that's also not good. 70 00:06:58,590 --> 00:07:02,010 Java's using the default equals method to compare team objects. 71 00:07:02,190 --> 00:07:08,130 So we need to provide our own equals method that allows Java to compare to objects based on their fields, 72 00:07:08,310 --> 00:07:09,540 not the reference. 73 00:07:11,000 --> 00:07:13,100 The equals method needs to return a boolean. 74 00:07:14,240 --> 00:07:15,500 And receives an object. 75 00:07:17,570 --> 00:07:18,870 What's the type of that object? 76 00:07:18,890 --> 00:07:20,850 We don't know, it can be any object. 77 00:07:21,450 --> 00:07:26,060 First, we're going to check if the object that we're comparing against is no, because if it happens 78 00:07:26,060 --> 00:07:29,840 to be null, then there's no way it equals the object that's calling this method. 79 00:07:30,790 --> 00:07:32,060 Let's assume the object, is it? 80 00:07:32,060 --> 00:07:32,320 No. 81 00:07:32,330 --> 00:07:37,880 Then we need to check if it isn't of type team if the object isn't an instance of the team class. 82 00:07:48,790 --> 00:07:53,980 OK, so at this point, the object, is it no, it is an instance of team, so we can typecast it to 83 00:07:53,980 --> 00:07:54,730 type team. 84 00:08:00,140 --> 00:08:04,940 And finally, we need to check if the objects fields are equal to the current object that's calling 85 00:08:04,940 --> 00:08:08,780 this method, we need to compare the house return. 86 00:08:08,780 --> 00:08:10,960 This thought house equals timecode house. 87 00:08:11,270 --> 00:08:12,830 We need to compare the keeper. 88 00:08:17,200 --> 00:08:19,180 This storekeeper equals team goalkeeper. 89 00:08:37,409 --> 00:08:41,909 And how do we compare the array, remember, we can use arrays not to string. 90 00:08:45,500 --> 00:08:50,870 The new string is going to convert the contents of the array into one long string, and so if the two 91 00:08:50,870 --> 00:08:54,590 happened to share the same string, then they must have the same elements. 92 00:09:01,940 --> 00:09:02,590 And that's a. 93 00:09:05,720 --> 00:09:07,130 And so now I guess it should work. 94 00:09:07,280 --> 00:09:08,120 Let's run the code. 95 00:09:17,330 --> 00:09:18,690 And it still doesn't work. 96 00:09:19,020 --> 00:09:20,960 Hmm, well, test out the setar. 97 00:09:32,750 --> 00:09:33,550 Same thing. 98 00:09:36,320 --> 00:09:39,020 I'm pretty sure we defined our equals method perfectly. 99 00:09:39,200 --> 00:09:41,690 It compares to objects based on their fields. 100 00:09:43,010 --> 00:09:46,490 Those two objects, one hundred percent have the same fields. 101 00:09:48,510 --> 00:09:51,030 So why isn't Hashmat able to retrieve the key? 102 00:09:52,950 --> 00:09:57,850 For Hashmat to retrieve a key, it's not enough for an object to be equal to it. 103 00:09:58,050 --> 00:10:00,560 It must also share the same hash code. 104 00:10:01,680 --> 00:10:04,350 You're going to learn more about hash codes in the next video.