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.6.
Task one is to initialize the passport whenever a new object is created so we can say this passport
is equal to a new array that can hold three elements.
Task two is to add the getter get passport.
So here we can say public.
We're going to be returning an array of string values when an object calls.
Gut passport.
And we're going to return arrays dot copy of.
We will return a copy of the passport array and we're going to copy over the full length of the passport
array.
And that should be on.
Task number three is to add this print line function at the end of your main method.
Simple enough, we just have to fill in the placeholders where necessary at the end.
So we'll do it here.
Fix up the spacing.
So here we call.
Person name or get name, I should say.
Here we call.
Person dot get nationality.
Fix up the spacing again.
Here we call.
Person dot get date of birth.
Person.
Don't get seat number.
And for the passport we need to print, it's two string.
So arrays dot to string, person, dot, get passport.
All right.
Your output should appear as follows.
Let's go ahead and visualize the runtime.
I'm only going to put a breakpoint over here because we have visualized the other getters countless
times before.
Here, we're creating a new object of the person class.
And because we are passing in four arguments, it's going to call the constructor that expects to receive
four parameters.
Here you can visualize the parameter values.
This points to the current object that was just created.
And here we set this name equal to the parameter.
Here we set this nationality equal to the nationality parameter.
Here.
We set this date of birth equal to the date of birth that was passed in.
And this passport does not require a parameter.
H is going to be set equal to a new array that can hold three elements, and this seat number will be
set equal to the seat number that was passed in.
All right, The constructor is done updating the objects as fields, and at this point the person variable
stores a reference to the object that we just created.
We're not dealing with this right now.
And you'll remember we have a breakpoint at person.
Get passport.
So let's just continue to it.
And so the passport field, ultimately it stores a reference that points to the array.
We don't want to return the same reference.
26.
If we were to say return passport, all it's going to do is return a reference to the same array.
26.
But in this case, we're returning a copy of the array.
So here you can see get passport returns, another reference for 37 to a brand new array.
But ultimately it's just a copy of this array.
So null.
Here.
We printed all of the fields values, including the passport, which at the moment is just null.
And so task number four is telling us our code contains a set or set password.
Notice it doesn't have a parameter inside set passport.
We set the passport equal to the name, nationality and date of birth fields.
So here inside of set passport, you can do one of two things.
You can say Passport at index zero is equal to the name.
Passport and index one is equal to the nationality and passport at index two is equal to the date of
birth.
You could have also done this in a single line.
It's totally up to you.
So here you could have just set passport equal to a new string array that stores the name, nationality
and date of birth.
But I'm going to stick to what we have over here.
And now we call the setter if the person gets approved for a passport.
Otherwise, we just don't do anything.
So if person got apply for passport is true, we are going to call person dot set passport.
Okay, let us visualize the runtime.
Here we create a new object of the person class calling in the constructor that expects to receive four
values.
The constructor gets called.
It receives every single value as a parameter.
We visualize the parameters over here.
This point to the current object that was just created, the one that's calling this constructor.
Here we set this name equal to the name value that we passed in.
Here we set this nationality equal to the nationality that was passed in date of birth.
We set passport equal to a new array that can store three elements, and here we set the seat number
equal to five.
All right, we're done.
Initializing our object person stores a reference to that object.
Here we apply for a passport.
Let's hope the application returns true.
It doesn't.
So you know what?
Let me just restart the runtime.
Skip all this, we create a new object again, going inside of apply for passport.
Does this return true again?
It does not.
So let me just skip ahead.
Finally, now we accept their passport application, which means person not set passport is going to
get called.
We step inside of set passport.
This points to the current object that called set passport.
At the moment it has a bunch of null values.
That's fine.
We're going to set the element at index zero equal to the name.
We're going to set the element that index one equal to the nationality and the element that index to
equal to the date of birth.
All right.
Looking good.
So now our person object has been fully updated.
Their passport was accepted and updated accordingly.
I'm just going to continue to the next breakpoint, get passport.
And so in order to protect our field, instead of returning a reference to the same array.
What we're going to do instead is return a copy of the array.
So here you can see that the method got passport returns, a brand new reference to a copy of the array.
And ultimately we're just printing all of the field values and we are golden.
Upon getting approved for a passport, their passport gets initialized.
All right.
And in the event that their passport gets denied, we should get the following values.
Let's just run it using Java C Main Java.
Java Main.
Once again, we get approved.
Let's just keep running it until we get denied.
In this case, this never gets called, which means we never update our passport field.
It stays null and then gets printed.
I hope you enjoyed this breakpoint session.