Lab 4: Polymorphism
Section 1: Background
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
Section 2: Polymorphism
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 allows 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!
Task 1: Define the is_ticket
method for each of the three different classes! 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).
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!
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.
Task 2: Define the check_item
method.
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. If it is a flight ticket, check whether the flight ticket is for the proper country name.
Note: 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.
Task 3: Run the program and check if your code works. If it does, you should see the following output:
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!
Section 3: You’re All Set!
Success! You’ve found the flight ticket, and the user is happy!