Before we start, did you try solving the workbook yourself?
If not, please click the link in the resources folder and follow the instructions for this workbook.
Welcome to Workbook 7.2.
The way we have it right now is as soon as we create an object of the person class, then we update
all of that object as fields.
That is a bit messy.
What we want to do is call a constructor that updates the fields as soon as we create the object.
So task one is to create a person constructor that receives four parameters.
For now, we ignore the passport.
So here we're going to create a new constructor that gets called whenever we create a person object.
That constructor is going to receive four parameters during object creation.
So the name it's going to receive a nationality.
It will receive a date of birth.
As well as a seat number.
For now, we ignore the passport.
D This keyword means the current object and what we can do is access the field of the current object
and set it equal to the parameter that gets passed in.
So we'll do that for the name.
We will set the nationality field of that object equal to the nationality parameter.
We will set the date of birth field equal to the date of birth parameter, and we can set the seat number
field equal to the seat number parameter.
All right.
And now, as soon as we create an object of the person class, in order to invoke this constructor,
we need to pass in the correct number of arguments.
So we need to pass in a name, nationality, date of birth, and seat number.
We will use the same values we're using over here.
For now, we're ignoring the passport and we're just going to assign this person a seat.
Number of five.
All right.
I think I did task one.
And task to task three is to print every field as part of one string so we can just copy this over.
How convenient.
Here, I will print that person's name.
Here, I will print that person's nationality.
Here.
I will print their date of birth.
And here we can print their seat number.
Okay.
I think we're all done.
We should be ready to visualize the run time.
Let's do just that.
I'm just going to put one breakpoint over here.
And that's it.
So here what we're doing is we're creating a new object of the person class.
And as soon as we create the object, the constructor is going to get called.
And what the constructor is going to do is set every single field inside of the current object that's
being created equal to a parameter that gets passed in.
So here we set the name field of the current object equal to Ray and Slim.
Here we set the nationality field of the current object equal to Canadian.
Here.
We set the date of birth field of the current object equal to the date of birth that gets passed in
11111.
Here we set the seat number field of the current object equal to the seat number that gets passed in.
All right.
Now that we've initialized the current object.
The person variable stores a reference to that object.
And here we're printing all of the fields that belong to this object.