Computer Science 111 Fundamentals of Programming I

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Noadswood Science,  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015.
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Computer Science 111 Fundamentals of Programming I Introduction to Object-Based Programming.
Fundamentals of Programming II Inheritance and Abstract Classes
Modules and Objects Introduction to Computing Science and Programming I.
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Abstract Data Types and Encapsulation Concepts
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
Microsoft Visual Basic 2005 CHAPTER 1 Introduction to Visual Basic 2005 Programming.
Computer Science 111 Fundamentals of Programming I User Interfaces Introduction to GUI programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 10 Classes. Review of basic OOP concepts  Objects: comprised of associated DATA and ACTIONS (methods) which manipulate that data.  Instance:
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Computer Science 112 Fundamentals of Programming II Graphics Programming.
Classes and Objects The basics. Object-oriented programming Python is an object-oriented programming language, which means that it provides features that.
Computer Science 112 Fundamentals of Programming II Interfaces and Implementations.
CLASSES Python Workshop. Introduction  Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CS190/295 Programming in Python for Life Sciences: Lecture 7 Instructor: Xiaohui Xie University of California, Irvine.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Object-Oriented Programming. Objectives Distinguish between object-oriented and procedure-oriented design. Define the terms class and object. Distinguish.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Classes, Interfaces and Packages
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
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.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
TK1924 Program Design & Problem Solving Session 2011/2012
Introduction to Computing Science and Programming I
Fundamentals of Programming I Overview of Programming
Fundamentals of Programming I Introduction to Object-Based Programming
Recap: If, elif, else If <True condition>:
Classes and OOP.
Introduction to Visual Basic 2008 Programming
Topics Procedural and Object-Oriented Programming Classes
Software Development Java Classes and Methods
The Object-Oriented Thought Process Chapter 1
Today’s Objectives Review the important points of classes
Table of Contents Class Objects.
Computer Science 111 Fundamentals of Programming I
Functions CIS 40 – Introduction to Programming in Python
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Section 11.1 Class Variables and Methods
Introduction to Object-oriented Program Design
Object Oriented Programming
Object-Oriented Programming
User Defined Classes CS F.
Teach A-Level Computer Science: Object-Oriented Programming in Python
Object-oriented Design in Processing
CS190/295 Programming in Python for Life Sciences: Lecture 7
Fundamentals of Programming I Windows, Labels, and Command Buttons
Objects (again) Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Computer Science 111 Fundamentals of Programming I User Interfaces
Fundamentals of Programming I The Model/View/Controller Design Pattern
Fundamentals of Programming I More Data Modeling
Classes CS 21a: Introduction to Computing I
A Level Computer Science Topic 6: Introducing OOP
Presentation transcript:

Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes

Objects, Classes, and Methods Every data value in Python is an object Every object is an instance of a class Built in classes include int, float, str, tuple, list, dict A class includes operations (methods) for manipulating objects of that class (append, pop, sort, find, etc.) Operators (==, [], in, +, etc.) are “syntactic sugar” for methods

What Do Objects and Classes Do for Us? An object bundles together data and operations on those data A computational object can model practically any object in the real (natural or artificial) world Some classes come with a programming language Any others must be defined by the programmer

Programmer-Defined Classes The EasyFrame class is used to create GUI windows that are easy to set up The Image class is used to load, process, and save images Like the built-in classes, these classes include operations to run with their instances

Other Examples A Student class represents information about a student and her test scores A Rational class represents rational numbers and their operations A Die class represents dice used in games SavingsAccount, CheckingAccount, Bank, and ATM are used to model a banking system Proton, Neutron, Electron, and Positron model subatomic particles in nuclear physics

The Die Class: Its Interface and Use die.py # The module for the Die class Die() # Returns a new Die object roll() # Resets the die's value getValue() # Returns the die's value

The Die Class: Its Interface and Use die.py # The module for the Die class Die() # Returns a new Die object roll() # Resets the die's value getValue() # Returns the die's value Use from die import Die d = Die() # Create a new Die object d.roll() # Roll it print(d.getValue()) # Display its value help(Die) # Look up the documentation

Specifying an Interface The user of a class is only concerned with learning the information included in the headers of the class’s methods This information includes the method name and parameters Collectively, this information comprises the class’s interface Docstrings describe what the methods do

Defining (Implementing) a Class The definition or implementation of a class includes completed descriptions of an object’s data and the methods for accessing and modifying those data The data are contained in instance variables and the methods are called instance methods Related class definitions often occur in the same module

Syntax Template for a Simple Class Definition <docstring for the module> <imports of other modules used> class <name>(<parent class name>): <docstring for the class> <method definitions> Basically a header followed by several method definitions

Defining the Die Class We’ll use random.randint to roll the die from random import randint class <name>(<parent class name>): <docstring for the class> <method definitions> We’ll use random.randint to roll the die

The Class Header from random import randint class Die(object): <docstring for the class> <method definitions> By convention, programmer-defined class names are capitalized in Python Built-in class names, like str, list, and object, are not Like built-in function names, built-in class names appear in purple

The Class Header All Python classes are subclasses of the object class from random import randint class Die(object): <docstring for the class> <method definitions> All Python classes are subclasses of the object class A class can inherit behavior from its parent class

The Class Docstring from random import randint class Die(object): """This class represents a six-sided die.""" <method definitions> A class’s docstring describes the purpose of the class

Setting the Initial State from random import randint class Die(object): """This class represents a six-sided die.""" def __init__(self): self.value = 1 A method definition looks a bit like a function definition The __init__ method (also called a constructor) is automatically run when an object is instantiated; this method usually sets the object’s initial state (d = Die())

The self Parameter from random import randint class Die(object): """This class represents a six-sided die.""" def __init__(self): self.value = 1 The name self must appear as the first parameter in each instance method definition Python uses this parameter to refer to the object on which the method is called

Instance Variables from random import randint class Die(object): """This class represents a six-sided die.""" def __init__(self): self.value = 1 self must also be used with all instance method calls and instance variable references within the defining class self refers to the current object (a die)

Using Instance Variables from random import randint class Die(object): """This class represents a six-sided die.""" def __init__(self): self.value = 1 def roll(self): """Resets the die's value.""" self.value = randint(1, 6) def getValue(self): return self.value self.value refers to this object’s instance variable

Where Are Classes Defined? Like everything else, in a module Define the Die class in a die module Related classes usually go in the same module (SavingsAccount and Bank the bank module)

Continue in Chapter 9 No quiz! For Monday Continue in Chapter 9 No quiz!