More On Classes UW CSE 160 Winter 2017 1.

Slides:



Advertisements
Similar presentations
1 A Censor Class class Censor: def __ init __ badwords=[]): self.badwords = badwords self.replacers = replacers def input_badwords(self,fpath):
Advertisements

Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Section 6.1 CS 106, Fall The Big Q What do we get by being able to define a class?! Or Do we really need this?!
Week 2 Classes, Objects and lists review Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Chapter 5 Black Jack. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-2 Chapter Objectives Provide a case study example from problem statement.
Guide to Programming with Python
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
Floating point numbers in Python Floats in Python are platform dependent, but usually equivalent to an IEEE bit C “double” However, because the significand.
Poker UML and ADT design plan.
11/27/07. >>> Overview * objects * class * self * in-object methods * nice printing * privacy * property * static vs. dynamic * inheritance.
Computer Science 111 Fundamentals of Programming I More Data Modeling.
Object-oriented Programming (review + a few new tidbits) "Python Programming for the Absolute Beginner, 3 rd ed." Chapters 8 & 9 Python 3.2 reference.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
Data Abstraction UW CSE 140 Winter What is a program? – A sequence of instructions to achieve some particular purpose What is a library? – A collection.
Guide to Programming with Python
Two Way Tables Venn Diagrams Probability. Learning Targets 1. I can use a Venn diagram to model a chance process involving two events. 2. I can use the.
Computer Science 111 Fundamentals of Programming I Model/View/Controller and Data model design.
More On Classes UW CSE 160 Spring Classes define objects What are objects we've seen? 2.
Guide to Programming with Python Week 11 Chapter Nine Inheritance Working with multiple objects.
Introduction to OOP OOP = Object-Oriented Programming OOP is very simple in python but also powerful What is an object? data structure, and functions (methods)
November 28, 2005ICP: Chapter 9: Object Oriented Programming 1 Introduction to Computer Programming Chapter 9: Object Oriented Programming Michael Scherger.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:
Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!
Author Ivan Dominic Baguio. ABOUT THE GAME Game Objective  The goal of each player in the game is to discard all cards in his hand before every other.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 12 Inheritance and Class Design 1.
Chapter 17 Q and A Victor Norman, et al. CS104. What is Object-oriented Programming? Q: What is object-oriented programming? A: It means defining classes/objects,
Q and A for Sections 6.2, 6.3 Victor Norman CS106.
Data Abstraction UW CSE 160 Spring What is a program? – A sequence of instructions to achieve some particular purpose What is a library? – A collection.
Chance We will base on the frequency theory to study chances (or probability).
Object-Oriented Programming (OOP) in Python References: Chapter 8.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
Math 1320 Chapter 7: Probability 7.4 Probability and Counting Techniques.
Probability Intro. Coin toss u Toss two coins 10 times keeping track of the results (head/tails) u Now toss 3 coins 10 times u Make a chart of all the.
Arrangements and Selections (and how they may be counted)
Section 5.5 Application: The Card Game of War. 5.5 Application: The Card Game of War A deck of cards is shuffled and dealt to two players The players.
Discrete Distributions
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Python programming - Defining Classes
Guide to Programming with Python
Compsci 6/101: I Python Techniques for looping
COMPSCI 107 Computer Science Fundamentals
CSSE 120—Rose Hulman Institute of Technology
Mathematical operators overload
Random Sampling Playing cards are our participants
CS-104 Final Exam Review Victor Norman.
Copyright (c) 2017 by Dr. E. Horvath
Chapter 5 Black Jack.
6.NS.5 Integer War You will need: deck of cards
Computer Science 111 Fundamentals of Programming I
Fundamentals of Programming I More Data Modeling
Probability of casino games
Data Abstraction UW CSE 160 Winter 2017.
Data Abstraction UW CSE 160 Winter 2016.
Data Abstraction UW CSE 160 Spring 2018.
We’re slowly working our way towards classes and objects
Computer Science 111 Fundamentals of Programming I
How many objects can you create by drawing in the circles?
Combinations.
Fundamentals of Programming I More Data Modeling
Task 2 Implementation help
Section 12.2 Theoretical Probability
Section 12.2 Theoretical Probability
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Multiplying and Dividing Integers
Migrating to Object-Oriented Programs
Introduction to Object-Oriented Programming (OOP) II
Section 12.2 Theoretical Probability
Data Abstraction UW CSE 160.
Presentation transcript:

More On Classes UW CSE 160 Winter 2017 1

Classes are a template for objects What are objects we've seen? 2

Classes are templates for objects Examples of objects we've seen: Dict List Set Graph File Others? 3

Objects can be created with constructors set_one = set() dict_one = dict() # dict_one = {} str_one = str() # str_one = "" list_one = list() # list_one = [] import networkx as nx graph_one = nx.Graph() 4

Objects have methods set_one.add('purple') dict_one.setdefault('four', 16) str_one.capitalize() list_one.extend([1, 2, 3, 4]) graph_one.add_edge(1, 2) 5

Objects have internal state str_one = 'purple' str_two = 'spectrographically' >> str_one.count('c') >> str_two.count('c') 2 >> graph_one.nodes() [1, 2] 6

Classes are templates for objects A class is a blueprint for an object. class Vehicle: Style Note: Classes use CamelCase. No spaces or underscore but the first letter of each word is capitalized. Usually keep class names to a single word if possible. 7

Classes are templates for objects class Vehicle: def __init__(self, make, color, passengers, wheels=4, tank=20): ''' Create a new Vehicle Object ''' self.model, self.color = make, color self.seats = passengers self.wheels, self.tank = wheels, tank self.gas = 0 if __name__ == '__main__': my_car = Vehicle('Honda', 'White', 4) your_motorcycle = Vehicle('Mazda', 'Red', 2, 2) semi = Vehicle('Mercedes', 'Black', 2, wheels=16) __init__ is the constructor. This is a “magic” method. Means something special to python. In this case it defines how to create a new Vehicle object. 8

Classes are templates for objects class Vehicle: def __init__(self, make, color, passengers, wheels=4, tank=20): ''' Create a new Vehicle Object ''' self.model, self.color = make, color self.seats = passengers self.wheels, self.tank = wheels, tank self.gas = 0 def fill_tank(self,gallons): '''Add gallons to tank. Until it is full''' self.gas += gallons if self.gas > self.tank : self.gas = self.tank 9

Classes are templates for objects class Vehicle: def __init__(self, make, color, passengers, wheels=4, tank=20): ''' Create a new Vehicle Object ''' self.model, self.color = make, color self.seats = passengers self.wheels, self.tank = wheels, tank self.gas = 0 def __str__(self): return 'Gas remaining: ' + str(self.gas) __str__ is a “magic” method to convert object to a string. 10

Let's Play With Vehicles import vehicle 11

Why Use Classes? Classes are blueprints for objects, objects model the real world. This makes programming easier. Have multiple objects with similar functions (methods) but different internal state. Provide a software abstraction for clients to use without needing to know the details of how the object is implemented. 12

A Card Game Create the base classes that could be used by a client to create multiple card games. Blackjack Spades Poker Cribbage Euchre (24 cards!) 13

A Card Game: Design What are some high level classes that might be useful? 14

A Card Game: Design What are some high level classes that might be useful? Deck Holds a set of cards, can be shuffled and deal cards into Hands. Hand Holds cards and has basic methods for calculating properties. (has pair, sum etc) Card Takes a face value character, points value, and suit. 15

A Card Game: Design Useful functions for Card class class Card: 16

A Card Game: Design class Card: def __init__(self, face, suit, value=1): '''Create a new card''' self.face, self.suit = face.upper()[0], suit.upper()[0] self.value = value def is_black(self): return self.suit == 'S' or self.suit == 'C' def is_face(self): return not self.face.isdigit() 17

A Card Game: Design More magic methods, comparing cards (Also in class Card:) … def __eq__(self,other): return self.value == other.value def __lt__(self,other): return self.value < other.value def __gt__(self,other): return self.value > other.value See Also: __ne__, __le__, __ge__ 18

A Card Game: Design Useful functions for the Hand class class Hand: 19

A Card Game: Design Useful functions for the Hand class class Hand: def __init__(self,cards): self.card = cards def value(self): return sum([c.value for c in self.cards]) def has_pair(self): '''Returns True if hand has a pair''' for i, c in enumerate(self.cards): for c2 in self.cards[i + 1:]: if c.face == c2.face: return True return False 20

A Card Game: Design Useful functions for the Deck class class Deck: 21

A Card Game: Design Useful functions for the Deck class class Deck: def __init__(self,cards): self.cards = cards def shuffle(self): '''Randomize the order of internal cards list''' random.shuffle(self.cards) def deal(self,n=1): hand_cards = self.cards[0:n] del self.cards[0:n] return Hand(hand_cards) 22

A Card Game: Design Useful functions for the Deck class (also in class Deck:) … def __len__(self): return len(self.cards) 23