Java Bootcamp – Workbooks and Challenges
)}

Workbook – Part 6

Prerequisite: you finished writing the code for Part 5.

Task 1 – Setting up main()


Remove your code from main(). Use the following template as starter code:

public static void main(String[] args) {

    Person person = new Person("Name", "Nationality", "Birth Date", Seat Number); 
    if (person.applyPassport() == true) {


    }
 }

Task 2 – Constructor


The constructor runs once you create a new object.

We cannot update the passport field until the person applies for it.

  • So, the constructor will not receive another parameter.

  • Inside the constructor, set the passport field equal to a new String[] array with a length of 3.

Task 3 – Getter


  • Add the getter: getPassport(). Be careful from Reference Trap 3.

  • Add this println() function at the end of your main() method. Replace the placeholders where appropriate.
System.out.println("Name: " + <name field> + "\n" + 
"Nationality: " + <nationality field > + "\n" + 
"Date of Birth: " + <birth date field> + "\n" +
"Seat Number: " + <seat field> + "\n" + "Passport: " +
 <toString of passport field> + "\n");

Your output should appear as follows:

>>: Name: Rayan Slim >>: Nationality: Canadian >>: Date of Birth: 01/01/1111 >>: Seat Number: 5 >>: Passport: [null, null, null]

Task 4 – Copy Constructor

The copy constructor (CC) copies every value from a source object into the current object (the one being created).

Inside the CC, set the current object's passport field equal to a copy of the source object's field.

Task 5 – Setter

Add the setter: setPassport().

  • This setter doesn't need a parameter.

  • The passport stores the name, nationality, and date of birth of the current object.

Task 6 – Call the setter

Call the setter if the person gets approved for a passport.

Output 1: If the person gets approved, their passport field should be updated:

>>: Name: Rayan Slim >>: Nationality: Canadian >>: Date of Birth: 01/01/1111 >>: Seat Number: 5 >>: Passport: [Rayan Slim, Canadian, 01/01/1111]

Output 2: Otherwise, the passport field contains null values:

>>: Name: Rayan Slim >>: Nationality: Canadian >>: Date of Birth: 01/01/1111 >>: Seat Number: 5 >>: Passport: [null, null, null]

Keep running your code and make sure you get alternating outputs.

Good Luck!

Feedback Summary
4.8
38 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.