Example: Class MSDie Introduction to Graphs and NetworkX

Similar documents
Introduction to Computer Programming for Non-Majors

Introduction. We've seen Python useful for. This lecture discusses Object Oriented Programming. Simple scripts Module design

Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors

Introduction to Computer Programming for Non-Majors

Outline: Defining objects (Ch10)

Lecture. Classes. Richard E Sarkis CSC 161: The Art of Programming

Introduction to Computer Programming for Non-Majors

Lecture 21. Topics: Chapter 10. Defining Classes 10.4 Data processing with class 10.5 Objects and encapsulation 10.6 Widgets

Python Programming: An Introduction To Computer Science

Defining Classes. CS Recitation 11

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors

CS1004: Intro to CS in Java, Spring 2005

Object Fundamentals. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 2 08/30/2007

Object Fundamentals, Part One. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 2 08/27/2009

CS108 Lecture 16: User Defined Classes. Overview/Questions

Programming I. Course 9 Introduction to programming

Java Object Oriented Design. CSC207 Fall 2014

CS 116 Week 8 Page 1

mith College Computer Science Week 9 CSC111 - Fall 2018 Dominique Thiébaut

Data Structures (list, dictionary, tuples, sets, strings)

Comp-304 : Object-Oriented Design What does it mean to be Object Oriented?

Chapter 5 Object-Oriented Programming

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Outline. Outline. 1 Chapter 2: Data Abstraction

Week 8 Lecture: Programming

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

CS1210 Lecture 28 Mar. 27, 2019

3. Object-Oriented Databases

JAVA MOCK TEST JAVA MOCK TEST II

What is a class? Responding to messages. Short answer 7/19/2017. Code Listing 11.1 First Class. chapter 11. Introduction to Classes

Introduction to Computer Programming for Non-Majors

List of lectures. Lecture content. Lecture goal. Object Oriented Programming. Object-oriented and type. TDDA69 Data and Program Structure

Object-Oriented Software Engineering. Chapter 2: Review of Object Orientation

Introduction to Object-Oriented Programming

PREPARING FOR THE FINAL EXAM

CIS 194: Homework 11. Due Thursday, April 5. Risk. The Rand StdGen monad

Programming Exercise 14: Inheritance and Polymorphism

Object Oriented Programming

5.6.1 The Special Variable this

PREPARING FOR PRELIM 2

Announcements. Homework 1 due Monday 10/12 (today) Homework 2 released next Monday 10/19 is due 11/2

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CMSC201 Computer Science I for Majors

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Review sheet for Final Exam (List of objectives for this course)

CS61A Lecture 20 Object Oriented Programming: Implementation. Jom Magrotker UC Berkeley EECS July 23, 2012

Constraint Systems. Lab 06 - Customizing Search

Lecture 23: Object Lifetime and Garbage Collection

Assignment 1 due Monday at 11:59pm

Methods Common to all Classes

03 Pyro. Python Remote Objects. Distributed Object Technology. 100% pure Python Naming Service Mobile objects Exceptions transports

15CS45 : OBJECT ORIENTED CONCEPTS

OOAD - OBJECT MODEL. The concepts of objects and classes are intrinsically linked with each other and form the foundation of object oriented paradigm.

FINAL EXAM May 2017 CSC /02: Introduction to Computer Science

College Board. AP CS A Labs Magpie, Elevens, and Picture Lab. New York: College Entrance Examination Board, 2013.

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 00. WELCOME TO OBJECTVILLE. Speaking the Language of OO

System Analysis and Design/ Object-Oriented System Modeling

Object Oriented Programming

Object-Oriented Design (OOD) and C++

Software Engineering

Chapter 6 Introduction to Defining Classes

Object Oriented Programming

PERL Scripting - Course Contents

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Objects and Classes. Chapter 8

Chapter 11 Object and Object- Relational Databases

Object Oriented Issues in VDM++

Object Oriented Software Development CIS Today: Object Oriented Analysis

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Mat 2170 Week 9. Spring Mat 2170 Week 9. Objects and Classes. Week 9. Review. Random. Overloading. Craps. Clients. Packages. Randomness.

OO design. Classes, Responsibilities, Collaborations (CRC) 13/9/1999 COSC

CS313D: ADVANCED PROGRAMMING LANGUAGE

CHAPTER 9 DESIGN ENGINEERING. Overview

16 April 2011 Alan, Edison, etc, Saturday.

What is a Class? Short Answer. Responding to Messages. Everything in Python is an Object 11/8/2010

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

The Object Oriented Paradigm

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Chapter 13. Object-Oriented Databases (from E&N and my editing)

CHAPTER 5 GENERAL OOP CONCEPTS

Object Model. Object Orientated Analysis and Design. Benjamin Kenwright

CSI33 Data Structures

Chapter 10 INHERITANCE 11/7/16 1

OBJECT ORIENTED PROGRAMMING

Object-Oriented Programming Paradigm

Chapter 4: Writing Classes

Object oriented programming Concepts

Student Responsibilities. Mat 2170 Week 9. Notes About Using Methods. Recall: Writing Methods. Chapter Six: Objects and Classes

So far, we have seen two different approaches for using computing to solve problems:

Review for Midterm. Instructor: Scott Kristjanson CMPT 135 SFU Surrey, Spring 2016

CSE 70 Final Exam Fall 2009

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Announcements for this Lecture

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Transcription:

Example: Class MSDie Introduction to Graphs and NetworkX Monday, March 30, 2009 1 Be careful whose random number generator you trust http://xkcd.com/221/ 2 1

Reminders Sample exam questions have been posted on Blackboard Exam 2 in Thursday, April 2, 6:30 in Lab. Questions on Project 3? 4 Plots generated, 3 inside the simulation are optimal Plots will be similar for the two initial states What values are interesting change? steps (larger than 3000), the range of Dv (larger) 3 Class example: Multi-Sided Dice Design a generic class MSDie to model multi-sided dice. When a new object is created, we specify n, the number of sides it will have. Three methods will operate on the die: roll set the die to a random value between 1 and n, inclusive setvalue set the die to a specific value (i.e. cheat) getvalue see what the current value is. 4 2

Example: Multi-Sided Dice >>> die1 = MSDie(6) >>> die1.getvalue() 1 >>> die1.roll() >>> die1.getvalue() 5 >>> die2 = MSDie(13) >>> die2.getvalue() 1 >>> die2.roll() >>> die2.getvalue() 9 >>> die2.setvalue(8) >>> die2.getvalue() 8 5 Example: Multi-Sided Dice Using object-oriented vocabulary, we create a die by invoking the MSDie constructor and providing the number of sides as a parameter. Die objects will keep track of this number internally as an instance variable. Another instance variable is used to keep the current value of the die. We initially set the value of the die to be 1 because that value is valid for any die. That value can be changed by the roll and setroll methods, and returned by the getvalue method. 6 3

Example: Multi-Sided Dice # msdie.py # Class definition for an n-sided die. from random import randrange class MSDie: def init (self, sides): self.sides = sides self.value = 1 def roll(self): self.value = randrange(1, self.sides+1) def getvalue(self): return self.value def setvalue(self, value): self.value = value 7 Example: Multi-Sided Dice Class definitions have the form class <class-name>: <method-definitions> Methods look a lot like functions! Placing a function inside a class makes it a method of the class, rather than a stand-alone function. The first parameter of a method is always named self, which is a reference to the object on which the method is acting. 8 4

Example: Multi-Sided Dice Suppose we have a main function that executes die1.setvalue(8). Just as in function calls, Python executes the following sequence: 1. main suspends at the point of the method application. 2. Python locates the appropriate method definition inside the class of the object to which the method is being applied. 3. Control is transferred to the setvalue method in the MSDie class. 9 Example: Multi-Sided Dice 4.The formal parameters of the method get assigned the values supplied by the actual parameters of the call. In the case of a method call, the first formal parameter refers to the object: self = die1 value = 8 5.The body of the method is executed. 10 5

Example: Multi-Sided Dice 11 Example: Multi-Sided Dice Objects contain their own data. Instance variables provide storage locations inside of an object. Instance variables are accessed by name using our dot notation: <object>.<instance-var> self.value refers to the instance variable value inside the object. Each MSDie object has its own value. 12 6

Example: Multi-Sided Dice Certain methods have special meaning. Thet have names that start and end with two _ s. def init (self, sides): self.sides = sides self.value = 1 init is the object contructor. Python calls this method to initialize a new MSDie. init provides initial values for the instance variables of an object. 13 Example: Multi-Sided Dice Outside the class, the constructor is referred to by the class name: die1 = MSDie(6) When this statement is executed, a new MSDie object is created and init is executed on that object. The net result is that die1.sides is set to 6 and die1.value is set to 1. Instance variables can remember the state of a particular object This is different than local function variables, whose values disappear when the function terminates. 14 7

Object Oriented (OO) Design Any program using classes can be written as one without classes Defining new classes is a good way to modularize a program. Once some useful objects are identified, the implementation details of the algorithm can be moved into a suitable class definition. OO allows: encapsulation, polymorphism, inheritence, information hiding 15 Encapsulating Useful Abstractions The main program only has to worry about what objects can do, not about how they are implemented. In computer science, this separation of concerns is known as encapsulation. The implementation details of an object are encapsulated in the class definition, which insulates the rest of the program from having to deal with them. 16 8

Encapsulating Useful Abstractions One of the main reasons to use classes and objects is to hide the internal complexities of the objects from the programs that use them. From outside the class, all interaction with an object can be done using the interface provided by its methods. Advantage: one can change classes independently without worrying about breaking other parts of the program holds as long as the interface provided by the methods does not change 17 Other OO terminology Polymorphism having more than one form An expression can do different things depending on the type of object to which the variable refers to For example, + can mean addition or concatenation 18 9

Other OO terminology Inheritance Allows one to recycle code A class has the ability to extend or override functionality of another class Create subclasses and superclasses Information Hiding Protect some components of the object from external entities 19 10