Lab 4: Polymorphism

View presentation

Tim is getting ready to go abroad! He is scheduled to give talks in Japan, France, and Germany but because he is so busy preparing his speeches, unfortunately that leaves him little to no time to worry about packing. He throws a few random items into his suitcase and heads to the airport in a hurry. As soon as he arrives at the gate, Tim realizes that he doesn’t know where his plane ticket is amongst the mess of other items in his bag. Help Tim find the correct plane ticket so that he doesn’t miss his big debut on the world stage!

Setup

Copy the following stencil code into a file named your_bag.py:

In the stencil, you will see that three classes are already defined: FlightTicket, Receipt, and Phone. These classes represent what kinds of objects populate the user’s bag. These classes also have individual methods that simulate what each object does in the real world.

Below the three classes, you will also find things_in_bag, which is a list of objects you find while looking through your bag. These are actual instances of the three classes defined above. You are later going to define the check_item function, which will look at an object and see if it is a type of flight ticket.

Part 1: Defining the Polymorphic Method

Once you’ve opened the bag, we are going to define the polymorphic method that will help us find the ticket easily. You might notice that in each class, there is a method called is_ticket. This method takes in a string country_name representing which country the ticket is for and returns a boolean value (which is the result of checking whether the current object is the ticket we are looking for).

Later in our search function, we are going to call the is_ticket method on each item contained in the list. So, right now your job is to define the is_ticket method for each of the three different classes! Doing so will allow our search method to later call this method on an object contained in the list, regardless of what type that object may be. Polymorphism in action!

Hint: You would only want the flight ticket to be able to return True when called on a ticket. So, the Receipt and Phone classes’ is_ticket methods can never return true - they might look very simple!

If you are confused about defining polymorphic methods, call a TA over for help.

Part 2: Defining the check_item Method

Now that we’ve defined the is_ticket method for each of the three classes, we are going to define our check_item method to locate the flight ticket from our list! This is where we’ll make use of our polymorphic method. Because we are looping through the list at the end of the program (where our search method will be used), our job right now is to call the is_ticket method to check whether each item is a flight ticket or not.

The item to check will come from the given list, and you want to return a boolean value signifying whether this object is a flight ticket or not.

Do not modify the things_in_bag list! This will be used to check if your code is functional.

Part 3: Is Your Ticket There?

Hurray! You’ve filled out all the methods. Now, we want to check if your code works. In your local terminal, run your file by typing in python3 your_bag.py. With the given list, your program should print out the following:

This is not the flight ticket
This is not the flight ticket
The flight ticket for France is in the bag!
This is not the flight ticket
This is not the flight ticket

This is because the flight ticket to France is the third item in the things_in_bag list.

If you find a bug (for example, your program only prints out one kind of statement), then try to use print lines to figure out how your code is being executed. If you have any questions, raise your hand and your Lab TA will help!

Success!

You’ve found the flight ticket, and the user is happy!