Fundamentals of Programming I Commonly Used Methods More 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.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Object Oriented Design and Classes
INFO 206 Lab Exercise 1 Introduction to Classes and Objects 1/18/2012i206 Lab 1 - Exercise1.
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.
Fundamentals of Python: From First Programs Through Data Structures
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
Classes 3 COMPSCI 105 S Principles of Computer Science.
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.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Computer Science 111 Fundamentals of Programming I More Data Modeling.
Introduction to Object-Oriented Programming
Object-Oriented Programming in C++
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
CS61A Lecture 13 Object-Oriented Programming Jom Magrotker UC Berkeley EECS July 10, 2012.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Computer Science 111 Fundamentals of Programming I Persistent Data Models Object Serialization.
Computer Science 112 Fundamentals of Programming II Interfaces and Implementations.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Topic 4 Inheritance.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
John Byrne Inheritance Bank account examples. John Byrne Example account hierarchy F account relationships: ACCOUNT SAVINGSCHEQUE TIME_ACCT.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Object Oriented Programing (OOP)
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.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
CSC 231: Introduction to Data Structures Python and Objects – Day 3 Dr. Curry Guinn.
GCSE COMPUTER SCIENCE Practical Programming using Python
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Chapter 13 - Inheritance.
Python programming - Defining Classes
COMPSCI 107 Computer Science Fundamentals
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
Computer Science 111 Fundamentals of Programming I
Fundamentals of Programming II Equality and Multiple Inheritance
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
Fundamentals of Programming I Managing the Namespace
CSC 131: Introduction to Computer Science
Packages, Interfaces & Exception Handling
suggested reading: Java Ch. 6
Object Oriented Programming
Introduction to Object-Oriented Programming (OOP) II
3.7 Variable Assignment Recall instance variables in Python:
Computer Science 111 Fundamentals of Programming I
15-110: Principles of Computing
Fundamentals of Programming I More Data Modeling
Fundamentals of Programming I More Data Modeling
JAVA CLASSES.
Introduction to Object-Oriented Programming
Fundamentals of Python: First Programs Second Edition
Lecture 18 Python OOP.
A Level Computer Science Topic 6: Introducing OOP
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Object-Oriented Design AND CLASS PROPERTIES
Introduction to Object-Oriented Programming (OOP) II
Fundamentals of Programming II What Is Equality?
Presentation transcript:

Fundamentals of Programming I Commonly Used Methods More Modeling Computer Science 111 Fundamentals of Programming I Commonly Used Methods More Modeling

Making Meatballs Is Serious Business

Mind if I Lend a Hand?

The Interface of the SavingsAccount Class SavingsAccount(name, pin, bal) # Returns a new object getBalance() # Returns the current balance deposit(amount) # Makes a deposit withdraw(amount) # Makes a withdrawal computeInterest() # Computes the interest and # deposits it

Defining the SavingsAccount Class class SavingsAccount(object): """This class represents a savings account.""" def __init__(self, name, pin, balance = 0.0): self.name = name self.pin = pin self.balance = balance # Other methods go here Note that name is a method’s parameter, whereas self.name is an object’s instance variable

The Lifetime of a Variable class SavingsAccount(object): """This class represents a savings account.""" def __init__(self, name, pin, balance = 0.0): self.name = name self.pin = pin self.balance = balance # Other methods go here Parameters exist only during the lifetime of a method call, whereas instance variables exist for the lifetime of an object

Parameters or Instance Variables? Use a parameter to send information through a method to an object Use an instance variable to retain information in an object An object’s state is defined by the current values of all of its instance variables References to instance variables must include the qualifier self

The Scope of a Variable The scope of a variable is the area of program text within which its value is visible The scope of a parameter is the text of its enclosing function or method The scope of an instance variable is the text of the enclosing class definition (perhaps many methods)

The Scope of a Variable class SavingsAccount(object): """This class represents a savings account.""" def __init__(self, name, pin, balance = 0.0): self.name = name self.pin = pin self.balance = balance def deposit(self, amount): self.balance += amount def withdraw(self, amount): self.balance -= amount self.balance always refers to the same storage area (for one object) amount refers to a different storage area for each method call

The Interface of the SavingsAccount Class SavingsAccount(name, pin, bal) # Returns a new object getBalance() # Returns the current balance deposit(amount) # Makes a deposit withdraw(amount) # Makes a withdrawal computeInterest() # Computes the interest and # deposits it

The Interface of the SavingsAccount Class SavingsAccount(name, pin, bal) # Returns a new object getBalance() # Returns the current balance deposit(amount) # Makes a deposit withdraw(amount) # Makes a withdrawal computeInterest() # Computes the interest and # deposits it str(account) # String representation of account a1 == a2 # Test for equality

Accessing Data in an Object >>> account = SavingsAccount('Ken', '3322', 1000.00) >>> print('Name: ' + account.getName() + '\n' + \ 'PIN: ' + account.getPin() + '\n' + \ 'Balance: ' + str(account.getBalance())) Name: Ken PIN: 3322 Balance: 1000.00 An object’s data can be viewed or accessed by using its accessor methods

String Representation >>> account = SavingsAccount('Ken', '3322', 1000.00) >>> print(str(account)) # Same as account.__str__() Name: Ken PIN: 3322 Balance: 1000.00 Each class can include a string conversion method named __str__ This method is automatically called when the str function is called with the object as a parameter

String Representation >>> account = SavingsAccount('Ken', '3322', 1000.00) >>> str(account) 'Name: Ken\nPIN: 3322\nBalance: 1000.00' >>> print(account) # Better still Name: Ken PIN: 3322 Balance: 1000.00 Each class can include a string conversion method named __str__ print runs str if it’s given an object to print - way cool!

The __str__ Method class SavingsAccount(object): """This class represents a savings account.""" def __init__(self, name, pin, balance = 0.0): self.name = name self.pin = pin self.balance = balance def __str__(self): return 'Name: ' + self.name + '\n' + \ 'PIN: ' + self.pin + '\n' + \ 'Balance: ' + str(self.balance) As a rule of thumb, you should include an __str__ method in each new class that you define

Equality with == The two variables refer to the same, identical object >>> account1 = SavingsAccount("ken", "1000", 4000.00) >>> account2 = account1 >>> account1 == account2 True The two variables refer to the same, identical object account1 SavingsAccount object account2

Equality with == >>> account1 = SavingsAccount("ken", "1000", 4000.00) >>> account2 = account1 >>> account1 == account2 True >>> account3 = SavingsAccount("ken", "1000", 4000.00) >>> account1 == account3 False The two account objects have the same contents but aren’t equal account1 SavingsAccount object account2 SavingsAccount object account3

Equality with is >>> account1 = SavingsAccount("ken", "1000", 4000.00) >>> account2 = account1 >>> account1 is account2 True >>> account3 = SavingsAccount("ken", "1000", 4000.00) >>> account1 is account3 False By default, == uses is, which tests for object identity account1 SavingsAccount object account2 SavingsAccount object account3

What Is Equality? Object identity: two variables refer to the exact same object Structural equivalence: two variables refer to distinct objects that have the same contents Object identity is pretty strict, maybe too strict == actually tests for structural equivalence with Python’s data structures

== Should Do Structural Equivalence >>> list1 = [1, 2] >>> list2 = list1 >>> list3 = [1, 2] >>> list1 is list2 True >>> list1 is list3 False >>> list1 == list3 Two lists are equal if they are identical or have the same elements list1 [1, 2] list2 [1, 2] list3

Define the Method __eq__ class SavingsAccount(object): """This class represents a savings account.""" def __init__(self, name, pin, balance = 0.0): self.name = name self.pin = pin self.balance = balance def __eq__(self, other): if self is other: return True if type(other) != SavingsAccount: return False return self.name == other.name and \ self.pin == other.pin Test for identity, then type, then equality of selected attributes The operator == actually calls the method __eq__

For Friday More on banking, etc.