Fundamentals of Programming I More Data Modeling

Slides:



Advertisements
Similar presentations
Computer Science 111 Fundamentals of Programming I Introduction to Object-Based Programming.
Advertisements

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes.
Computer Science 112 Fundamentals of Programming II Array-Based Queues.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Object Oriented Design and Classes
CS 100Lecture 61 CS100J Lecture 6 n Previous Lecture –Programming Concepts n Programming by stepwise refinement –a pattern –sequential refinement –case.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
INFO 206 Lab Exercise 1 Introduction to Classes and Objects 1/18/2012i206 Lab 1 - Exercise1.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Object Oriented Design and Classes. What is a class? Programming mechanism to support OOD Data and the methods that operate on that data – collectively.
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
Guide to Programming with Python
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
CHAPTER 10 OBJECT INHERITANCE Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING DATA ABSTRACTION Jehan-François Pâris
Who wants to be a Millionaire? Click to begin game.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Inheritance. Inhertance Inheritance is used to indicate that one class will get most or all of its features from a parent class. class Dog(Pet): Make.
Computer Science 111 Fundamentals of Programming I Search Algorithms.
Module 7: Object-Oriented Programming in Visual Basic .NET
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
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.
Computer Science 111 Fundamentals of Programming I Persistent Data Models Object Serialization.
Computer Science 112 Fundamentals of Programming II Interfaces and Implementations.
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
Computer Science 112 Fundamentals of Programming II Binary Search Trees.
Computer Science 112 Fundamentals of Programming II Implementation Strategies for Unordered Collections.
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Lecture 09 – Classes.  At the end of this lecture, students should be able to:  Define a new class  Store state information about instances of the.
Objective: Learn to describe the relationships and extend the terms in arithmetic sequence.
Savings Options Principles of Business Objective 4.03.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Do Now Why do people have checking accounts?. Unit 4: Lesson 13: Checking Accounts Day 1 Objective: identify vocabulary terms related to checking accounts.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
CSC 231: Introduction to Data Structures Python and Objects – Day 3 Dr. Curry Guinn.
Python programming - Defining Classes
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
1-4 Adding Integers Warm Up Problem of the Day Lesson Presentation
Adding Integers We are going to predict patterns and make rules based on what we notice. Then, we are going to simplify variable expressions.
Computer Science 111 Fundamentals of Programming I
Goal: use counters to add integers
COMP280:Introduction to Software Development Week 10, Lecture 28
Fundamentals of Programming II Binary Search Trees
Fundamentals of Programming II Interfaces and Implementations
Computer Science 111 Fundamentals of Programming I
Fundamentals of Programming I More Data Modeling
CSC 131: Introduction to Computer Science
Lesson : Adding Integers Using Rules
Computer Science 112 Fundamentals of Programming II
Packages, Interfaces & Exception Handling
Object Oriented Programming
Chapter 10 Defining Classes. Chapter 10 Defining Classes.
Computer Science 111 Fundamentals of Programming I
Fundamentals of Programming I Commonly Used Methods More Modeling
15-110: Principles of Computing
Fundamentals of Programming I More Data Modeling
Fundamentals of Programming I More Data Modeling
JAVA CLASSES.
Fundamentals of Python: First Programs Second Edition
SECTION 5-3 Account Statements pp
Lecture 18 Python OOP.
1-4 Adding Integers Warm Up Problem of the Day Lesson Presentation
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Fundamentals of Programming I More Data Modeling 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 Describes the relationship between two classes Bank * SavingsAccount Describes the relationship between two classes A Bank object contains zero or more SavingsAccount objects

The Interface of the Bank Class 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

Example Use 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) account.withdraw(20) print(wellsFargo.computeInterest())

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

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

Defining the Bank Class 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 Always define __init__ and __str__ first

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

Adding an Account 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 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 from account import SavingsAccount a1 = SavingsAccount('Ken', '3322', 1000.00) a2 = SavingsAccount('Catherine', '3321', 10000.00) print(SavingsAccount.RATE) SavingsAccount SavingsAccount.RATE a1 a2 self.balance self.balance

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

Defining a Class Variable 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 Class variables are picked out using the class name as a prefix Their names are usually in uppercase

Design patterns with classes For Friday Design patterns with classes