Java Bootcamp – Workbooks and Challenges
)}

Workbook – Part 9

Prerequisites:

  • You finished writing the code for Part 8.

Task 1 – Setting up main()


Remove all the code in your main() method. Use the following starter code:

public static void main(String[] args) { Person[] people = new Person[] { new Person("Cleopatra", "Egypt", "69 BC", 1), new Person("Alexander the Great", "Macedon", "356 BC", 2), new Person("Julius Caesar", "Rome", "100 BC", 3), new Person("Hannibal", "Carthage", "247 BC", 4), new Person("Confucius", "China", "551 BC", 5), new Person("Pericles", "Greece", "429 BC", 6), new Person("Spartacus", "Thrace", "111 BC", 7), new Person("Marcus Aurelius", "Rome", "121 AD", 8), new Person("Leonidas", "Greece", "540 BC", 9), new Person("Sun Tzu", "China", "544 BC", 10), new Person("Hammurabi", "Babylon", "1750 BC", 11), }; Airline airline = new Airline(); for (int i = 0; i < people.length; i++) { } }

Task 2 – Creating reservations.


Recall, the airline can perform one action: create a reservation.

Inside the Airline class, add a method called createReservation. It will return void and receive one parameter: Person person.

Inside the function:

  1. Use the seatNumber of the person object to index the seats array.

  2. Set the indexed element equal to the person object.

    • Watch out for the Reference Trap.

    • Set the element equal to a new copy of the object.

  3. println: Thank you < person name > for flying with Java airlines. Your seat number is <person seat number>. (new line)

Task 3 – Call the method.


From main(), call the method for every Person object.

Expected output:

>>: Thank you Cleopatra for flying with Java airlines. Your seat number is 1.
>>: Thank you Alexander the Great for flying with Java airlines. Your seat number is 2.
>>: Thank you Julius Caesar for flying with Java airlines. Your seat number is 3.
>>: Thank you Hannibal for flying with Java airlines. Your seat number is 4.
>>: Thank you Confucius for flying with Java airlines. Your seat number is 5.
>>: Thank you Pericles for flying with Java airlines. Your seat number is 6.
>>: Thank you Spartacus for flying with Java airlines. Your seat number is 7.
>>: Thank you Marcus Aurelius for flying with Java airlines. Your seat number is 8.
>>: Thank you Leonidas for flying with Java airlines. Your seat number is 9.
>>: Thank you Sun Tzu for flying with Java airlines. Your seat number is 10.
>>: Thank you Hammurabi for flying with Java airlines. Your seat number is 11.

Task 4 – Choose another seat.


Change Hammurabi's seat number to 10.

Person[] people = new Person[] { new Person("Cleopatra", "Egypt", "69 BC", 1), new Person("Alexander the Great", "Macedon", "356 BC", 2), new Person("Julius Caesar", "Rome", "100 BC", 3), new Person("Hannibal", "Carthage", "247 BC", 4), new Person("Confucius", "China", "551 BC", 5), new Person("Pericles", "Greece", "429 BC", 6), new Person("Spartacus", "Thrace", "111 BC", 7), new Person("Marcus Aurelius", "Rome", "121 AD", 8), new Person("Leonidas", "Greece", "540 BC", 9), new Person("Sun Tzu", "China", "544 BC", 10), new Person("Hammurabi", "Babylon", "1750 BC", 10), <-------- };

The requirements state that:

The airline should not create a reservation if the requested seat has already been taken.

We need to add this functionality to the createReservation method.

  1. Inside the method, create a loop that keeps running while the indexed seat is not null.

    • The idea is that if the indexed seat is not null, then it's already taken.
  2. While the loop is running, println:

    • (new line) <person name >, seat: <person seat> is already taken. Please choose another one. (new line).

    • Then, get them to choose another seat (call person.chooseSeat()).

Example Output:

Thank you Cleopatra for flying with Java airlines. Your seat number is 1. 
Thank you Alexander the Great for flying with Java airlines. Your seat number is 2. 
Thank you Julius Caesar for flying with Java airlines. Your seat number is 3.
Thank you Hannibal for flying with Java airlines. Your seat number is 4. 
Thank you Confucius for flying with Java airlines. Your seat number is 5. 
Thank you Pericles for flying with Java airlines. Your seat number is 6. 
Thank you Spartacus for flying with Java airlines. Your seat number is 7. 
Thank you Marcus Aurelius for flying with Java airlines. Your seat number is 8. 
Thank you Leonidas for flying with Java airlines. Your seat number is 9. 
Thank you Sun Tzu for flying with Java airlines. Your seat number is 10. 

Hammurabi, seat: 10 is already taken. Please choose another seat.
Hammurabi, seat: 6 is already taken. Please choose another seat.
Hammurabi, seat: 6 is already taken. Please choose another seat.
Hammurabi, seat: 2 is already taken. Please choose another seat.
Hammurabi, seat: 2 is already taken. Please choose another seat.
Hammurabi, seat: 2 is already taken. Please choose another seat.
Hammurabi, seat: 4 is already taken. Please choose another seat.
Thank you Hammurabi for flying with Java airlines. Your seat number is 11.

The application keeps prompting the passenger to choose a seat that isn't taken.

Task 5 – toString.


Add a toString method to the Airline class. The toString for Airline will return a description of every person in the seats array.

  • Inside the method, write: String temp = "";

  • Create a for loop that runs through every person in the seats array.

    • During each run, add the toString description of each seated Person to temp.

    • During each run, add two new lines \n\n to temp.

  • return temp.

Task 6 – Print the airline.


At the end of main():

1.  println : ****************** RESERVATIONS COMPLETE! ****************** (new line)

2.  println : airline

Example Output:

Thank you Cleopatra for flying with Java airlines. Your seat number is 1. 
Thank you Alexander the Great for flying with Java airlines. Your seat number is 2. 
Thank you Julius Caesar for flying with Java airlines. Your seat number is 3. 
Thank you Hannibal for flying with Java airlines. Your seat number is 4. 
Thank you Confucius for flying with Java airlines. Your seat number is 5. 
Thank you Pericles for flying with Java airlines. Your seat number is 6. 
Thank you Spartacus for flying with Java airlines. Your seat number is 7. 
Thank you Marcus Aurelius for flying with Java airlines. Your seat number is 8. 
Thank you Leonidas for flying with Java airlines. Your seat number is 9. 
Thank you Sun Tzu for flying with Java airlines. Your seat number is 10. 

Hammurabi, seat: 10 is already taken. Please choose another seat. 
Hammurabi, seat: 9 is already taken. Please choose another seat. 
Thank you Hammurabi for flying with Java airlines. Your seat number is 11.

>>: ****************** RESERVATIONS COMPLETE! ******************
>>: Name: Cleopatra
>>: Nationality: Egypt
>>: Date of Birth: 69 BC
>>: Seat Number: 1
>>: Passport: [null, null, null]

>>: Name: Alexander the Great
>>: Nationality: Macedon
>>: Date of Birth: 356 BC
>>: Seat Number: 2
>>: Passport: [null, null, null]

>>: Name: Julius Caesar
>>: Nationality: Rome
>>: Date of Birth: 100 BC
>>: Seat Number: 3
>>: Passport: [null, null, null]

>>: Name: Hannibal
>>: Nationality: Carthage
>>: Date of Birth: 247 BC
>>: Seat Number: 4
>>: Passport: [null, null, null]

>>: Name: Confucius
>>: Nationality: China
>>: Date of Birth: 551 BC
>>: Seat Number: 5
>>: Passport: [null, null, null]

>>: Name: Pericles
>>: Nationality: Greece
>>: Date of Birth: 429 BC
>>: Seat Number: 6
>>: Passport: [null, null, null]

>>: Name: Spartacus
>>: Nationality: Thrace
>>: Date of Birth: 111 BC
>>: Seat Number: 7
>>: Passport: [null, null, null]

...

>>: Name: Hammurabi
>>: Nationality: Babylon
>>: Date of Birth: 1750 BC
>>: Seat Number: 11
>>: Passport: [null, null, null]

Task 7 – Applying for a passport.


Each person needs to apply for a passport before flying with Java airlines. Currently, everyone's passport field contains null values (see output above).

Inside the for loop in main(), call applyPassport from each object.

  • If the return value is true, call setPassport(). Then, create the reservation.

  • Otherwise, do nothing.

Expected Output:

>> CRASH! NullPointer Exception.

Task 8 – Fixing the NullPointer Exception .


If you did everything correctly, your code should crash at the toString method.

Not everyone qualifies for a passport, so some of the seats are going to be null. That being said, can you guess which line of code crashes specifically?

Spoiler alert: temp += seats[i].toString();

You cannot call a method or access something from a null. If the element (seat[i]) happens to be null, then calling toString from it results in a NullPointerException.

  • So, create an if statement that checks if the element is not null.

    • There, add the Strings to temp as usual.
  • Otherwise:

    • add Seat (i + 1) is empty. to temp. Why (i + 1)? Think about it.

    • add two new lines \n\n to temp.

Your final output should appear as follows:

Thank you Cleopatra for flying with Java airlines. Your seat number is 1.
Thank you Alexander the Great for flying with Java airlines. Your seat number is 2.
Thank you Hannibal for flying with Java airlines. Your seat number is 4.
Thank you Pericles for flying with Java airlines. Your seat number is 6.


>>: ******************* RESERVATIONS COMPLETE! ******************

>>: Name: Cleopatra
>>: Nationality: Egypt
>>: Date of Birth: 69 BC
>>: Seat Number: 1
>>: Passport: [Cleopatra, Egypt, 69 BC]

>>: Name: Alexander the Great
>>: Nationality: Macedon
>>: Date of Birth: 356 BC
>>: Seat Number: 2
>>: Passport: [Alexander the Great, Macedon, 356 BC]

>>: Seat 3 is empty.

>>: Name: Hannibal
>>: Nationality: Carthage
>>: Date of Birth: 247 BC
>>: Seat Number: 4
>>: Passport: [Hannibal, Carthage, 247 BC]

>>: Seat 5 is empty.

>>: Name: Pericles
>>: Nationality: Greece
>>: Date of Birth: 429 BC
>>: Seat Number: 6
>>: Passport: [Pericles, Greece, 429 BC]

>>: Seat 7 is empty.

>>: Seat 8 is empty.

>>: Seat 9 is empty.

>>: Seat 10 is empty.

>>: Seat 11 is empty.

Task 9 – Running a test.


The output gets really interesting if half the passengers start with the same seat numbers.

Person[] people = new Person[] {
  new Person("Cleopatra", "Egypt", "69 BC", 1),
  new Person("Alexander the Great", "Macedon", "356 BC",1), 
  new Person("Julius Caesar", "Rome", "100 BC", 1),
  new Person("Hannibal", "Carthage", "247 BC", 1),
  new Person("Confucius", "China", "551 BC", 1), 
  new Person("Pericles", "Greece", "429 BC", 2), 
  new Person("Spartacus", "Thrace", "111 BC", 2),
  new Person("Marcus Aurelius", "Rome", "121 AD", 2),
  new Person("Leonidas", "Greece", "540 BC", 2),
  new Person("Sun Tzu", "China", "544 BC", 2),
  new Person("Hammurabi", "Babylon", "1750 BC", 2) };

Example output:

Thank you Julius Caesar for flying with Java airlines. Your seat number is 1. 
Confucius, seat: 1 is already taken. Please choose another seat. 
Thank you Confucius for flying with Java airlines. Your seat number is 7.
Thank you Pericles for flying with Java airlines. Your seat number is 2.
Leonidas, seat: 2 is already taken. Please choose another seat.
Thank you Leonidas for flying with Java airlines. Your seat number is 9.
Hammurabi, seat: 2 is already taken. Please choose another seat. 
Thank you Hammurabi for flying with Java airlines. Your seat number is 10.
>>: ******************* RESERVATIONS COMPLETE! ******************* >>: Name: Julius Caesar >>: Nationality: Rome >>: Date of Birth: 100 BC >>: Seat Number: 1 >>: Passport: [Julius Caesar, Rome, 100 BC] >>: Name: Pericles >>: Nationality: Greece >>: Date of Birth: 429 BC >>: Seat Number: 2 >>: Passport: [Pericles, Greece, 429 BC] >>: Seat 3 is empty. >>: Seat 4 is empty. >>: Seat 5 is empty. >>: Seat 6 is empty. >>: Name: Confucius >>: Nationality: China >>: Date of Birth: 551 BC >>: Seat Number: 7 >>: Passport: [Confucius, China, 551 BC] >>: Seat 8 is empty. >>: Name: Leonidas >>: Nationality: Greece >>: Date of Birth: 540 BC >>: Seat Number: 9 >>: Passport: [Leonidas, Greece, 540 BC] >>: Name: Hammurabi >>: Nationality: Babylon >>: Date of Birth: 1750 BC >>: Seat Number: 10 >>: Passport: [Hammurabi, Babylon, 1750 BC] >>: Seat 11 is empty.

Task 10 – Admire your work!


Once you're done, feel free to admire the complexity of this application, yet how clean and expressive the code remains in main().

Therein lies the beauty of Object-Oriented Programming!

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.