Java Bootcamp – Workbooks and Challenges
)}

Workbook – Part 4

Prerequisite: you finished writing the code for Part 3.

In this workbook, you will write a copy constructor. The copy constructor copies every value from a source object into the current object. So, it's our best solution for reference trap 2.

Task 1 – Add a copy constructor


Inside the Person class, add a copy constructor.

The copy constructor sets every field in the current object equal to a value from the source object.

Task 2 – Test the copy constructor


I'm assuming you already created a Person object. In which case, that person's identical twin is also flying with Java airlines.

  • Using the copy constructor, set a person2 variable equal to a copy of the first object.

  • Using the setters, change person2's name and seatNumber fields to something else.

  • Finally, use the same println() template to print person2's fields.

Example output:

Name: Rayan Slim Nationality: Canadian Date of Birth: 01/01/1111 Seat Number: 10 Name: Jad Slim Nationality: Canadian Date of Birth: 01/01/1111 Seat Number: 3

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.