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.
The purpose of this workbook is to define a function inside of the person class that every object is
able to use.
The function will be called Apply PLI passport.
It will return a boolean and does not expect any parameters.
So here we can say public boolean.
It will return a boolean.
Apply a passport and it does not expect any parameters.
This code randomly returns zero or one.
Simple enough.
We covered math random very extensively in the functions section.
And what we need to do is use this code to return true if the number equals one and false.
Otherwise, it tells us not to use if else, to accomplish this task because we can just say return
the number equals one.
So if this is true, true will be returned.
If this is false, false will be returned.
There is no need to say if this is true, then return true.
That would just be very redundant.
Otherwise return false.
This looks really tedious.
We're already returning the result of a comparison, so we are good.
Task two is inside the person class to find a function called to seat.
It's void does not expect any parameters and it must use the following code to update the person's seat
with a random seat number.
So here we can say public void to seat.
This function will allow any object that calls it to choose a random seat.
Choosing a random seat will be based on the following code.
So here we can say this.
Not seat number.
Actually, there is no need to use the this keyword because there is no conflicting parameter.
By saying seat number, it's automatically going to reference the field of whatever object is calling
this method.
And we can set that equal to a random seat number between one and 11.
So a range goes from 1 to 11.
If you're wondering why that is, I highly recommend revisiting the function section.
Now your starter project should contain one object.
Let us confirm this.
Indeed, it does call apply passport from your object and if the function returns true, we print the
following.
Otherwise we print this.
So here we'll say.
If a person does not apply passport, if the result of them applying for a passport returns true, then
we're going to print the following message.
Congratulations.
Whatever the person's name is, your passport was approved.
Otherwise, if applying for a passport ends up returning false, then we print this message.
Sorry, we cannot process your application.
And it seems like, according to the output, we print their fields first, then we give them the result
of their application.
So what we're going to do is put this after.
And I think we're good.
Let us visualize the runtime.
As always.
One breakpoint here.
Only need one here, and then we can just keep stepping over.
Okay.
So here we're creating a new object of the person class, and we're calling the constructor that expects
to receive four values.
The constructor is going to receive every single value that we pass in as a parameter, such that this
points to the current object that we just created.
Here.
We set this name equal to the name parameter.
Here we set this nationality equal to the nationality parameter.
We set the objects as date of birth equal to the date of birth parameter.
And we set the object to seat number equal to the seat number that we passed in.
All right, now the constructor is finished updating the object as Field's person variable is going
to store a reference to that object.
And here we print that object as field values minus the passport because it's null.
And here that same person, that same object is going to call a PLI passport.
So this points to the current object that's calling a PLI passport, the same object with a reference
of at 14.
And this is just going to generate a random integer that is either zero or one.
In this case, it generates a number of zero.
Zero does not equal one.
So there are passport application is false.
Which means else is going to get triggered.
We are sorry.
We cannot process your application.
Okay, let's now try this again and hope that the next time it ends up being true.
So this is just going to create a new object of the person class printing every single field value.
We are applying for a passport again.
Hopefully it's true.
It is true.
One equals one.
Their passport application is going to get processed.
Congratulations person.
Get name.
Your passport was approved.
All right.
And that is all we were able to achieve both outputs.
This would look a little bit cleaner if you just run the code using terminal commands.
So a Java C main, Java, Java main.
And now you can run the code to your heart's content and you're going to get alternating results.