{ "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": [ "# A:" ] }, { "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": [ "# A:" ] }, { "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": [ "# A:" ] }, { "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", "