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.8.
Task one is to add a constructor that does not receive any parameters inside the airline class.
So we will create a constructor that gets called whenever we create an object of the airline class.
And the constructor will not expect any values because in task number two, inside the constructor,
all we have to do is set people, the people field equal to a new array that can store 11 person objects.
We can say people is equal to a new person array that can store 11 elements.
And although people already points to the people field, let's just say in the future we decide to add
a parameter here.
There would be a conflict.
So let's just add the this keyword anyway.
So task number three, add a getter named get person that receives an index and returns a person object
based on the index.
So public.
We're going to return a single person object.
We call the gutter get person, and we're going to return a person based on the index that gets passed
in here.
We can say person, person is equal to whatever person object that we index from the people's array.
And here we're going to return a new copy of the person object.
So new person creates a new object of the person class because we're only passing in one argument.
The copy constructor is going to get called and it's going to populate the object that we created with
a values from the source object.
Task number four is to add a setter named set person that receives one parameter.
Person person.
The Seder is going to be void.
It will be called set person.
What was the parameter again?
The person itself.
All right, Where are we going with this?
And now consider that the people array contains 11 elements such that we start counting from zero.
The parameter person, it's going to have a seat number that ranges from 1 to 11.
So we need to use that person's seat number to index the people's array, but we need to do that without
stepping out of bounds.
INT index is equal to whatever that person, the seat number is minus one.
Because if the person does seat number happens to be 11, we would want to access the element at index
ten.
If the seat number happens to be one, we would want to access the element at index zero.
All right.
So now we can use this index to access a particular element from the people's array.
And we're going to set that element equal to a new object of the person class.
Not object is going to get all of its values from the person we pass in.
The starter code contains an array of person objects.
It also contains an object of the airline class.
Fair enough.
Use your for loop to populate the airline with objects from the array in main.
So here we can say airline.
Not set person the person at index I.
All right.
Print the objects at indices one, five and ten.
Let's just copy this over and visualize the runtime.
So here we've created an array that can store objects of type person, and this array has 11 person
objects.
If you click the AI icon, you can visualize each object.
All right.
And now what we're going to do next is create an object of the airline class because we're passing in
zero arguments.
It's going to call the constructor that expects zero parameters.
This point to the current object of the airline class that we just created, and here inside the constructor,
we're setting this dot people equal to a new array that can store 11 person objects.
And efforts.
Every single element inside this array starts off as null.
So now we can press continue to the next breakpoint, because in this loop we're going to populate every
single element inside of this field.
Here we call airline that set person passing in the person object at Index zero.
So here is the person object that we passed in Cleopatra, the very first element.
And this points to the airline object that called set person.
Cleopatra has a seat number is one.
But when you're coding, we start counting from zero.
So index is going to equal the seat number minus one.
Which means here we are accessing the element at index zero.
We're setting this equal to a new object of the person class passing in one source object, which is
going to end up calling the copy constructor.
The source object is the object that we passed in this point to the object that we just created.
And here we're populating every single field inside of this object with values from the source.
All right.
At this point, we populated every single field inside of the current object and the element that index
zero is going to store a reference that points to it.
Beautiful.
So that's the first run of our loop.
Let's do this one more time, and then I'm just going to skip to this breakpoint.
So now I equals one, which means airline set person is going to pass in this object.
Alexander the Great.
The object we passed in Alexander the Great has a seat number of two.
So the index is going to be that minus one.
Here, we're setting the element at index one equal to a new object of the person class.
And because we're passing in one argument as we're creating the object, it's going to call the constructor
that expects to receive one parameter.
This points to the object that we just created.
And here all we're doing is we're setting every single field in that object with values from the source.
So at this point, we have populated every single field in the object we just created.
And we set the element at index one equal to a reference to that object.
All right.
Now, I'm not going to keep doing this for the other nine elements, so let's just remove these breakpoints.
Press Continue.
And at this point, all of our airline has been populated with passengers.
We are good here.
We print the objective index one.
So that's going to be Alexander the Great.
All right, Here we present the object at index five.
That's going to be parallelize.
Here we print the object at index ten.
That's going to be arabe.
And we're done.
Let's make sure we have what we're supposed to have.
Beautiful.
I hope you enjoyed once again another breakpoint session.