# Classes --- CS 65 // 2021-04-13 ## Extra Credit Activities - MathCS talk on Friday + "A Tutorial on Designing Person-Centered Accessible Technologies" + Professor Meredith Moore + 2:00 to 3:00pm ## Assignment 7 - Can be completed individually or in a group of your choosing - Let me know if you need an extension + I am willing to give an extra day or two # Questions ## ...about anything? # Final Project ## Final Project - Groups of 1-3 students - Open ended project on a topic of your choice + **Option 1:** Data Analysis + **Option 2:** Computer Game + **Option 3:** Simulation Environment - Includes three parts + Proposal + Implementation + In-class demo ## Previous Data Projects - Haiku generator - Uncle Iroh meme generator - Investigated the change in public preference of words in song lyrics and visualized the results ## Previous Game Projects - Connect Four - Minesweeper - Battleship - Break Break - Flappy Bird - Pacman ## Previous Sim Projects - Fish-tank simulator - Biological simulator (diseased vs. white blood cells, change of seasons, etc.) - Galaxy simulator (motion of planets, etc.) ## your project ideas? - I will create breakout rooms on Thursday for self-assembling into groups with similar interest - Any project ideas that you want to throw out? + I will add a breakout room for it # Classes ## Classes - Python lets you create your own types of objects---called **classes** - A **class** is like a "template" for how to create objects and specifies: + What **instance variables** objects of this class have (i.e. internal data) + What **methods** can be performed on these objects (i.e. operations) ## Classes - Remember that when learning a **new type** (or creating your own type) you should ask yourselves a few questions: + What is the **name** of the type? + What is its **purpose**? + How do I **express** (i.e. construct) values of this type to the computer? + What **operations** (i.e. methods) can be performed on this type? ## The `Point` Class **3-2-1-GO Questions** --- - What is its **purpose**? - How do I **express** values of type `Point`? - What **operations** (i.e. methods) are supported? ## The `Image` Class **3-2-1-GO Questions** --- - What is its **purpose**? - How do I **express** values of type `Image`? - What **operations** (i.e. methods) are supported? # Creating Classes ## Our First Class - Suppose we'd like to program the game **Yahtzee** - During gameplay, players need to roll many **dice** - To help us make Yahtzee, we might decide to create an object type that we can use for **rolling dice** ## Our First Class - What do we want to **name** it? - What would its **purpose** be? - In what ways do we want to **express** values of this type? - What **operations** can be performed on this type? ## Our First Class - We should be able to interact with the class like this: ```py >>> d6 = MSDie(6) # creates a 6-sided die >>> d6.roll() # randomizes the die's value >>> d6.getValue() # gets the face up value 4 >>> d6.roll() >>> d6.getValue() 1 ``` ## Our First Class - To define a class, you use the **`class`** keyword ```py class MSDie: # class definition goes here ``` ## Our First Class - Inside a class definition, the `def` keyword defines a **method** for that class ```py class MSDie: def roll(self): # implement the roll() method here def getValue(self): # implement the getValue() method here ``` - The first parameter to a method always refers to the **object the method is called on** + `d6.roll()` is the same as `roll(d6)` + It is best practice to name this parameter **`self`** ## Our First Class - Every class has a special method named **`__init__`** which is responsible for **initializing** the object when it is being created ```py class MSDie: def __init__(self, num_sides): # implement the MSDie(num_sides) initializer here def roll(self): # implement the roll() method here def getValue(self): # implement the getValue() method here ``` - `d6 = MSDie(6)` will execute `__init__` with `num_sides` equal to 6 ## Our First Class - Each `MSDie` object should keep track of how many sides it has and its current value ```py class MSDie: def __init__(self, num_sides): self.sides = num_sides # tracks sides self.value = 1 # tracks value def roll(self): # implement the roll() method here def getValue(self): # implement the getValue() method here ``` - Variables that are stored inside an object are called **instance variables** ## Our First Class - Methods of a class can access instance variables through the `self` parameter ```py class MSDie: def __init__(self, num_sides): self.sides = num_sides # tracks sides self.value = 1 # tracks value def roll(self): # implement the roll() method here def getValue(self): return self.value # gets the value ``` ## Our First Class - Methods can also **change** instance variables through the `self` parameter ```py from random import randrange class MSDie: def __init__(self, num_sides): self.sides = num_sides self.value = 1 def roll(self): self.value = randrange(self.sides) + 1 def getValue(self): return self.value ``` # Lab Time