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.4.
Task one is to add a copy constructor inside the person class.
So while our normal constructor gets called when we pass in four parameters while creating an object,
the copy constructor is going to get called when we pass in another object.
So we're going to set the name of the current object that's being created equal to the field value from
our resource object.
We're going to set the nationality of the current object being created equal to the nationality of the
source object.
We're going to set the date of birth of the current object being created equal to the date of birth
from the source object from the object that was passed in.
And we're going to set the seat number equal to source dot seat number.
Okay.
Your code contains a person object.
The person's twin is also flying with Java Airlines.
Use this code to test your copy constructor so we can copy this over.
Here, we're creating a new object of the person class, and we're going to initialize this object as
fields using another object.
Because one object is our argument.
It's going to call the constructor that expects to receive one object as a parameter.
And once we create this object based on the old object, then we can update the newly created objects
as name equal to another value, and then update their seat number as well.
Let us go ahead and print the other person's fields as well.
Before we start visualizing the runtime.
So I'm going to put one breakpoint over here, one over here.
And should be good.
So here we're creating a new object of the person class passing in all of these values into the constructor.
That means the constructor that expects to receive four parameters is going to get called.
Here we set every single field inside of the current object that's being created equal to a parameter
value.
So first we're going to start with the name.
We set this name equal to Rand Slim.
We set this nationality equal to the parameter that was passed in Canadian.
We set this date of birth equal to the date of birth that was passed in 11111.
We set this seat number equal to the seat number that was passed in.
All right.
The person variable stores a reference to this object.
And if I step inside here, set seat number gets called for this object.
And what it's going to do is set the seat number for the current object equal to the seat number that
was passed in.
All right, beautiful.
I'm just going to continue to the next breakpoint now.
So at this point, it printed all of this object as fields.
Here, once again, we're creating a new object of the person class.
But because we're only passing in one argument, it's going to invoke the constructor that expects to
receive one parameter, the copy constructor.
And so here this points to the object that we just created, the one that has everything as null and
zero.
So if I step over here, we're setting every single field inside of the current object equal to a field
value from the source object that we passed in.
So here we're updating the name based on the source object.
Here we're updating the nationality based on the source object.
Here we update the date of birth, and here we update the seat number.
The variable twin stores, a reference to the object that we just created based on the field values
of another object.
And now what we're going to do is call set name for the twin object.
So I'm going to step inside here.
We're updating the name of the current object called this method equal to the following value.
Stepping inside a gun set seat number gets called for this object.
Here we update the seat number from 10 to 3.
Now if I step over.
Here we print the field values of the first object, and here we print the field values of the second
object, and we are done.