Workbook 7.5
From the Java Bootcamp Resources
, launch Workbook 7.5
.
Task 1
Inside the Person
class, define a function called applyPassport
.
/** * Function name: applyPassport * * * * */
The function returns a boolean
and does not expect parameters.
/** * Function name: applyPassport * @return (boolean) <---- * * * */
This code randomly returns 0 or 1.
int number = (int) (Math.random() * 2); //random int that can be 0 or 1.
Use this code to return true
if the number equals 1
, and false
otherwise.
/** * Function name: applyPassport * @return (boolean) * * Inside the function: * 1. Returns a random boolean of true or false. */
Do not use if
- else
to accomplish this task.
Task 2
Inside the Person
class, define a function called chooseSeat
.
/** * Function name: chooseSeat * * * */
This code returns a random number between 1 and 11.
(int) (Math.random() * 11 + 1);
chooseSeat
must use this code to update this.seat
with a random seat.
/** * Function name: chooseSeat * * Inside the function: * 1. Sets this.seat to a random number between 1 -- 11. */
Task 3
Your starter project should contain one object.
Person person = new Person("Rayan Slim", "Canadian", "01/01/1111", 5);
Call applyPassport
from your object. If the function returns true
:
System.out.println("Congratulations " + person.getName() + ". Your passport was approved!");
Otherwise:
System.out.println("We are sorry " + person.getName() + ". We cannot process your application.");
Example Output 1:
>>: Name: Rayan Slim >>: Nationality: Canadian >>: Date of Birth: 01/01/1111 >>: Seat Number: 10 >>: Congratulations Rayan Slim. Your passport was approved!
Example Output 2:
>>: Name: Rayan Slim
>>: Nationality: Canadian
>>: Date of Birth: 01/01/1111
>>: Seat Number: 10
>>: We are sorry Rayan Slim. We cannot process your application.
Run your code multiple times. Each run should output a random result.
Task 4.
Unfortunately the person's seat is already taken. Your object must call chooseSeat
to "choose another seat".
Example Output 1
>>: Name: Rayan Slim >>: Nationality: Canadian >>: Date of Birth: 01/01/1111 >>: Seat Number: 10 >>: Congratulations Rayan Slim. Your passport was approved!
Example Output 2
>>: Name: Rayan Slim >>: Nationality: Canadian >>: Date of Birth: 01/01/1111 >>: Seat Number: 10 >>: We are sorry Rayan Slim. We cannot process your application.
Example Output 3
>>: Name: Rayan Slim >>: Nationality: Canadian >>: Date of Birth: 01/01/1111 >>: Seat Number: 4 >>: Congratulations Rayan Slim. Your passport was approved!
Keep testing the function. It should keep updating the person's seatNumber
in the range 1 - 11
.
Visualizing the Runtime
After you solve this workbook, I still recommend watching the video solution on Udemy.
It will show you how to visualize the runtime using Visual Studio Code.