1 of 20

ME 4990: Intro to CS�Object-Oriented Programming & Machine Learning 101

Static Methods and OOP Review

2 of 20

Outline

  • Static Method and class attribute
    • Static Method
    • Class attribute
  • Wrap up of object-oriented programming
    • Attributes
    • Methods
    • Inheritance
    • Static Methods

3 of 20

Introduction

  • In the tkinter example:
    • Tk is a class;
    • self.root is an instance of the Tk class

    • Frame is a class
    • self.frame is an instance of the Frame class

    • Label is a class
    • self.label_1 is an instance of the Label class
    • config is a method of the Label class

4 of 20

Introduction

  • When we use math library
    • Appears math is a class
    • However, we do not have an instance
    • We directly class a method using the class name
    • class_name.method() rather than instance_name.method()

5 of 20

Static Method, see static_eg

  • Static methods allow programmers to call a method without instantiation.
  • Commonly used in library authoring
  • Not recommended for routine application
  • In python 3, simply mark with @staticmethod

6 of 20

Class Attribute

  • See Card.py, which is also here

Instance Attribute

Class Attribute Declaration

Use Class Attribute

7 of 20

Outline

  • Static Method
    • Introduction and Example
  • Using UML for Class and Inheritance Modeling (AD) Chapter 18
    • UML for card
    • Card
    • Deck
    • Hand

8 of 20

Card

  • A deck of poker cards without joker

    • Has 4 types of suit
    • Each suit has 13 numbers

9 of 20

We can define card class

  • The card class has 2 “class attributes”, suit_names and rank_names
  • The card class has 2 “instance attributes”, suit of a card, and rank of a card

10 of 20

Class methods

  • We can write class methods for comparison, for example, check if a card is greater than the other:

11 of 20

Deck class

  • A deck of card contains 52 instances of card

cards

[52*Card]

list

Deck

deck1�Instance

12 of 20

Deck class operation

  • We can do operation of cards in a deck

13 of 20

Hand class

  • The hand class shall inherit from a deck class, because you have all operations of deck
  • You shall have a “card” instance attribute, but no card inside
  • The hand class shall have a “label” instance attribute

14 of 20

Hand class

  • The hand class shall inherit from a deck class, because you have all operations of deck
  • When you start a hand instance, you start with no cards

  • Where is the super method?

Cards

label

[]

list

Hand

hand1�Instance

15 of 20

UML

  • HAS-A relationship: arrow head, possibly with *, with reference of another class
  • IS-A relationship: hollow triangle arrow

  • Roughly translated: hand is a kind of deck, a deck has multiple cards

16 of 20

Exercise 1

  • Write “deal_hand”
  • Read the Card.py

Write a Deck method called deal_hands that takes two parameters, the number of hands and the number of cards per hand. It should create the appropriate number of Hand objects, deal the appropriate number of cards per hand, and return a list of Hands.

17 of 20

Terms

18 of 20

Exercise 2

  • Add methods to Deck class named has_pair, has_twopair, etc. that return True or False according to whether or not the hand meets the relevant criteria. Your code should work correctly for “hands” that contain any number of cards (although 5 and 7 are the most common sizes).

19 of 20

Exercise 3

  • Write a method named classify that figures out the highest-value classification for a hand and sets the label attribute accordingly. For example, a 7-card hand might contain a flush and a pair; it should be labeled “flush”.

20 of 20

Exercise 4

  • When you are convinced that your classification methods are working, the next step is to estimate the probabilities of the various hands. Write a function that
    • shuffles a deck of cards,
    • divides it into hands
    • classifies the hands and
    • counts the number of times various classifications appear.
  • Run your program with larger and larger numbers of hands until the output values converge to a reasonable degree of accuracy. Compare your results to the values at

http://en.wikipedia.org/wiki/Hand_rankings