8.2 Car Part 1

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
4 hours 19 minutes
Difficulty
Beginner
CEU/CPE
4
Video Transcription
00:00
Hi and welcome back to the course. I hope you guys are doing well. And if you remember in the last video lecture, we went over one of the components of old p called inheritance and what it is all about. And in this lab, we are going to be making use of inheritance by creating a program that is based on the idea
00:19
of cars.
00:21
And we will have a few classes that will take advantage of a more general class to demonstrate this idea of inheritance.
00:29
Now, at this point, I'd like you to go ahead and switch over to intelligence.
00:33
And my intelligent over here has four classes. We have a car class and Audi class of walks, value class and then a tester class.
00:42
Now, you can probably tell from the names that we are referring to cars over here and then our tester classes just to test our program in our coat.
00:50
Now, I would like you to go ahead and create these classes by going on file
00:55
on then
00:57
foil, uh, new and then Java class. And here you can type in whatever you want and click. Okay. And you'll be good to go
01:06
once you have act,
01:07
um, you can go ahead and add these comments if you'd like, but they're just a quick guide to what we're going to be doing in our car class.
01:15
So let's begin making this class over here.
01:19
So one of the very first things that we can see is that
01:23
we have a comment that says, Store the speed.
01:26
No,
01:27
we want to use encapsulation in our class and we want to store the speed, but we want to keep it private, and we would like to do the same for the year and the model because there is no need for anyone else to know
01:40
the year speed or model of our car. And using encapsulation generally is a good practice and programming.
01:47
So I'm gonna go ahead and create a private variable called Sir Private integer called Aunt
01:53
and that we're going to do by doing this.
01:57
And then we will do the same for year private into year.
02:02
And finally we would like to store the model version, which is a string so we will say private string
02:08
and then let's call it a model
02:12
and always remember to keep all your names relevance to what you're trying to accomplish
02:16
in your program.
02:19
Okay, so we do not need a constructor for this lab since where you have, we're gonna be creating, get and set methods for these variables over here. So let's get started on the get methods.
02:31
So first, I would like to get the speed
02:36
off my
02:38
car. And I do that by making a get method called get speed and the reason we have to make these get and set methods again just give you a quick reminder is that since these variables air private, in order for us to access these variables outside of the car class, we need to have these get and set methods get methods will allow you to
02:58
get the values or
03:00
the information from these methods, or so from these variables. And the set methods will allow you to set or modify or change whatever you may call it the values of these variables.
03:15
So since I am getting the speed and my return, time is int, I would like to return speed,
03:21
return speed, and we will do the same for a year so public
03:25
into year.
03:28
So you get year and then
03:30
we are going to be returning
03:32
the year, so return
03:36
you're perfect. And let's do the same for
03:39
model so public and string. In this case, since we're returning a string and model is a string, we will name it, get model
03:49
and then returned
03:51
model perfect.
03:53
And now we have our get methods. And since we're done with our get methods, let's move on to our set methods.
04:00
So let's create a, um
04:04
actually, let's go up right here and let's create a set speed methods of public and
04:11
set
04:12
speed.
04:14
And I would like to specify,
04:16
uh, the amount, so I'm going to pass in a variable called speed, which is
04:21
the speed that I want to set for my car.
04:25
So then I'm going to say
04:28
speed
04:30
or this starts speed, which is referring to speed and this class speed very well in this class to speed.
04:39
And then I will go ahead and
04:42
do the same for and sort. This is going to be a void method. Since we aren't returning anything, we're actually setting something,
04:49
and then I'm going to do the same for get year and get model. So forget year what we would like to do is, um,
04:58
create another segmented,
05:00
so public
05:02
void. Since we're not returning anything
05:06
set year
05:09
and then in here, what we can do is you can say, in
05:15
year
05:16
on over here, we can say,
05:18
um, this start
05:20
year,
05:23
he close.
05:24
Yeah,
05:25
perfect.
05:26
And let's also do that for model
05:29
so public.
05:30
Boyd Set model
05:32
string Model
05:35
two.
05:38
This start
05:40
model equals
05:42
What?
05:44
Okay, Perfect. So that is what we have done for our
05:50
car class. We have our three get methods. We have our three set methods for these three private variables over here.
05:59
And now what we need to do is we need to go into our Audi and walks back in class, and we need to override a default to string method now, not going to say too much about it right now, so I'll pause the lab over here, and we will continue in part two of this lab.
06:16
Thank you for watching. And I look forward to seeing you in part two
Up Next