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.
It is bad practice to be able to directly access each field.
What we need to do is make every single field private to the class that it was defined in.
That way, nobody outside of the class can have direct access to these fields.
They are not visible.
The only place where they can be accessed is this class.
And so what we need to do is in task to add getters that return copies of each field value.
So the first getter.
Old get name.
It will return a string copy of the name field.
The second getter.
Will return a string copy of the nationality field.
So here we can return nationality.
The third getter will return a string called Get Date of Birth, and it will be used to get the date
of birth.
It's going to return the string value stored inside of the date of birth field.
And finally, we say public.
Integer.
Get seat number.
And what that will do is return the integer value stored inside of the seat number field.
Return.
Seat number.
All right.
Call the gutters from your print line function and rerun the code so we cannot access the fields directly,
but we can call the getters which return a copy of each field value.
Person get name.
Person.
Nationality.
Person dot.
Get date of birth.
Person dot.
Get seat number.
All right.
We can go ahead and run our code in debug mode.
Here we create a new object of the person class, and the constructor is going to initialize every single
field in that object.
The person variable stores a reference that points to the object we just created.
Here, if I step inside.
It's going to go inside of the string builder.
We don't care about that.
But if I press, step inside again, it's going to go inside of get name.
This points to the current object that's calling get name.
The current object has a field value of Rand Slim, which we are returning.
The get name getter returns ran Slim over here.
What I'll do actually is I will put breakpoints here so that we can just press continue.
So the next getter that we're calling is get nationality.
This points to the current object that's calling this gutter.
The current object has a nationality of Canadian, which we are returning.
All right.
The next method that we're calling is get date of birth.
The current object that's calling get date of birth has the following field value, which we are returning.
All right.
The fourth getter that's being called from the current object is get seat number.
And the current object has a seat number of five, which we are then returning.
And now if I press continue, the runtime will end and it ends up printing every single field value
in our object.
All right.
Task four is to add setters for name, nationality, date of birth, and seat number.
The reason we do that is because we cannot directly access any of the field and then update their values.
We need to update our field values through setters.
So we're going to have one setter that's going to set the name.
It will receive a string parameter name and using the parameter that gets passed in.
It's going to set the name field of the current object equal to the name parameter.
We're going to have another setter.
This one will be called set Nationality.
And here what we do is we set the nationality of the current object that's calling this method equal
to the nationality that gets passed in.
We create another setter.
That receives a date of birth.
So we'll call it set Date of birth.
It will receive a date of birth parameter.
And what it's going to do is set the date of birth of the current object that's calling this method
equal to the date of birth that gets passed in.
And we'll have one more getter.
This one will be called set seat number.
It will receive a seat number parameter.
And what it will do is set the seat number of the current object that's calling this method equal to
the seat number that gets passed in.
All right.
The task number five is the person's current seat number, unfortunately, is taken.
So what we need to do is update their seat number to ten before printing the fields.
So here we can say person, we're going to update that object seat number, set, seat number equal
to ten.
So now what I'm going to do is visualize the runtime.
All right.
So here we're creating a new object of the person class, and we're using our constructor to initialize
all of the objects fields.
So here we have our person variable, which stores a reference to that object.
And now we're going to call a set seat number for that same object.
This points to the current object that's calling this method, and we're setting the seat number of
the current object equal to the seat number that was passed in ten.
All right, Now that we've updated our object to seat number to ten, we can print all of its fields.
So if I press continue, it will continue to the first breakpoint.
Get name here we're calling person, get name this point to the current object that's calling this method
and we're returning a copy of the field value name.
The next method that we're calling from our person object is get nationality.
Once again, this point to the current object that's calling this getter.
Here we're just returning the field value Canadian.
We're also calling the gutter a date of birth.
And in this case, we're going to return the following field value.
And here we're going to return the seat number that we just recently updated.
So here is our object.
And here you can see the field values that we printed.
All right.
I hope you enjoyed this breakpoint session.