{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### \n", "\n", "# Unit 3 Lab: Object-Oriented Programming\n", "\n", "## Overview\n", "Welcome to the Unit 3 lab!\n", "\n", "We're making a slight jump with this lab to OOP structure, so please use the starter notebook as a starting point.\n", "\n", "Let's use object-oriented programming concepts to improve our code. Specifically, we'll be using dictionaries and classes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Media Class\n", "\n", "Create a `Media` class. This will be the parent class of `Movie`, and it itself will inherit from the `object` base python class.\n", "\n", "- The `Media` class will take two arguments, `publisher` and `market`, both of which are strings. These strings will set class instance variables, `self.publisher` and `self.market`. Whenever this class is instantiated or called, assume values of `Universal Studios` for `publisher` and `USA` for `market`.\n", "- The `Media` class will have one class method, `get_media_info()`. This method will print the `self.publisher` and `self.market` class instance variables to stdout and return `None`." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "class Media(object):\n", " \"\"\"Base class for Movie and Book objects\"\"\"\n", "\n", " def __init__(self, publisher, market):\n", " self.publisher = publisher\n", " self.market = market\n", "\n", " def get_media_info(self):\n", " \"\"\"Prints media info to stdout\"\"\"\n", " print(f'The Media object\\'s publisher is {self.publisher} \\\n", " and the market is {market}.')\n", " return None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Movie Class\n", "\n", "Create a `Movie` class. This class will inherit from the base `Media` class. We're going to have several movies, each of which will have a title and a rating. We can use a `Movie` class as a scaffold to create many movie objects. Your `Movie` class should have three methods:\n", " 1. `__init__`, which will take in `self`, `movie_data` (this will be a dictionary containing the title and rating of each movie), `publisher` and `market`. `__init__` will set a member variable, `movie_data`, to equal the `movie_data` dictionary passed in. `publisher` and `market` variables will be passed to the parent `Media` object, which must also have its `__init__()` method called using the `super()` builtin function.\n", " 1. `get_movie_title()`, a getter function that returns the value of the `title` key in the `movie_data` dictionary.\n", " 1. `get_movie_rating()`, a getter function that returns the value of the `rating` key in the `movie_data` dictionary." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "class Movie(Media):\n", " \"\"\"\n", " Movie objects contain all information about a particular movie,\n", " including the title and rating.\n", " \"\"\"\n", "\n", " def __init__(self, movie_data, publisher, market):\n", " \"\"\"\n", " Initialize Movie class, note that the init of the superclass,\n", " Media, is called with the super().__init__() call\n", " \"\"\"\n", " # Call the constructor (init) method of Media\n", " super().__init__(publisher, market)\n", " # Store the raw data in this object so that we can use the\n", " # data in the getter functions.\n", " self.movie_data = movie_data\n", "\n", " def get_movie_title(self):\n", " \"\"\"\n", " get_movie_title is a getter function that returns the movie title.\n", " \"\"\"\n", " # Return the title from the movie data.\n", " return self.movie_data[\"title\"]\n", "\n", " def get_movie_rating(self):\n", " \"\"\"\n", " get_movie_rating is a getter function that returns the rating.\n", " \"\"\"\n", " # Return the rating from the movie data.\n", " return self.movie_data[\"rating\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Return Single Movie Object\n", "\n", "Now that we have a class, let's make a function that creates `Movie` objects.\n", " - Create a function called `return_single_movie_object()` that takes four arguments:\n", " - `movie_title`\n", " - `movie_rating`\n", " - `publisher`\n", " - `market`\n", " - Have it create and return a `Movie` object with those values." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def return_single_movie_object(movie_title, movie_rating, publisher, market):\n", " \"\"\"\n", " Take in the movie title and rating, and return the movie object.\n", " \"\"\"\n", "\n", " return Movie({'title': movie_title, 'rating': movie_rating}, publisher, market)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Print All Ratings\n", "Let's make a function, `print_all_ratings` that takes a list of movie titles, iterates through them, and prints a result to stdout (more on that below).\n", "\n", "- The `print_all_ratings` function will call the `return_single_object` function on each iteration to return a `Movie` object.\n", "- Use the following hard-coded values of the following parameters when calling `return_single_object`:\n", "\n", "
| Variable | \n", "Value | \n", "
| movie_rating | \n", "4 | \n", "
| publisher | \n", "\"Universal Studios\" | \n", "
| market | \n", "\"USA\" | \n", "