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,140 --> 00:00:13,510 Welcome to Workbook 7.6. 4 00:00:13,510 --> 00:00:22,930 Task one is to initialize the passport whenever a new object is created so we can say this passport 5 00:00:22,960 --> 00:00:27,520 is equal to a new array that can hold three elements. 6 00:00:28,680 --> 00:00:31,050 Task two is to add the getter get passport. 7 00:00:31,050 --> 00:00:33,090 So here we can say public. 8 00:00:33,300 --> 00:00:39,520 We're going to be returning an array of string values when an object calls. 9 00:00:39,540 --> 00:00:40,920 Gut passport. 10 00:00:41,280 --> 00:00:46,770 And we're going to return arrays dot copy of. 11 00:00:47,290 --> 00:00:53,890 We will return a copy of the passport array and we're going to copy over the full length of the passport 12 00:00:53,890 --> 00:00:54,570 array. 13 00:00:54,580 --> 00:00:56,050 And that should be on. 14 00:00:56,410 --> 00:01:01,020 Task number three is to add this print line function at the end of your main method. 15 00:01:01,030 --> 00:01:07,990 Simple enough, we just have to fill in the placeholders where necessary at the end. 16 00:01:07,990 --> 00:01:09,550 So we'll do it here. 17 00:01:11,130 --> 00:01:12,870 Fix up the spacing. 18 00:01:13,050 --> 00:01:14,640 So here we call. 19 00:01:14,970 --> 00:01:18,450 Person name or get name, I should say. 20 00:01:19,500 --> 00:01:20,320 Here we call. 21 00:01:20,340 --> 00:01:23,460 Person dot get nationality. 22 00:01:24,660 --> 00:01:26,490 Fix up the spacing again. 23 00:01:27,520 --> 00:01:28,720 Here we call. 24 00:01:29,140 --> 00:01:31,900 Person dot get date of birth. 25 00:01:36,440 --> 00:01:36,980 Person. 26 00:01:36,980 --> 00:01:38,630 Don't get seat number. 27 00:01:41,170 --> 00:01:44,560 And for the passport we need to print, it's two string. 28 00:01:44,650 --> 00:01:50,620 So arrays dot to string, person, dot, get passport. 29 00:01:51,370 --> 00:01:52,450 All right. 30 00:01:53,990 --> 00:01:56,030 Your output should appear as follows. 31 00:01:56,030 --> 00:01:58,910 Let's go ahead and visualize the runtime. 32 00:02:00,640 --> 00:02:06,610 I'm only going to put a breakpoint over here because we have visualized the other getters countless 33 00:02:06,610 --> 00:02:07,630 times before. 34 00:02:10,229 --> 00:02:12,810 Here, we're creating a new object of the person class. 35 00:02:12,810 --> 00:02:18,240 And because we are passing in four arguments, it's going to call the constructor that expects to receive 36 00:02:18,240 --> 00:02:19,470 four parameters. 37 00:02:19,860 --> 00:02:22,310 Here you can visualize the parameter values. 38 00:02:22,320 --> 00:02:26,370 This points to the current object that was just created. 39 00:02:26,670 --> 00:02:30,420 And here we set this name equal to the parameter. 40 00:02:32,140 --> 00:02:36,490 Here we set this nationality equal to the nationality parameter. 41 00:02:37,600 --> 00:02:37,840 Here. 42 00:02:37,840 --> 00:02:42,090 We set this date of birth equal to the date of birth that was passed in. 43 00:02:42,100 --> 00:02:45,250 And this passport does not require a parameter. 44 00:02:45,250 --> 00:02:53,200 H is going to be set equal to a new array that can hold three elements, and this seat number will be 45 00:02:53,200 --> 00:02:55,750 set equal to the seat number that was passed in. 46 00:02:55,990 --> 00:03:03,280 All right, The constructor is done updating the objects as fields, and at this point the person variable 47 00:03:03,280 --> 00:03:07,060 stores a reference to the object that we just created. 48 00:03:07,600 --> 00:03:09,550 We're not dealing with this right now. 49 00:03:10,330 --> 00:03:12,910 And you'll remember we have a breakpoint at person. 50 00:03:13,150 --> 00:03:13,960 Get passport. 51 00:03:13,960 --> 00:03:15,910 So let's just continue to it. 52 00:03:16,660 --> 00:03:22,630 And so the passport field, ultimately it stores a reference that points to the array. 53 00:03:23,820 --> 00:03:25,920 We don't want to return the same reference. 54 00:03:25,920 --> 00:03:27,100 26. 55 00:03:27,120 --> 00:03:33,570 If we were to say return passport, all it's going to do is return a reference to the same array. 56 00:03:33,570 --> 00:03:34,590 26. 57 00:03:37,650 --> 00:03:41,040 But in this case, we're returning a copy of the array. 58 00:03:43,150 --> 00:03:48,660 So here you can see get passport returns, another reference for 37 to a brand new array. 59 00:03:48,670 --> 00:03:51,540 But ultimately it's just a copy of this array. 60 00:03:51,550 --> 00:03:52,840 So null. 61 00:03:54,650 --> 00:03:54,920 Here. 62 00:03:54,920 --> 00:04:00,320 We printed all of the fields values, including the passport, which at the moment is just null. 63 00:04:02,410 --> 00:04:07,330 And so task number four is telling us our code contains a set or set password. 64 00:04:07,360 --> 00:04:11,100 Notice it doesn't have a parameter inside set passport. 65 00:04:11,110 --> 00:04:16,140 We set the passport equal to the name, nationality and date of birth fields. 66 00:04:16,149 --> 00:04:20,470 So here inside of set passport, you can do one of two things. 67 00:04:20,470 --> 00:04:24,700 You can say Passport at index zero is equal to the name. 68 00:04:24,700 --> 00:04:34,780 Passport and index one is equal to the nationality and passport at index two is equal to the date of 69 00:04:34,780 --> 00:04:35,440 birth. 70 00:04:36,850 --> 00:04:39,540 You could have also done this in a single line. 71 00:04:39,550 --> 00:04:41,110 It's totally up to you. 72 00:04:41,110 --> 00:04:48,340 So here you could have just set passport equal to a new string array that stores the name, nationality 73 00:04:48,340 --> 00:04:49,390 and date of birth. 74 00:04:49,600 --> 00:04:53,140 But I'm going to stick to what we have over here. 75 00:04:55,010 --> 00:04:58,910 And now we call the setter if the person gets approved for a passport. 76 00:04:58,940 --> 00:05:01,090 Otherwise, we just don't do anything. 77 00:05:01,100 --> 00:05:07,970 So if person got apply for passport is true, we are going to call person dot set passport. 78 00:05:09,540 --> 00:05:12,000 Okay, let us visualize the runtime. 79 00:05:14,480 --> 00:05:20,390 Here we create a new object of the person class calling in the constructor that expects to receive four 80 00:05:20,390 --> 00:05:21,320 values. 81 00:05:21,500 --> 00:05:23,000 The constructor gets called. 82 00:05:23,000 --> 00:05:25,870 It receives every single value as a parameter. 83 00:05:25,880 --> 00:05:28,130 We visualize the parameters over here. 84 00:05:28,160 --> 00:05:34,050 This point to the current object that was just created, the one that's calling this constructor. 85 00:05:34,070 --> 00:05:38,550 Here we set this name equal to the name value that we passed in. 86 00:05:38,570 --> 00:05:43,840 Here we set this nationality equal to the nationality that was passed in date of birth. 87 00:05:43,850 --> 00:05:50,420 We set passport equal to a new array that can store three elements, and here we set the seat number 88 00:05:50,420 --> 00:05:51,710 equal to five. 89 00:05:52,100 --> 00:05:53,780 All right, we're done. 90 00:05:53,780 --> 00:05:58,670 Initializing our object person stores a reference to that object. 91 00:05:58,700 --> 00:06:00,970 Here we apply for a passport. 92 00:06:00,980 --> 00:06:03,590 Let's hope the application returns true. 93 00:06:04,750 --> 00:06:05,440 It doesn't. 94 00:06:05,440 --> 00:06:06,220 So you know what? 95 00:06:06,220 --> 00:06:08,250 Let me just restart the runtime. 96 00:06:08,260 --> 00:06:13,180 Skip all this, we create a new object again, going inside of apply for passport. 97 00:06:13,180 --> 00:06:14,830 Does this return true again? 98 00:06:14,830 --> 00:06:15,720 It does not. 99 00:06:15,730 --> 00:06:17,800 So let me just skip ahead. 100 00:06:19,080 --> 00:06:25,710 Finally, now we accept their passport application, which means person not set passport is going to 101 00:06:25,710 --> 00:06:26,340 get called. 102 00:06:26,340 --> 00:06:28,620 We step inside of set passport. 103 00:06:29,820 --> 00:06:34,410 This points to the current object that called set passport. 104 00:06:35,070 --> 00:06:37,480 At the moment it has a bunch of null values. 105 00:06:37,500 --> 00:06:38,280 That's fine. 106 00:06:38,280 --> 00:06:41,370 We're going to set the element at index zero equal to the name. 107 00:06:42,220 --> 00:06:47,530 We're going to set the element that index one equal to the nationality and the element that index to 108 00:06:47,530 --> 00:06:49,060 equal to the date of birth. 109 00:06:49,090 --> 00:06:49,960 All right. 110 00:06:49,960 --> 00:06:50,980 Looking good. 111 00:06:51,430 --> 00:06:54,820 So now our person object has been fully updated. 112 00:06:54,820 --> 00:06:58,600 Their passport was accepted and updated accordingly. 113 00:06:58,630 --> 00:07:02,410 I'm just going to continue to the next breakpoint, get passport. 114 00:07:02,800 --> 00:07:08,710 And so in order to protect our field, instead of returning a reference to the same array. 115 00:07:12,420 --> 00:07:16,260 What we're going to do instead is return a copy of the array. 116 00:07:17,960 --> 00:07:24,740 So here you can see that the method got passport returns, a brand new reference to a copy of the array. 117 00:07:24,740 --> 00:07:30,290 And ultimately we're just printing all of the field values and we are golden. 118 00:07:31,540 --> 00:07:36,250 Upon getting approved for a passport, their passport gets initialized. 119 00:07:37,280 --> 00:07:37,730 All right. 120 00:07:37,730 --> 00:07:42,610 And in the event that their passport gets denied, we should get the following values. 121 00:07:42,620 --> 00:07:46,250 Let's just run it using Java C Main Java. 122 00:07:47,180 --> 00:07:48,390 Java Main. 123 00:07:49,620 --> 00:07:51,420 Once again, we get approved. 124 00:07:51,450 --> 00:07:54,190 Let's just keep running it until we get denied. 125 00:07:54,210 --> 00:07:59,520 In this case, this never gets called, which means we never update our passport field. 126 00:07:59,520 --> 00:08:02,640 It stays null and then gets printed. 127 00:08:02,670 --> 00:08:05,340 I hope you enjoyed this breakpoint session.