1 00:00:00,240 --> 00:00:03,570 The constructor's the first thing that runs as soon as you create a new object. 2 00:00:06,720 --> 00:00:10,530 So it should be the one to update the fields of a newly created object. 3 00:00:13,870 --> 00:00:15,820 In this lesson, you're going to fix your constructor. 4 00:00:19,630 --> 00:00:22,870 Right now, we're calling the Senate to update the cars field, remove it. 5 00:00:25,940 --> 00:00:30,290 Because when you create a new object, it's the constructor's job to update every field. 6 00:00:33,080 --> 00:00:36,800 The dealership only has one field, so the constructor needs to receive one parameter. 7 00:00:40,560 --> 00:00:45,990 An array of car objects and instead of a fixed length of three, the field is going to share the same 8 00:00:45,990 --> 00:00:48,360 length as the parameter that gets passed in. 9 00:00:56,030 --> 00:01:00,950 All right, so back in Maine, you need to pass in one argument that wage Albino's to call the constructor 10 00:01:00,950 --> 00:01:02,000 with one parameter. 11 00:01:05,550 --> 00:01:08,100 And the argument we're going to pass on is the cars are Ray. 12 00:01:14,920 --> 00:01:18,790 And before testing the code, I'm going to add two more cars to the array being Pastan. 13 00:01:25,190 --> 00:01:26,300 It's going to be a Honda. 14 00:01:32,430 --> 00:01:35,240 Worth 7000 made in 2019. 15 00:01:37,120 --> 00:01:41,230 A color of orange, and it's going to come with the standard tires and filter. 16 00:01:43,700 --> 00:01:46,730 The last car is going to be a Mercedes worth twelve thousand. 17 00:01:52,820 --> 00:01:55,820 Made in 2015, color of black. 18 00:01:57,280 --> 00:02:01,330 And it's going to come with tires, filters, and that's put in a transmission. 19 00:02:07,540 --> 00:02:08,060 All right. 20 00:02:08,080 --> 00:02:12,910 And feel free to add as many car objects as you'd like, but I'm going to go ahead and print the dealership 21 00:02:12,910 --> 00:02:13,480 object. 22 00:02:45,670 --> 00:02:49,330 All right, there are five spots in the cars field and all of them are empty. 23 00:02:52,770 --> 00:02:57,750 This makes sense because so far we're only set the length of the Crisfield, we still have an updated 24 00:02:57,750 --> 00:03:01,860 every element, so every element along the array is going to be no. 25 00:03:02,860 --> 00:03:07,720 OK, so the next step is to update every element in the cars filled with an object from the car's parameter. 26 00:03:11,540 --> 00:03:15,020 So create a fault that runs through the length of the car's perimeter. 27 00:03:15,470 --> 00:03:20,000 We can use the fawry shortcut and it creates a far loop that runs through the length of the nearest 28 00:03:20,000 --> 00:03:20,370 array. 29 00:03:21,080 --> 00:03:24,320 And now the cars field and cars parameter share the same length. 30 00:03:24,890 --> 00:03:28,220 So using this for a loop, it can index every element in the car's field. 31 00:03:35,800 --> 00:03:38,980 And at this point, careful not to fall into the reference trap. 32 00:03:40,720 --> 00:03:44,380 Said every element in the cars field equal to a new car object. 33 00:03:48,210 --> 00:03:53,940 In each new car, object is going to copy its values from a source and this source is going to be the 34 00:03:53,940 --> 00:03:56,550 object that we're indexing from the car's parameter. 35 00:03:58,620 --> 00:04:00,480 And that's it, you're ready to run your code. 36 00:04:13,980 --> 00:04:14,970 And that's perfect. 37 00:04:15,000 --> 00:04:19,740 There are five spots in the cars field and the constructor sets every element in the cars field equal 38 00:04:19,740 --> 00:04:21,750 to an object from the car's parameter. 39 00:04:29,610 --> 00:04:31,410 Why not use a raise a copy of. 40 00:04:33,010 --> 00:04:36,550 You should not use a raised copy of for an array of objects. 41 00:04:39,240 --> 00:04:43,110 A radar copy of Shallow Copy is the reference from every element. 42 00:04:51,390 --> 00:04:55,440 So elements from both arrays are going to share a reference to the same object. 43 00:04:57,230 --> 00:05:01,370 In other words, this would be the same thing as writing this. 44 00:05:03,800 --> 00:05:05,960 Which means would be falling into a trap to. 45 00:05:07,700 --> 00:05:11,570 Instead, use a loop that deep copies the object from every element. 46 00:05:21,100 --> 00:05:25,670 So be careful, a, that copy of is going to shallow copy the reference of every element. 47 00:05:26,110 --> 00:05:30,100 However, the loop deep copies the object from every element. 48 00:05:33,050 --> 00:05:35,360 In this lesson, you fix the dealership constructor. 49 00:05:35,420 --> 00:05:40,610 At first we were using the center to update the newly created dealership object, but when you create 50 00:05:40,610 --> 00:05:44,630 a new object, it's the constructors job to run and update your fields. 51 00:05:45,290 --> 00:05:50,030 In this case, the constructor sets every element in the car's field equal to an object from the car's 52 00:05:50,030 --> 00:05:50,630 parameter.