1 00:00:00,210 --> 00:00:03,870 Before we start, did you try solving the workbook yourself? 2 00:00:03,900 --> 00:00:09,660 If not, please click the link in the resources folder and follow the instructions for this workbook. 3 00:00:11,000 --> 00:00:15,000 It is bad practice to be able to directly access each field. 4 00:00:15,020 --> 00:00:20,420 What we need to do is make every single field private to the class that it was defined in. 5 00:00:22,630 --> 00:00:28,800 That way, nobody outside of the class can have direct access to these fields. 6 00:00:28,810 --> 00:00:31,030 They are not visible. 7 00:00:31,060 --> 00:00:35,110 The only place where they can be accessed is this class. 8 00:00:35,260 --> 00:00:40,670 And so what we need to do is in task to add getters that return copies of each field value. 9 00:00:40,690 --> 00:00:42,130 So the first getter. 10 00:00:43,060 --> 00:00:44,710 Old get name. 11 00:00:44,830 --> 00:00:49,300 It will return a string copy of the name field. 12 00:00:50,580 --> 00:00:51,900 The second getter. 13 00:00:52,970 --> 00:00:57,350 Will return a string copy of the nationality field. 14 00:00:57,620 --> 00:01:00,680 So here we can return nationality. 15 00:01:02,230 --> 00:01:09,130 The third getter will return a string called Get Date of Birth, and it will be used to get the date 16 00:01:09,130 --> 00:01:09,630 of birth. 17 00:01:09,640 --> 00:01:14,880 It's going to return the string value stored inside of the date of birth field. 18 00:01:14,890 --> 00:01:17,680 And finally, we say public. 19 00:01:18,810 --> 00:01:19,620 Integer. 20 00:01:21,710 --> 00:01:23,060 Get seat number. 21 00:01:23,180 --> 00:01:29,120 And what that will do is return the integer value stored inside of the seat number field. 22 00:01:29,210 --> 00:01:30,230 Return. 23 00:01:31,510 --> 00:01:32,680 Seat number. 24 00:01:34,750 --> 00:01:35,980 All right. 25 00:01:37,390 --> 00:01:44,290 Call the gutters from your print line function and rerun the code so we cannot access the fields directly, 26 00:01:44,290 --> 00:01:49,040 but we can call the getters which return a copy of each field value. 27 00:01:49,060 --> 00:01:50,500 Person get name. 28 00:01:50,500 --> 00:01:50,980 Person. 29 00:01:51,460 --> 00:01:52,480 Nationality. 30 00:01:52,480 --> 00:01:53,560 Person dot. 31 00:01:53,560 --> 00:01:54,820 Get date of birth. 32 00:01:55,210 --> 00:01:56,240 Person dot. 33 00:01:56,290 --> 00:01:57,550 Get seat number. 34 00:01:58,090 --> 00:01:58,960 All right. 35 00:01:58,960 --> 00:02:03,550 We can go ahead and run our code in debug mode. 36 00:02:07,720 --> 00:02:13,090 Here we create a new object of the person class, and the constructor is going to initialize every single 37 00:02:13,090 --> 00:02:14,770 field in that object. 38 00:02:15,160 --> 00:02:19,930 The person variable stores a reference that points to the object we just created. 39 00:02:20,590 --> 00:02:22,420 Here, if I step inside. 40 00:02:22,690 --> 00:02:24,550 It's going to go inside of the string builder. 41 00:02:24,550 --> 00:02:26,140 We don't care about that. 42 00:02:26,230 --> 00:02:29,770 But if I press, step inside again, it's going to go inside of get name. 43 00:02:31,780 --> 00:02:36,010 This points to the current object that's calling get name. 44 00:02:37,600 --> 00:02:42,550 The current object has a field value of Rand Slim, which we are returning. 45 00:02:44,750 --> 00:02:47,990 The get name getter returns ran Slim over here. 46 00:02:48,560 --> 00:02:54,140 What I'll do actually is I will put breakpoints here so that we can just press continue. 47 00:02:57,600 --> 00:03:00,810 So the next getter that we're calling is get nationality. 48 00:03:02,420 --> 00:03:06,080 This points to the current object that's calling this gutter. 49 00:03:06,110 --> 00:03:10,940 The current object has a nationality of Canadian, which we are returning. 50 00:03:16,010 --> 00:03:16,990 All right. 51 00:03:17,000 --> 00:03:20,320 The next method that we're calling is get date of birth. 52 00:03:20,330 --> 00:03:26,810 The current object that's calling get date of birth has the following field value, which we are returning. 53 00:03:28,740 --> 00:03:29,730 All right. 54 00:03:30,300 --> 00:03:34,880 The fourth getter that's being called from the current object is get seat number. 55 00:03:34,890 --> 00:03:40,920 And the current object has a seat number of five, which we are then returning. 56 00:03:42,360 --> 00:03:47,580 And now if I press continue, the runtime will end and it ends up printing every single field value 57 00:03:47,580 --> 00:03:49,020 in our object. 58 00:03:49,260 --> 00:03:49,620 All right. 59 00:03:49,650 --> 00:03:54,420 Task four is to add setters for name, nationality, date of birth, and seat number. 60 00:03:54,960 --> 00:04:00,950 The reason we do that is because we cannot directly access any of the field and then update their values. 61 00:04:00,960 --> 00:04:04,040 We need to update our field values through setters. 62 00:04:04,050 --> 00:04:09,270 So we're going to have one setter that's going to set the name. 63 00:04:09,930 --> 00:04:14,880 It will receive a string parameter name and using the parameter that gets passed in. 64 00:04:17,360 --> 00:04:21,980 It's going to set the name field of the current object equal to the name parameter. 65 00:04:22,370 --> 00:04:24,440 We're going to have another setter. 66 00:04:24,470 --> 00:04:27,290 This one will be called set Nationality. 67 00:04:31,320 --> 00:04:37,560 And here what we do is we set the nationality of the current object that's calling this method equal 68 00:04:37,560 --> 00:04:39,840 to the nationality that gets passed in. 69 00:04:39,990 --> 00:04:41,910 We create another setter. 70 00:04:43,360 --> 00:04:45,340 That receives a date of birth. 71 00:04:45,340 --> 00:04:48,100 So we'll call it set Date of birth. 72 00:04:49,980 --> 00:04:53,220 It will receive a date of birth parameter. 73 00:04:53,520 --> 00:04:58,710 And what it's going to do is set the date of birth of the current object that's calling this method 74 00:04:58,710 --> 00:05:01,290 equal to the date of birth that gets passed in. 75 00:05:01,890 --> 00:05:03,570 And we'll have one more getter. 76 00:05:04,530 --> 00:05:07,200 This one will be called set seat number. 77 00:05:08,200 --> 00:05:10,810 It will receive a seat number parameter. 78 00:05:11,110 --> 00:05:18,100 And what it will do is set the seat number of the current object that's calling this method equal to 79 00:05:18,100 --> 00:05:20,050 the seat number that gets passed in. 80 00:05:21,040 --> 00:05:21,820 All right. 81 00:05:23,910 --> 00:05:29,160 The task number five is the person's current seat number, unfortunately, is taken. 82 00:05:29,160 --> 00:05:34,260 So what we need to do is update their seat number to ten before printing the fields. 83 00:05:34,260 --> 00:05:41,790 So here we can say person, we're going to update that object seat number, set, seat number equal 84 00:05:41,790 --> 00:05:42,630 to ten. 85 00:05:43,170 --> 00:05:46,740 So now what I'm going to do is visualize the runtime. 86 00:05:55,070 --> 00:05:56,240 All right. 87 00:06:00,410 --> 00:06:06,200 So here we're creating a new object of the person class, and we're using our constructor to initialize 88 00:06:06,200 --> 00:06:08,210 all of the objects fields. 89 00:06:09,620 --> 00:06:14,330 So here we have our person variable, which stores a reference to that object. 90 00:06:14,870 --> 00:06:18,710 And now we're going to call a set seat number for that same object. 91 00:06:21,600 --> 00:06:27,930 This points to the current object that's calling this method, and we're setting the seat number of 92 00:06:27,930 --> 00:06:32,460 the current object equal to the seat number that was passed in ten. 93 00:06:39,220 --> 00:06:45,240 All right, Now that we've updated our object to seat number to ten, we can print all of its fields. 94 00:06:45,250 --> 00:06:48,940 So if I press continue, it will continue to the first breakpoint. 95 00:06:48,940 --> 00:06:56,050 Get name here we're calling person, get name this point to the current object that's calling this method 96 00:06:56,050 --> 00:06:59,170 and we're returning a copy of the field value name. 97 00:06:59,650 --> 00:07:04,240 The next method that we're calling from our person object is get nationality. 98 00:07:04,240 --> 00:07:08,260 Once again, this point to the current object that's calling this getter. 99 00:07:08,260 --> 00:07:11,020 Here we're just returning the field value Canadian. 100 00:07:14,170 --> 00:07:16,480 We're also calling the gutter a date of birth. 101 00:07:16,480 --> 00:07:19,970 And in this case, we're going to return the following field value. 102 00:07:19,990 --> 00:07:24,340 And here we're going to return the seat number that we just recently updated. 103 00:07:28,690 --> 00:07:30,190 So here is our object. 104 00:07:30,190 --> 00:07:33,100 And here you can see the field values that we printed. 105 00:07:33,130 --> 00:07:33,640 All right. 106 00:07:33,640 --> 00:07:36,160 I hope you enjoyed this breakpoint session.