Computer Science 111 Fundamentals of Programming I More Data Modeling.

Slides:



Advertisements
Similar presentations
Black Jack in Objective-C
Advertisements

Topics in Python Blackjack & TKinter
C9, a case study: Solitaire
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For.
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
© Glenn Rowe AC Lab 2 A simple card game (twenty- one)
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.
It’s All in the Cards Adding and Subtracting Integers
Computer Science 111 Fundamentals of Programming I Introduction to Object-Based Programming.
Computer Science 112 Fundamentals of Programming II Array-Based Queues.
Describing Probability
Playing Cards Deck of 52 uniquely designed playing cards.
12 Pontoon1May Pontoon program CE : Fundamental Programming Techniques.
CS 106 Introduction to Computer Science I 11 / 13 / 2006 Instructor: Michael Eckmann.
Design Example. Requirements Make a program that simulates the game of blackjack For now, we ignore money/betting…. just simulate game play But… this.
Intro to Probability & Games
CS 106 Introduction to Computer Science I 11 / 08 / 2006 Instructor: Michael Eckmann.
INFO 206 Lab Exercise 1 Introduction to Classes and Objects 1/18/2012i206 Lab 1 - Exercise1.
Chapter 5 Black Jack. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-2 Chapter Objectives Provide a case study example from problem statement.
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
Mau Mau (mow- mow) A card game
A R R A Y ! for Multiplication!!
VOCABULARY  Deck or pack  Suit  Hearts  Clubs  Diamonds  Spades  Dealer  Shuffle  Pick up  Rank  Draw  Set  Joker  Jack 
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
Fundamentals of Python: From First Programs Through Data Structures
Guide to Programming with Python
CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies.
Algorithms. What is an algorithm?  An algorithm is a clear, concise, and correct step- by-step sequence of actions.
Poker UML and ADT design plan.
Compound Probability Pre-AP Geometry. Compound Events are made up of two or more simple events. I. Compound Events may be: A) Independent events - when.
Copyright © Cengage Learning. All rights reserved. 4 Probability.
Guide to Programming with Python
Copyright 2013, 2010, 2007, Pearson, Education, Inc. Section 12.2 Theoretical Probability
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.
Poker. Basic card terminology Suits: Hearts, diamonds, clubs, spades Deuce, face cards.
15.3 Counting Methods: Combinations ©2002 by R. Villar All Rights Reserved.
Computer Science 111 Fundamentals of Programming I Persistent Data Models Object Serialization.
More On Classes UW CSE 160 Spring Classes define objects What are objects we've seen? 2.
Computer Science 112 Fundamentals of Programming II Interfaces and Implementations.
Guide to Programming with Python Week 11 Chapter Nine Inheritance Working with multiple objects.
Designing a Card Game Solitaire--Thirteens. Game.
Review Homework pages Example: Counting the number of heads in 10 coin tosses. 2.2/
November 28, 2005ICP: Chapter 9: Object Oriented Programming 1 Introduction to Computer Programming Chapter 9: Object Oriented Programming Michael Scherger.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
Draw 3 cards without replacement from a standard 52 card deck. What is the probability that: 1.They are all red ? 2.At least one is black ? 3.They are.
Week 8 : User-Defined Objects (Simple Blackjack Game)
Chance We will base on the frequency theory to study chances (or probability).
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.
Python programming - Defining Classes
Guide to Programming with Python
Fundamentals of Programming I Introduction to Object-Based Programming
COMPSCI 107 Computer Science Fundamentals
CSSE 120—Rose Hulman Institute of Technology
Software Development Java Classes and Methods
11/7/16 Entry 173 – Objective I will review and continue developing my understanding of uniform probability models, including the complement of an event.
Chapter 5 Black Jack.
Fundamentals of Programming II Interfaces and Implementations
Computer Science 111 Fundamentals of Programming I
Fundamentals of Programming I More Data Modeling
Fundamentals of Programming I More Data Modeling
Computer Science 111 Fundamentals of Programming I
Fundamentals of Programming I Commonly Used Methods More Modeling
Fundamentals of Programming I More Data Modeling
Fundamentals of Programming I More Data Modeling
Task 2 Implementation help
Fundamentals of Python: First Programs Second Edition
Homework Due Friday.
P(softball) = P(not a baseball) = P(golf ball) = A box contains 3 tennis balls, 7 softballs, and 11 baseballs. One ball is chosen at random. Find.
HOW TO PLAY POKER.
Presentation transcript:

Computer Science 111 Fundamentals of Programming I More Data Modeling

More Modeling: a Bank Manages a set of accounts Operations –Add a new account –Remove an account –Access an account for deposits and withdrawals –Compute the interest on all accounts

Class Diagram Bank SavingsAccount * Describes the relationship between two classes A Bank object contains zero or more SavingsAccount objects

Bank() # Returns a new bank add(account) # Adds account to bank remove(name, pin) # Removes an account from bank get(name, pin) # Returns an account or None computeInterest() # Computes the interest and # deposits it in each account str(aBank) # Returns string rep of bank The Interface of the Bank Class

from bank import Bank wellsFargo = Bank() for pinNumber in range(1000, 1005): wellsFargo.add(SavingsAccount('Ken', str(pinNumber))) print(wellsFargo) account = wellsFargo.get('Ken', '1001') account.deposit(100) print(wellsFargo) account.withdraw(20) print(wellsFargo.computeInterest()) Example Use of the Bank Class

Defining a Bank Class Set up a data structure for the data Define methods to –Add a new account –Remove an account –Access an account for deposits and withdrawals –Compute the interest on all accounts

class Bank(object): """This class represents a bank.""" def __init__(self): self._accounts = {} # Other methods go here Defining the Bank Class Use a dictionary for the accounts, keyed by the name + pin

class Bank(object): """This class represents a bank.""" def __init__(self): self._accounts = {} def __str__(self): result = "" for account in self._accounts.values(): result += str(account) + "\n" return result Defining the Bank Class Always define __init__ and __str__ first

class Bank(object): """This class represents a bank.""" def __init__(self): self._accounts = {} def __str__(self): return "\n".join(map(str, self._accounts.values())) Defining the Bank Class Simplify, simplify!

class Bank(object): """This class represents a bank.""" def __init__(self): self._accounts = {} def add(self, account): key = account._name + account._pin self._accounts[key] = account Adding an Account An account is a value in the dictionary, keyed by its name and pin The remaining methods are similar

Class Variables An instance variable refers to storage owned by a single instance A class variable refers to storage owned by the class, and thus available to all of its instances For example, each savings account owns a separate balance, so that should be an instance variable But all savings accounts have the same interest rate, so that should be a class variable

Class and Instance Variables SavingsAccount a1a2 SavingsAccount.RATE self._balance from account import SavingsAccount a1 = SavingsAccount('Ken', '3322', ) a2 = SavingsAccount('Catherine', '3321', ) print(SavingsAccount.RATE)

account = SavingsAccount('Ken', '3322', ) print(account.computeInterest()) SavingsAccount.RATE = 0.06 print(account.computeInterest()) Using Class Variables

class SavingsAccount(object): """This class represents a savings account.""" RATE = 0.06 # Class variable def __init__(self, name, pin, balance = 0.0): self._name = name self._pin = pin self._balance = balance def computeInterest(self): interest = SavingsAccount.RATE * self._balance self.deposit(interest) return interest Defining a Class Variable Class variables are picked out using the class name as a prefix Their names are usually in uppercase

Another Example: Playing Cards Used in card games, such as War, Blackjack, Poker, and Crazy Eights A standard deck consists of 52 cards Each card has a rank (a number from 1-13, where Ace is 1 and face cards are 11-13) Each card has a suit (Spades, Hearts, Diamonds, Clubs)

A Card: State and Behavior State: –a rank –a suit Behavior: examine the values of the state variables, but don ’ t ever modify them

>>> card = Card(3, 'Spades') >>> print(str(card.rank) + ' of ' + card.suit) 3 of Spades >>> print(card) 3 of Spades Using the Card Class A Card object contains two data values, and these are never changed Thus, there is no need for accessor or mutator methods Just reference the card ’ s variables to get their values

class Card(object): SUITS = ('Spades', 'Hearts', 'Diamonds', 'Clubs') RANKS = tuple(range(1, 14)) def __init__(self, rank, suit): self.rank = rank self.suit = suit Defining the Card Class The Card class can specify the ranges of ranks and suits as class variables

class Card(object): … def __str__(self): """Returns the string representation of a card.""" if self.rank == 1: rank = 'Ace' elif self.rank == 11: rank = 'Jack' elif self.rank == 12: rank = 'Queen' elif self.rank == 13: rank = 'King' else: rank = self.rank return str(rank) + ' of ' + self.suit String Representation

for suit in Card.SUITS: for rank in Card.RANKS: card = Card(rank, suit) print(card) Create and Print all 52 Cards print automatically runs str to obtain an object ’ s string representation

A Deck: State and Behavior A deck initially contains 52 cards The user can –shuffle the deck –deal a single card from its top –check to see if there are more cards to deal Deck Card 0..52

Deck() shuffle() deal() # Removes and returns the top card isEmpty() str(aDeck) The Deck Interface and Its Use Interface

Deck() shuffle() deal() # Removes and returns the top card isEmpty() The Deck Interface and Its Use from cards import Deck deck = Deck() deck.shuffle() while not deck.isEmpty(): card = deck.deal() print(card) Interface Use

Application: The Game of War A simplified version with two players and a single war pile Each player has 2 piles of cards: –An unplayed pile –A winnings pile

Playing the Game of War Deal 26 cards to each player’s unplayed pile While both unplayed piles are not empty Each player moves the topmost card from the unplayed pile to the top of the game’s war pile If these cards do not have the same rank Move the cards from the game’s war pile to the winner’s winnings pile The player with the largest winnings pile wins

Additional Classes Player contains 2 piles of cards WarGame contains a deck, two players, and a single pile of cards; allows the user to step through the game On each step, the cards are drawn and the war pile is shifted if there is a winner

Classes and Relationships Deck Card WarGame Player 2 cards.py: Card, Deck wargame.py: Player, WarGame

WarGame() # Creates a deck and 2 players deal() # Deals 26 cards to each player step() # Draws the cards and shifts the piles winner() # Returns None if the game is not over, # or the results as a string otherwise str(aGame) # The current state of the game as a string The WarGame Interface

WarGame() # Creates a deck and 2 players deal() # Deals 26 cards to each player step() # Draws the cards and shifts the piles winner() # Returns None if the game is not over, # or the results as a string otherwise str(aGame) # The current state of the game as a string Playing the Game def main(): game = WarGame() game.deal() while not game.winner(): game.step() print(game) print(game.winner())

For Wednesday Data Persistence