0:00:00.050,0:00:03.139 [Music] 0:00:04.160,0:00:08.400 in this exercise we're asked to create a 0:00:06.560,0:00:10.000 my car class and implement some 0:00:08.400,0:00:11.840 functionality in it 0:00:10.000,0:00:13.920 so you'll see here that i've broken down 0:00:11.840,0:00:15.120 the instructions and the different steps 0:00:13.920,0:00:16.880 and even though you may not need to do 0:00:15.120,0:00:18.400 this visually it's a good idea to do it 0:00:16.880,0:00:19.680 as you follow 0:00:18.400,0:00:21.840 instructions that are a little bit more 0:00:19.680,0:00:23.920 involved so the first sign 0:00:21.840,0:00:25.279 the first line just says create a class 0:00:23.920,0:00:28.560 called my car 0:00:25.279,0:00:30.800 so we'll do that my car end 0:00:28.560,0:00:32.719 all right the next step says when you 0:00:30.800,0:00:33.840 initialize a new instance or object of 0:00:32.719,0:00:35.920 the class 0:00:33.840,0:00:37.760 allow the user to define some instance 0:00:35.920,0:00:40.719 variables that tell us the year 0:00:37.760,0:00:41.520 model and color of the car all right so 0:00:40.719,0:00:44.960 let's 0:00:41.520,0:00:46.800 write the initialize method and it says 0:00:44.960,0:00:47.840 we need to allow the user to determine 0:00:46.800,0:00:50.960 three things 0:00:47.840,0:00:52.640 the year the model and the color 0:00:50.960,0:00:54.000 so we'll pass those as arguments to the 0:00:52.640,0:00:55.920 initialize method 0:00:54.000,0:00:57.039 and then we'll assign whatever the unit 0:00:55.920,0:01:00.320 user gives us 0:00:57.039,0:01:03.440 to instance variables of the same name 0:01:00.320,0:01:06.720 so year equals year model equals 0:01:03.440,0:01:08.240 model color equals color 0:01:06.720,0:01:10.720 all right so we did the first two things 0:01:08.240,0:01:12.720 there the third one says 0:01:10.720,0:01:14.799 create an instance variable that is set 0:01:12.720,0:01:16.560 to zero during instantiation of the 0:01:14.799,0:01:17.600 object to track the current speed of the 0:01:16.560,0:01:20.240 car as well 0:01:17.600,0:01:22.000 okay so that means we need an instance 0:01:20.240,0:01:23.680 variable called current speed 0:01:22.000,0:01:25.600 and instead of allowing the user to 0:01:23.680,0:01:28.560 determine that value by 0:01:25.600,0:01:29.280 passing in another argument we just set 0:01:28.560,0:01:30.720 it to zero 0:01:29.280,0:01:32.560 because when you create a car it's not 0:01:30.720,0:01:34.479 moving hopefully 0:01:32.560,0:01:36.320 all right so before we go into the last 0:01:34.479,0:01:39.040 part about instance methods 0:01:36.320,0:01:40.479 let's just make sure this works so far 0:01:39.040,0:01:44.320 so let's create a new 0:01:40.479,0:01:48.079 object say my first car 0:01:44.320,0:01:51.200 car equals and we'll use the class name 0:01:48.079,0:01:53.040 my car class method new and now we need 0:01:51.200,0:01:56.399 to pass in three arguments 0:01:53.040,0:02:00.240 so let's do a 1997 0:01:56.399,0:02:02.799 chevy lumina and white 0:02:00.240,0:02:03.439 we can actually just rename this to 0:02:02.799,0:02:05.840 lumina 0:02:03.439,0:02:07.040 for short keep track of it alright so 0:02:05.840,0:02:10.080 now 0:02:07.040,0:02:12.160 when we output this object 0:02:10.080,0:02:13.360 and we want to make sure this returns a 0:02:12.160,0:02:15.200 my car object i'm going to make sure 0:02:13.360,0:02:16.720 this runs the errors so let's just test 0:02:15.200,0:02:20.080 this really quick 0:02:16.720,0:02:22.319 so right ruby rb 0:02:20.080,0:02:24.239 and great that's what we get so we get a 0:02:22.319,0:02:26.080 my car object and we can even see the 0:02:24.239,0:02:28.480 year the model the color 0:02:26.080,0:02:29.200 and the current speed so this looks 0:02:28.480,0:02:30.879 great 0:02:29.200,0:02:32.800 now let's add that functionality that 0:02:30.879,0:02:34.400 they asked for on the last line 0:02:32.800,0:02:36.160 create instance methods that allow the 0:02:34.400,0:02:38.959 car to speed up brake 0:02:36.160,0:02:39.680 and shut the car off alright so let's 0:02:38.959,0:02:42.640 call those 0:02:39.680,0:02:42.640 speed up 0:02:43.040,0:02:50.560 brake and 0:02:46.480,0:02:53.519 shut down and let's also add another 0:02:50.560,0:02:55.680 method called current speed just to 0:02:53.519,0:02:58.319 really round out our functionality here 0:02:55.680,0:02:59.120 because here this allows us to go faster 0:02:58.319,0:03:00.720 slower 0:02:59.120,0:03:01.760 and turn it off but what if we just want 0:03:00.720,0:03:03.360 to know how fast we're going in the 0:03:01.760,0:03:05.120 first place 0:03:03.360,0:03:06.640 so let's go through each of these and 0:03:05.120,0:03:09.280 add in the functionality 0:03:06.640,0:03:10.239 so speed up we also want this to have a 0:03:09.280,0:03:12.080 parameter 0:03:10.239,0:03:13.760 so the user can determine how much they 0:03:12.080,0:03:17.760 want to speed up 0:03:13.760,0:03:20.400 and this will affect the current speed 0:03:17.760,0:03:22.319 so we want to increase it by that number 0:03:20.400,0:03:25.840 and then we can just output 0:03:22.319,0:03:30.959 what happened so you push 0:03:25.840,0:03:34.400 the gas pedal and accelerate 0:03:30.959,0:03:37.440 whatever that amount was miles per hour 0:03:34.400,0:03:41.200 okay let's make sure this works 0:03:37.440,0:03:44.319 let's say we want the lumina to speed up 0:03:41.200,0:03:47.840 by 20 miles per hour 0:03:44.319,0:03:47.840 so let's run our code again 0:03:48.560,0:03:51.840 all right great you push the gas pedal 0:03:50.319,0:03:54.000 and accelerate 20 miles per hour 0:03:51.840,0:03:56.000 it's exactly what we're going to see all 0:03:54.000,0:03:57.920 right so now let's implement the same 0:03:56.000,0:03:59.680 the same functionality but the opposite 0:03:57.920,0:04:02.080 for the break 0:03:59.680,0:04:03.200 so now we want to still affect the 0:04:02.080,0:04:05.040 current speed 0:04:03.200,0:04:06.720 but now we want it to go down the amount 0:04:05.040,0:04:08.080 that we're given we need to add that as 0:04:06.720,0:04:11.200 a parameter 0:04:08.080,0:04:14.319 as well and we still want to tell 0:04:11.200,0:04:19.759 people what's going on so you push the 0:04:14.319,0:04:19.759 brake and de-sell or decelerate 0:04:20.479,0:04:27.040 that many miles per hour okay 0:04:23.680,0:04:29.680 let's just change this to break 0:04:27.040,0:04:31.280 and see what that does so we'll run this 0:04:29.680,0:04:32.479 again 0:04:31.280,0:04:34.160 all right you push the brake and 0:04:32.479,0:04:36.639 decelerate 20 miles per hour that's 0:04:34.160,0:04:39.440 working great 0:04:36.639,0:04:40.160 let's go to shut down now when you shut 0:04:39.440,0:04:43.680 down the car 0:04:40.160,0:04:45.040 your current speed is zero so we want to 0:04:43.680,0:04:48.800 update that 0:04:45.040,0:04:50.240 and let's just say let's park this bad 0:04:48.800,0:04:53.360 boy 0:04:50.240,0:04:54.320 alright so when we do that i should 0:04:53.360,0:04:59.040 write 0:04:54.320,0:05:01.600 shut down okay let's see 0:04:59.040,0:05:02.240 run this again and let's practice bad 0:05:01.600,0:05:03.680 boy great 0:05:02.240,0:05:04.960 now this time we're not outputting the 0:05:03.680,0:05:06.560 current speed we're going to leave that 0:05:04.960,0:05:09.360 to the current speed method 0:05:06.560,0:05:11.039 so we'll do that now so in the current 0:05:09.360,0:05:12.720 speed we don't need to update anything 0:05:11.039,0:05:14.320 we just need to output what the current 0:05:12.720,0:05:17.840 speed is 0:05:14.320,0:05:21.120 so we'll have it say puts you are now 0:05:17.840,0:05:23.199 going and we want to access the 0:05:21.120,0:05:24.400 current speed instance variable so let's 0:05:23.199,0:05:27.680 use the at and then 0:05:24.400,0:05:32.479 current speed miles per hour 0:05:27.680,0:05:34.160 okay so let's test that 0:05:32.479,0:05:35.680 current speed but let's make sure we're 0:05:34.160,0:05:39.199 actually updating it so let's say 0:05:35.680,0:05:42.160 lumina dot uh speed up 0:05:39.199,0:05:43.600 and we'll go 25 so this should tell us 0:05:42.160,0:05:45.440 we're accelerating and then this should 0:05:43.600,0:05:46.800 just tell us that we're still going 25 0:05:45.440,0:05:47.919 miles per hour because that's all we've 0:05:46.800,0:05:51.360 changed 0:05:47.919,0:05:52.639 so let's run this again 0:05:51.360,0:05:54.720 all right you push the gas pedal and 0:05:52.639,0:05:55.759 accelerate 25 miles per hour that's from 0:05:54.720,0:05:57.919 the speed up method 0:05:55.759,0:06:00.639 and then you are now going 25 miles per 0:05:57.919,0:06:02.319 hour that's the current speed method 0:06:00.639,0:06:05.280 great so let's make this just a little 0:06:02.319,0:06:08.720 more complicated so say lumina dot 0:06:05.280,0:06:10.479 break and let's take it down 15. so then 0:06:08.720,0:06:13.440 now our current speed should be 0:06:10.479,0:06:16.240 10 right because 25 minus 15 is 10. 0:06:13.440,0:06:16.240 let's run this again 0:06:16.960,0:06:23.759 okay 25 then 15 and then 10 great 0:06:20.720,0:06:27.440 and just to bring it full circle 0:06:23.759,0:06:30.800 let's shut her down at the end 0:06:27.440,0:06:33.840 and run that okay 0:06:30.800,0:06:36.800 so we sped up to 25 went down 0:06:33.840,0:06:37.759 15 miles per hour and then we won 10 and 0:06:36.800,0:06:40.400 then we went 0:06:37.759,0:06:42.319 zero now let's make sure just that our 0:06:40.400,0:06:45.280 current speed really does go down to 0:06:42.319,0:06:48.240 zero after we shut down so let's run 0:06:45.280,0:06:50.400 this one last time 0:06:48.240,0:06:51.440 so after we shut down it really is zero 0:06:50.400,0:06:54.000 miles per hour 0:06:51.440,0:06:54.880 and that is updated appropriately all 0:06:54.000,0:06:57.120 right there you have it 0:06:54.880,0:07:03.840 there's all of our functionality for our 0:06:57.120,0:07:03.840 my car class 0:07:03.919,0:07:06.000 you