Java Bootcamp – Workbooks and Challenges
)}

Workbook – Part 7

Prerequisite: you finished writing the code for Part 6.

Any class that models an object needs a toString() method.

Task 1 – toString


Add a toString method to the Person class. Return a String that follows this template:

"Name: " + <name field> + "\n" + "Nationality: " + 
<nationality field> + "\n" + "Date of Birth: " + 
<date of birth field> + "\n" + "Seat Number: " +
<seat number field> + "\n" + "Passport: " + 
<passport field> + "\n";

Task 2 – Setting up main()


  • Remove the large println() function from Part 6.

  • Print your object directly.

Java should internally call toString and print its return value:

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

Good Luck!

Feedback Summary
4.8
39 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.