Java Bootcamp – Workbooks and Challenges

Workbook – Part 8


  • You finished writing the code for Part 7.

  • You finished Quiz 7.7.

In Part 8, you will create an object of the Airline class.

Task 1 – Create the class

In Quiz 7.1, you identified two types of objects:

You also found the corresponding fields and actions.

  1. So, create a new file named Inside the file, create the Airline class.

  2. Inside the Airline class, define a private field named seats.

  3. The seats field will be an array of Person objects.

Task 2 – Add a constructor

  • For now, your constructor will not receive any parameters.

  • Inside the constructor, set seats equal to a new array that can store 11 Person objects.

Task 3 – Add a getter

  • The getter will be named: getPerson. It will also receive one parameter: int index.

  • From the seats field, return a Person object at the requested index.

    • Watch out for the Reference Trap.

    • Make sure to return a new Person object.

Task 4 – Add a setter

The setter will be named: setPerson. It will only receive one parameter: Person person.

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

  • Then, set the indexed element equal to the person object.

    • Watch out for the Reference Trap.

    • Make sure to set the element equal to a new copy of the object.

Task 5 – 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();

The starter code contains an array of Person objects. It also contains an object of the Airline class.

Task 6 – Populate the seats field.

  • Create a for loop that runs through the length of the people array.

  • Set every element in the seats field of the airline equal to an object from the people's array.

Task 7 – Run the application.

  • If your code runs, you can proceed to Task 8.

  • If your application crashes, it's likely because your index is out of bounds.

    • Hint: The seat numbers go from 1 -- 11. The index range for seats is 0 -- 10.

    • Don't change the seat numbers. Instead, fix the code in your setter.

Task 8 – Test the application.

Using a getter, print the objects at indexes: 1, 5, and 10 of the seats array.

Expected Output:

>>: Name: Alexander the Great >>: Nationality: Macedon >>: Date of Birth: 356 BC >>: Seat Number: 2 >>: Passport: [null, null, null] >>: Name: Pericles >>: Nationality: Greece >>: Date of Birth: 429 BC >>: Seat Number: 6 >>: Passport: [null, null, null] >>: Name: Hammurabi >>: Nationality: Babylon >>: Date of Birth: 1750 BC >>: Seat Number: 11 >>: Passport: [null, null, null]

Good Luck!

Feedback Summary
38 students





Written Reviews
There are no written reviews yet.