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:10,920 --> 00:00:12,600 Welcome to Workbook 7.8. 4 00:00:12,630 --> 00:00:18,000 Task one is to add a constructor that does not receive any parameters inside the airline class. 5 00:00:18,000 --> 00:00:23,940 So we will create a constructor that gets called whenever we create an object of the airline class. 6 00:00:23,940 --> 00:00:30,510 And the constructor will not expect any values because in task number two, inside the constructor, 7 00:00:30,510 --> 00:00:38,250 all we have to do is set people, the people field equal to a new array that can store 11 person objects. 8 00:00:39,270 --> 00:00:46,860 We can say people is equal to a new person array that can store 11 elements. 9 00:00:47,220 --> 00:00:52,950 And although people already points to the people field, let's just say in the future we decide to add 10 00:00:52,950 --> 00:00:54,110 a parameter here. 11 00:00:54,120 --> 00:00:55,620 There would be a conflict. 12 00:00:55,620 --> 00:00:58,650 So let's just add the this keyword anyway. 13 00:00:59,920 --> 00:01:06,880 So task number three, add a getter named get person that receives an index and returns a person object 14 00:01:06,880 --> 00:01:08,650 based on the index. 15 00:01:08,800 --> 00:01:10,930 So public. 16 00:01:12,020 --> 00:01:15,020 We're going to return a single person object. 17 00:01:15,020 --> 00:01:22,940 We call the gutter get person, and we're going to return a person based on the index that gets passed 18 00:01:22,940 --> 00:01:23,630 in here. 19 00:01:23,630 --> 00:01:31,760 We can say person, person is equal to whatever person object that we index from the people's array. 20 00:01:32,240 --> 00:01:36,800 And here we're going to return a new copy of the person object. 21 00:01:40,340 --> 00:01:47,070 So new person creates a new object of the person class because we're only passing in one argument. 22 00:01:47,090 --> 00:01:52,430 The copy constructor is going to get called and it's going to populate the object that we created with 23 00:01:52,430 --> 00:01:54,590 a values from the source object. 24 00:01:56,390 --> 00:02:01,460 Task number four is to add a setter named set person that receives one parameter. 25 00:02:01,460 --> 00:02:02,750 Person person. 26 00:02:03,550 --> 00:02:05,950 The Seder is going to be void. 27 00:02:06,460 --> 00:02:08,509 It will be called set person. 28 00:02:08,530 --> 00:02:10,289 What was the parameter again? 29 00:02:10,300 --> 00:02:12,220 The person itself. 30 00:02:14,700 --> 00:02:17,350 All right, Where are we going with this? 31 00:02:17,370 --> 00:02:23,250 And now consider that the people array contains 11 elements such that we start counting from zero. 32 00:02:23,850 --> 00:02:29,280 The parameter person, it's going to have a seat number that ranges from 1 to 11. 33 00:02:29,280 --> 00:02:35,640 So we need to use that person's seat number to index the people's array, but we need to do that without 34 00:02:35,640 --> 00:02:37,410 stepping out of bounds. 35 00:02:39,570 --> 00:02:46,080 INT index is equal to whatever that person, the seat number is minus one. 36 00:02:46,740 --> 00:02:52,290 Because if the person does seat number happens to be 11, we would want to access the element at index 37 00:02:52,290 --> 00:02:52,860 ten. 38 00:02:52,950 --> 00:02:58,200 If the seat number happens to be one, we would want to access the element at index zero. 39 00:02:58,560 --> 00:02:59,580 All right. 40 00:03:00,510 --> 00:03:06,210 So now we can use this index to access a particular element from the people's array. 41 00:03:07,890 --> 00:03:13,260 And we're going to set that element equal to a new object of the person class. 42 00:03:13,500 --> 00:03:18,420 Not object is going to get all of its values from the person we pass in. 43 00:03:19,840 --> 00:03:22,740 The starter code contains an array of person objects. 44 00:03:22,750 --> 00:03:25,450 It also contains an object of the airline class. 45 00:03:25,480 --> 00:03:26,380 Fair enough. 46 00:03:26,410 --> 00:03:31,990 Use your for loop to populate the airline with objects from the array in main. 47 00:03:33,040 --> 00:03:35,980 So here we can say airline. 48 00:03:36,860 --> 00:03:40,760 Not set person the person at index I. 49 00:03:40,790 --> 00:03:41,390 All right. 50 00:03:41,390 --> 00:03:44,390 Print the objects at indices one, five and ten. 51 00:03:44,390 --> 00:03:48,560 Let's just copy this over and visualize the runtime. 52 00:04:00,800 --> 00:04:08,180 So here we've created an array that can store objects of type person, and this array has 11 person 53 00:04:08,180 --> 00:04:09,140 objects. 54 00:04:10,080 --> 00:04:13,760 If you click the AI icon, you can visualize each object. 55 00:04:13,770 --> 00:04:14,700 All right. 56 00:04:15,270 --> 00:04:21,720 And now what we're going to do next is create an object of the airline class because we're passing in 57 00:04:21,720 --> 00:04:22,710 zero arguments. 58 00:04:22,710 --> 00:04:26,640 It's going to call the constructor that expects zero parameters. 59 00:04:26,820 --> 00:04:33,780 This point to the current object of the airline class that we just created, and here inside the constructor, 60 00:04:33,780 --> 00:04:41,760 we're setting this dot people equal to a new array that can store 11 person objects. 61 00:04:42,960 --> 00:04:43,980 And efforts. 62 00:04:43,980 --> 00:04:47,760 Every single element inside this array starts off as null. 63 00:04:49,040 --> 00:04:55,160 So now we can press continue to the next breakpoint, because in this loop we're going to populate every 64 00:04:55,160 --> 00:04:57,440 single element inside of this field. 65 00:04:57,440 --> 00:05:03,800 Here we call airline that set person passing in the person object at Index zero. 66 00:05:05,470 --> 00:05:11,620 So here is the person object that we passed in Cleopatra, the very first element. 67 00:05:11,920 --> 00:05:16,720 And this points to the airline object that called set person. 68 00:05:16,720 --> 00:05:19,510 Cleopatra has a seat number is one. 69 00:05:19,510 --> 00:05:22,420 But when you're coding, we start counting from zero. 70 00:05:22,420 --> 00:05:25,600 So index is going to equal the seat number minus one. 71 00:05:26,900 --> 00:05:31,340 Which means here we are accessing the element at index zero. 72 00:05:31,670 --> 00:05:37,910 We're setting this equal to a new object of the person class passing in one source object, which is 73 00:05:37,910 --> 00:05:40,190 going to end up calling the copy constructor. 74 00:05:40,220 --> 00:05:46,900 The source object is the object that we passed in this point to the object that we just created. 75 00:05:46,910 --> 00:05:54,530 And here we're populating every single field inside of this object with values from the source. 76 00:06:03,490 --> 00:06:03,940 All right. 77 00:06:03,940 --> 00:06:10,920 At this point, we populated every single field inside of the current object and the element that index 78 00:06:10,960 --> 00:06:14,380 zero is going to store a reference that points to it. 79 00:06:15,070 --> 00:06:15,880 Beautiful. 80 00:06:18,580 --> 00:06:20,710 So that's the first run of our loop. 81 00:06:20,890 --> 00:06:25,120 Let's do this one more time, and then I'm just going to skip to this breakpoint. 82 00:06:26,070 --> 00:06:33,160 So now I equals one, which means airline set person is going to pass in this object. 83 00:06:33,180 --> 00:06:34,830 Alexander the Great. 84 00:06:35,780 --> 00:06:40,040 The object we passed in Alexander the Great has a seat number of two. 85 00:06:40,370 --> 00:06:43,580 So the index is going to be that minus one. 86 00:06:45,430 --> 00:06:51,850 Here, we're setting the element at index one equal to a new object of the person class. 87 00:06:52,150 --> 00:06:57,340 And because we're passing in one argument as we're creating the object, it's going to call the constructor 88 00:06:57,340 --> 00:07:00,070 that expects to receive one parameter. 89 00:07:00,310 --> 00:07:03,830 This points to the object that we just created. 90 00:07:03,850 --> 00:07:10,540 And here all we're doing is we're setting every single field in that object with values from the source. 91 00:07:12,530 --> 00:07:17,390 So at this point, we have populated every single field in the object we just created. 92 00:07:18,170 --> 00:07:23,840 And we set the element at index one equal to a reference to that object. 93 00:07:24,980 --> 00:07:26,110 All right. 94 00:07:26,530 --> 00:07:32,990 Now, I'm not going to keep doing this for the other nine elements, so let's just remove these breakpoints. 95 00:07:33,010 --> 00:07:34,210 Press Continue. 96 00:07:34,780 --> 00:07:39,980 And at this point, all of our airline has been populated with passengers. 97 00:07:40,000 --> 00:07:41,620 We are good here. 98 00:07:41,620 --> 00:07:43,990 We print the objective index one. 99 00:07:43,990 --> 00:07:46,540 So that's going to be Alexander the Great. 100 00:07:47,830 --> 00:07:52,310 All right, Here we present the object at index five. 101 00:07:52,340 --> 00:07:55,010 That's going to be parallelize. 102 00:07:56,160 --> 00:07:59,450 Here we print the object at index ten. 103 00:07:59,460 --> 00:08:02,070 That's going to be arabe. 104 00:08:03,230 --> 00:08:04,130 And we're done. 105 00:08:04,850 --> 00:08:08,350 Let's make sure we have what we're supposed to have. 106 00:08:09,440 --> 00:08:10,250 Beautiful. 107 00:08:10,280 --> 00:08:13,730 I hope you enjoyed once again another breakpoint session.