Lab 4: Polymorphism

Silly Premise

After virtually visiting many countries, you now want to travel to Kyoto, Japan. In order to do so, you first need to travel to Haneda Airport in Tokyo, and then take a bus from there. But, you realize that your bag is full of so many objects after your previous hectic travel! Because of that, you cannot find your flight ticket to Tokyo. The flight time is nearing, and you wish you could write a program that can quickly search for the flight ticket. Find the ticket and safely make it to your flight!

Part 1: Opening Your Bag

Open up a new project in PyCharm and name it lab04. Then, create a new file in that project named your_bag.py. Make sure to copy the stencil code shown below into that file you just created!

class FlightTicket:
    def __init__(self, country_name: str, departure_time: int):
        self.country_name = country_name
        self.departure_time = departure_time

    def read_departure_time(self):
        print(self.departure_time)

    def is_ticket(self, query: str) -> bool:
        #TODO: fill in this method to check if the current item is the flight ticket!
        pass

class Receipt:
    def __init__(self, store_name: str, price: int):
        self.store_name = store_name
        self.price = price

    def is_ticket(self, query: str) -> bool:
        #TODO: fill in this method to check if the current item is the flight ticket!
        pass

class Phone:
    def __init__(self, brand: str, is_on: bool):
        self.brand = brand
        self.is_on = False

    def turn_on(self):
        self.is_on = True

    def turn_off(self):
        self.is_on = False

    def is_ticket(self, query: str) -> bool:
        #TODO: fill in this method to check if the current item is the flight ticket!
        pass

things_in_bag = [Receipt("Starbucks", 13), Phone("Apple", False), FlightTicket("Japan", 400),
    Receipt("Uniqlo", 200), Receipt("Brown Bookstore", 350)]

def search(item, query: str) -> bool:
     """
    Here, you would need to define the search method utilizing the polymorphic method
    defined in the classes above in order to see if the current query item is the flight ticket or not.
    This method is going to get called in the for loop below
    and print out the results of your search.
    """
    pass

for item in things_in_bag:
    if search(item, "Japan"):
        print('The flight ticket for Japan is in the bag!')
    else:
        print('This is not the flight ticket')

In the stencil, you will see that three classes are already defined. These classes represent what kinds of objects populate your bag. These classes also have individual methods that represent what each object does.

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 search function, which will look at these objects and see if any one of them is the flight ticket we are looking for.

Part 2: Defining the Polymorphic Method

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

Later in our search function, we are going to use 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 a variable, regardless of what type that variable may be. Polymorphism in action!

Hint: You would only want the flight ticket to return True when called on this method. So, the Receipt and Phone classes’ is_ticket() method might look very simple!

If you are confused by the concept of defining polymorphic methods, raise your hand on Zoom and your Lab TA will help!

Part 3: Defining the Search Method

Now that we’ve defined the is_ticket() method for each of the three classes, we are going to define our search() method to locate the flight ticket to Japan 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 and check whether each item is the 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 equals to the Japan ticket or not. Perhaps using conditional statements would be good?

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

Part 4: 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 Japan is in the bag!
This is not the flight ticket
This is not the flight ticket

This is because the flight ticket to Japan 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 where your code is being executed. If you have any questions, raise your hand on Zoom and your Lab TA will help!

Success!

You’ve found the flight ticket in time, and you’re off to Tokyo!