# Classes --- CS 65 // 2021-04-15 ## 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 - Extended deadline to Saturday night - CS 65 review session will be held from 2-3pm tomorrow via Zoom # Questions ## ...about anything? # Classes, Continued ## Classes - We can create our own types---called **classes** - A **class** is like a "template" for how to create objects and specifies: + What **instance variables** objects of this class have + What **methods** can be performed on these objects ## Multi-Sided Die Class - We implemented a class `MSDie` last time: ```py >>> d6 = MSDie(6) # creates a 6-sided die >>> d6.roll() # rolls the die to randomize it >>> d6.getValue() # gets the current value of the die 4 >>> d6.roll() >>> d6.getValue() 1 ``` ## Multi-Sided Die Class - `__init__` is the **constructor** (or initializer) for the class and is called when an `MSDie` object is created ```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 ``` # Cannonball ## Cannonball Program - Suppose we want to create a simulation of **firing a cannonball** - For fun, let's also *visualize* the simulation using Zelle's graphics library ## Cannonball Program - First, let's write some **pseudocode** to help guide us in our implementation: ```text import necessary stuff here def main(): setup graphics window create ball to visualize cannonball and draw it setup initial position, velocity, and angle while the simulation is not over: update the position of the cannon ball apply the force of gravity to ball main() ``` ## Cannonball Program ```py from graphics import * from math import cos, sin, radians def main(): win = GraphWin("Cannonball", 600, 300) win.setCoords(0, 0, 600, 300) ball = Circle(Point(0, 5), 10) ball.setFill("red2") ball.draw(win) init_velocity, init_angle = 2, 45 velx = init_velocity * cos(radians(init_angle)) vely = init_velocity * sin(radians(init_angle)) while win.isOpen(): ball.move(velx, vely) vely = vely - 0.02 update(60) main() ``` ## Launching Cannonballs - Now let's change the program in the following way: + Whenever the **spacebar** key is pressed, a new cannonball is fired - This requires tracking **multiple** cannonballs simultaneously! - How can we modify our program to do this? + Would need to keep track of the velocity, angle, position, and Circle **for each cannonball** ## Launching Cannonballs - Instead of copying and pasting our variables to keep track of a single cannonball, let's **create a class** - We will have the class be in charge of: + The velocity, angle, position, and Circle of a ball + Have a method for updating its location ## Launching Cannonballs - Here's the cannonball class: ```py gravity = 0.02 class Cannonball: def __init__(self, win, init_velocity, init_angle): self.velx = init_velocity * cos(radians(init_angle)) self.vely = init_velocity * sin(radians(init_angle)) self.ball = Circle(Point(0, 5), 10) self.ball.setFill("red2") self.ball.draw(win) def simStep(self): self.ball.move(self.velx, self.vely) self.vely = self.vely - gravity if self.ball.getCenter().getY() <= 0: self.vely = -self.vely ``` ## Launching Cannonballs - Here's the `main` function using our class: ```py def main(): win = GraphWin("Cannonball", 600, 300) win.setCoords(0, 0, 600, 300) cannonballs = [] while win.isOpen(): if win.checkKey() == "space": b = Cannonball(win, 3, 80) cannonballs.append(b) for ball in cannonballs: ball.simStep() update(60) ``` # Encapsulation ## Encapsulation - Encapsulation is the principle of **hiding implementation details** of the class from the developers that use it - Encapsulation has the following benefits: + It provides **abstraction** by allowing users to focus on *how the object behaves* rather than the details of how its implemented + It also helps protect the integrity of objects by making it difficult for developers to use an object in a way it is not meant to be used ## Cannonball Revisited - Let's look at our `main` function again - Notice how we do NOT need to know exactly *how* Cannonball was implemented to use it ```py def main(): win = GraphWin("Cannonball", 600, 300) win.setCoords(0, 0, 600, 300) cannonballs = [] while win.isOpen(): if win.checkKey() == "space": b = Cannonball(win, 3, 80) cannonballs.append(b) for ball in cannonballs: ball.simStep() update(60) ``` # 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 ## Part One: The Proposal - **Due:** Thursday, April 23 - Includes two deliverables: 1. Project Overview 2. Storyboard ## Proposal: Project Overview - 1-2 page overview of your project, including: + A **description** of your project + Any **data sets** you will be using (if applicable) + Your **development plan** for the project * Description of functions, classes, etc. and your plan for implementing them + A description of your preferred goal, minimum goal, and stretch goal ## Proposal: Storyboard - A **storyboard** (1-2 pages) that demonstrates visually how users will interact with your program + Basically a hand-drawn comic strip of any visual components of your project and the ways the user will interact with it ## Part Two: Implementation - **Due:** Thursday, May 14 - Includes all of the following items + All of your **code** (with docstrings / comments) + Any **data sets** you used (if applicable) + A **README** on your project ## README 1. Should include a **brief overview** of your project and whether you achieved your goals - Lists authors, citations, and other relevant info 2. **Instructions** for running your code / playing the game / interacting with the simulation 3. A list of any known bugs, errors, or any unimplemented features you didn't get to ## Part Three: Demo - Every group will prepare a **demo** of their project to present to the rest of the class - Plan to have a **live demo** of your project so students can see how it works and give you feedback # Final Project Time