CS1210 Lecture 28 Mar. 27, 2019

Similar documents
Objects and Classes. Chapter 8

CS1 Lecture 3 Jan. 18, 2019

CS1 Lecture 22 Mar. 6, 2019

MITOCW watch?v=flgjisf3l78

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Recall. Key terms. Review. Encapsulation (by getters, setters, properties) OOP Features. CSC148 Intro. to Computer Science

CS1 Lecture 3 Jan. 22, 2018

PREPARING FOR PRELIM 2

CMSC201 Computer Science I for Majors

CS1 Lecture 5 Jan. 26, 2018

Logistics. Half of the people will be in Wu & Chen The other half in DRLB A1 Will post past midterms tonight (expect similar types of questions)

CS150 Sample Final. Name: Section: A / B

CS1 Lecture 27 Mar. 26, 2018

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Review for the Final Exam CS 8: Introduction to Computer Science, Winter 2018 Lecture #15

Intro. Scheme Basics. scm> 5 5. scm>

OBJECT ORIENTED PROGRAMMING 6

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

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

CS1 Lecture 2 Jan. 16, 2019

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

CS1 Lecture 5 Jan. 25, 2019

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

: Dimension. Lecturer: Barwick. Wednesday 03 February 2016

CSI33 Data Structures

PREPARING FOR THE FINAL EXAM

CS1 Lecture 9 Feb. 5, 2018

CS1 Lecture 13 Feb. 13, 2019

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CSE : Python Programming

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Classes and Objects 1

CS150 Sample Final Solution

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Pointers in C/C++ 1 Memory Addresses 2

Lecture 19. Operators and Abstraction

Programming I. Course 9 Introduction to programming

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

CS3157: Advanced Programming. Announcement

CS112 Lecture: Defining Instantiable Classes

CS1 Lecture 4 Jan. 23, 2019

CMSC 201 Fall 2018 Lab 04 While Loops

MUTABLE LISTS AND DICTIONARIES 4

Objective-C. Stanford CS193p Fall 2013

Text Input and Conditionals

CS 105 Perl: Modules and Objects

Introduction to Python

Programming Paradigms in Python

Lecture 2. Object Orientation

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

S206E Lecture 19, 5/24/2016, Python an overview

CME 193: Introduction to Scientific Python Lecture 4: Strings and File I/O

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

CMSC201 Computer Science I for Majors

Software Engineering /48

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Overview of List Syntax

The Pyth Language. Administrivia

CS61A Lecture 15 Object Oriented Programming, Mutable Data Structures. Jom Magrotker UC Berkeley EECS July 12, 2012

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Programming for Engineers in Python

Finally, all fields are public by default, so you can easily write simple functions, such as this one, without writing getters:

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

File Operations. Working with files in Python. Files are persistent data storage. File Extensions. CS111 Computer Programming

CS150 - Sample Final

T H E I N T E R A C T I V E S H E L L

Lecture 23: Priority Queues 10:00 AM, Mar 19, 2018

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

3 ADT Implementation in Java

Lecture 1: Overview

Starting to Program in C++ (Basics & I/O)

Object Oriented Programming

Midterm Exam (REGULAR SECTION)

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Introduction to Computer Systems

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

CS3157: Advanced Programming. Outline

CS 61C: Great Ideas in Computer Architecture Introduction to C

Example: Class MSDie Introduction to Graphs and NetworkX

File Operations. Working with files in Python. Files are persistent data storage. File Extensions. CS111 Computer Programming

CS1 Lecture 4 Jan. 24, 2018

CMSC 201 Spring 2017 Homework 4 Lists (and Loops and Strings)

CS1 Lecture 11 Feb. 9, 2018

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

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

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Dan Grossman Winter 2013

Software Design and Analysis for Engineers

Python Lists and Dictionaries CS 8: Introduction to Computer Science, Winter 2019 Lecture #13

2.4 Choose method names carefully

Java Bytecode (binary file)

Objects. say something to express one's disapproval of or disagreement with something.

Ruby logistics. CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Ruby: Not our focus. Ruby: Our focus. A note on the homework

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Lecture 21. Programming with Subclasses

Transcription:

CS1210 Lecture 28 Mar. 27, 2019 Discussion section exam scores posted score # people 0-5 6-10 11-15 16-20 21-25 26-30 28 48 39 37 30 9 median: 13 Some words about overall grades more detail next Wednesday HW6 Q1 and 2 out of 4 available One on Big-O, one using randomization Q3 and Q4 by Friday Today and next two lectures: Ch 15-18 Classes and Object-oriented programming

Ch15-18. Classes and Object-oriented (OO) programming This is a very important topic for modern programming. Many many real-world systems are heavily objectoriented. E.g. to program ios/iphone/ipad, you ll have to deal with large complex OO libraries/frameworks It s a very big topic. terms like: class, attribute, object, method, instance, inheritance, abstraction, encapsulation, information hiding, polymorphism, we ll cover the basics

Introduction to Classes defs lets us add new functions. Extremely useful for breaking down large program into components, building modules or libraries of computational tools classes let us define whole new types. Think of a class as a set of objects (the instances of the class)having 1) attributes and,possibly also 2) operations (called methods) defined on them. You are alraedy familiar with types: int, float, Boolean, string, list, tuple, dictionary with class definitions you can create your own types. Programs can be much clearer, easier to understand and maintain when written in terms of appropriate types and instances of those types Instead of using, say, a list or dictionary to represent a person: p = [ jim, 58, blue, professor ] and using basic list operations to extract age p[1] # access age define and use a Person class and related attributes and operations p = Person( ) p.birthdate p.eyecolor p.occupation p.getage() p.computecreditrating()

Classes can provide abstraction. We can use objects without knowing details of how data is stored documentation tells you how to use objects but doesn t need to tell you implementation details. In fact, the implementation details can be changed without you having to worry about it # assumes personlists1 and 2 have birthdate stored at index 1 defolderthan (personlist1, personlist2): return (personlist1[1] < personlist2[1]) # assumes person1 and 2 are objects with birthdate attributes def olderthan(person1, person2): return (person1.birthdate < person2.birthdate) # assumes person1 and 2 are objects with getage() methods def olderthan(person1, person2): return (person1.getage() > person2.getage()) In the first example, the olderthan function needs to understand how a person is represented as a list in which the second element contains the age. In the second, we need to know that a Person object has a birthdate attribute. In the third, we only need to know the Person class has a getage() operation/method defined on it. We don t need to know exactly what attributes are used to represent a Person. We don t know and don t need to know.

Basic Python Types and Classes Basic python types are actually themselves classes. list objects are instances of the list class the operations defined for a class are called methods. You ve been using methods via the dot notation: [1,2,3].append(4) Earlier I suggested you think about such methods as strange function call syntax [1,2,3].append(4) à append([1,2,3],4) That is useful but if try it exactly like that, you ll get an exception >>> append([1,2,3],4) Methods are indeed functions just special ones specific to a class. The list append method is defined as part of the definition of the list class. Execute help(list) in Python shell to see things defined for the list class Turns out you can directly call append in plain function style, if we use append s full name list.append (the append function owned by the list class) >>> list.append([1,2,3],4) Similarly, see help(int). + actually shorthand for add method for integers. >>> a = 3 >>>a. add (4) (more on these foo functions later) 7

Defining classes In Python (and other languages) to define a class, you define object attributes (also often called properties) and the methods (operations) that can be invoked on objects (instances) of that class. General form: class Myclass (): classattribute1 = def method1(self, ): self.objectattribute1 = self.objectattribute2 = computation in terms of properties and arguments passed to method return def method2(self, ) computation in terms of properties and arguments passed to method... Note: variable name self is a convention (standard practice/usage). The first argument to a method is always the object that invoked the method. It is legal to name it anything but please stick to standard practice use self

Chapter 15: demonstrates classes as simple containers of attributes, but without methods. It is useful though most people would not call this object oriented programming. It s simply using classes as another simple container type like lists and dictionaries. E.g. >>> class Point: represents a point in 2D space >>> pt1 = Point() pt1 >>> pt1.x = 3.0 >>> pt1.y = 4.0 pt2 >>> pt2 = Point() x 3.0 y 4.0

Ch 15 E.g. >>> class Point: represents a point in 2D space >>> pt1 = Point() >>> pt1.x = 3.0 >>> pt1.y = 4.0 >>> pt2 = Point() pt1 >>> pt2.birthday = June??? pt2 We usually don t want to do things this way! An empty class definition (plus a comment) does not define the interface/api to the class. Instead, good practice is to define a class via methods that are used to work with instances of the class. And, though legal, it is often considered good practice not to access attributes directly (pt1.x) but only via methods ( getters and setters ) pt1.getx() x 3.0 y 4.0 Birthday June

Ch 15 E.g. >>> class Point: represents a point in 2D space >>> pt1 = Point() >>> pt1.x = 3.0 >>> pt1.y = 4.0 >>> pt2 = Point() pt1 >>> pt2.x = 5.0 >>> pt2.y = 8.0 >>> xdiff = pt2.x pt1.x >>> ydiff = pt2.y pt1.y pt2 xdiff ydiff x 3.0 y 4.0 Even though not super common to program this way, it is important to know how to access attributes since when you define methods you ll write will access attributes directly. x 5.0 Y 8.0 2.0 3.0

Ch 16 using classes in functions Like 15, is not really demonstrating object-oriented style. It demonstrates use of classes as containers for sets of properties. It mainly contains some examples of functions that do things with instances of user-defined classes like those in Ch15 that have no methods. Again, in object-oriented programming we don t usually want to do things this way writing top-level functions that access object attributes directly (point.x, etc.). It s more like using structs in a C HOWEVER, it can be useful Will demonstrate a Time class using this approach and then, after introducing methods, see how we can convert that to the more standard object-oriented style of Ch 17. ch16time.py

Ch 17 This is the chapter to study most carefully General rule for defining classes: define an init method initializing values for all properties/attributes (e.g. hour, minutes, seconds for time) define methods that represent the public interface to the class. Users should work with instances of the class only via these methods rather than by accessing object attributes directly Will redo ch16time.py in more standard objectoriented style. Plus other examples.

Friday and Monday Ch 17 next time classes with methods General rule for defining classes: define an init method initializing values for all properties/attributes (e.g. hour, minutes, seconds for time) define methods that represent the public interface to the class. Users should work with instances of the class only via these methods rather than by accessing object attributes directly Will redo ch16time.py in more standard objectoriented style. Plus other examples. Ch 18 important idea of inheritance