1 00:00:00,510 --> 00:00:02,760 The dealership clerk should have a two string method as well. 2 00:00:03,360 --> 00:00:07,530 I showed you that two string makes it easy to print every field and the current object. 3 00:00:10,000 --> 00:00:14,080 When you had to string to your class, Java internally calls it when you're printing the object. 4 00:00:20,020 --> 00:00:24,040 When we print the dealership, it would be nice to display every car in the dealership. 5 00:00:27,110 --> 00:00:30,590 So in this lesson, you're going to have to string method to the dealership class. 6 00:00:32,720 --> 00:00:36,710 Like before, the two string method is going to be public and return to string value. 7 00:00:45,990 --> 00:00:50,670 It's really important that you follow the signature exactly so that Gever recognizes it as the two string 8 00:00:50,670 --> 00:00:51,930 method that it needs to call. 9 00:00:53,580 --> 00:00:57,130 And this method is going to return a strong representation of every car and the dealership. 10 00:00:57,150 --> 00:01:02,790 So first I'm going to set a temporary variable equal to an empty string and I'll return it's. 11 00:01:09,690 --> 00:01:12,750 Then a for loop is going to run through the entire length of the car's array. 12 00:01:13,840 --> 00:01:15,680 You can choose to put this or not. 13 00:01:15,700 --> 00:01:20,440 It doesn't matter, Javin knows that you're referring to the field cars because it's the only thing 14 00:01:20,440 --> 00:01:21,160 within scope. 15 00:01:21,650 --> 00:01:25,570 But you know me, I like to always include this keyword when referring to a field. 16 00:01:27,780 --> 00:01:33,270 And during each run, we're going to update the string to describe every parking spot, so I'm going 17 00:01:33,270 --> 00:01:37,830 to set temp equal to itself, plus the words parking spot. 18 00:01:39,570 --> 00:01:41,940 Followed by the current index that we're running through. 19 00:01:47,760 --> 00:01:50,310 As well as a new line for aesthetic purposes. 20 00:01:52,560 --> 00:01:57,660 Ultimately, this group is going to run three times and each time it's going to append the current parking 21 00:01:57,660 --> 00:01:59,030 spot that we're running through. 22 00:02:09,880 --> 00:02:12,670 For now, I'm going to test this code by printing the dealership object. 23 00:02:24,940 --> 00:02:30,760 And as expected, printing an object automatically calls its two string method, in this case the two 24 00:02:30,760 --> 00:02:35,230 string method for dealership runs and returns a string that describes every parking spot. 25 00:02:41,420 --> 00:02:45,440 And now remember that each car object also has a two string method as well that describes all of its 26 00:02:45,440 --> 00:02:46,010 fields'. 27 00:02:49,300 --> 00:02:55,180 So during each run, right after we describe the current spot, we're going to add the two string value 28 00:02:55,180 --> 00:02:56,970 of the current car that we're running through. 29 00:02:57,640 --> 00:03:03,400 So we're going to update the string variable to include the two string description of each car object. 30 00:03:06,370 --> 00:03:09,190 Once again, I'm also going to add a new line for aesthetic purposes. 31 00:03:12,700 --> 00:03:17,920 So we should expect that during each run, the temp variable is going to update with the parking spot 32 00:03:17,920 --> 00:03:21,460 and the two string description of the car parked at that spot. 33 00:03:22,330 --> 00:03:24,610 It's going to repeat this three times until I restring. 34 00:03:24,610 --> 00:03:28,300 Variable describes every parking spot and the car that's parked in it. 35 00:03:30,050 --> 00:03:31,550 All right, let's run the code. 36 00:03:39,290 --> 00:03:44,450 And we get a no, a exception, but remember that the third object in the car is array is No. 37 00:03:46,820 --> 00:03:51,440 The for loop effectively grabs the string representation of the first and second objects. 38 00:04:01,710 --> 00:04:07,290 However, the third spot in the area is no, and you can't access or call anything from Intel, Java 39 00:04:07,320 --> 00:04:08,190 is going to crash. 40 00:04:12,260 --> 00:04:17,740 So inside the loop, we need to check if an object is null before attempting to use its two string, 41 00:04:18,500 --> 00:04:20,630 if the current element that the loop is running. 42 00:04:20,660 --> 00:04:21,320 There was no. 43 00:04:33,730 --> 00:04:37,300 Then we're going to update the string to say empty for that parking spot. 44 00:04:49,630 --> 00:04:53,950 Otherwise, if there is an object at that element, we can append its two string. 45 00:04:59,870 --> 00:05:01,370 That's the run your code. 46 00:05:20,060 --> 00:05:24,950 And this is great, the first two elements aren't empty, but the one in index two is. 47 00:05:28,650 --> 00:05:33,090 By printing the object, Jova calls the two string method, which returns a string that effectively 48 00:05:33,090 --> 00:05:35,010 displays every car in the array. 49 00:05:39,780 --> 00:05:44,370 All right, we're officially done programming the car and dealership class, there's still one little 50 00:05:44,370 --> 00:05:50,460 thing we have to fix, but in the end we blueprinted every field action and we added the necessary getters, 51 00:05:50,460 --> 00:05:51,680 setters and to strength. 52 00:05:52,380 --> 00:05:55,860 And now we can effectively program using car and dealership objects. 53 00:05:58,370 --> 00:06:02,850 All of these options are going to come together and form a beautiful application once we add user input. 54 00:06:03,290 --> 00:06:05,510 So for now, let's remove all the function calls. 55 00:06:11,280 --> 00:06:15,720 In this lesson, you added a two string method to the dealership class, the tutoring method for dealership 56 00:06:15,720 --> 00:06:18,660 effectively returns every object in the car's array.