CMSC201 Computer Science I for Majors

Similar documents
CMSC201 Computer Science I for Majors

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

CMSC201 Computer Science I for Majors

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

Object Oriented Programming

MITOCW watch?v=flgjisf3l78

CSC148H Week 1 >> COURSE INFORMATION, RECAP, OOP. Sadia Rain Sharmin Week of May 7, 2018

ECE 364 Software Engineering Tools Laboratory. Lecture 7 Python: Object Oriented Programming

Classes and Objects 1

Object oriented programming Concepts

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Credit: The lecture slides are created based on previous lecture slides by Dan Zingaro.

Object Oriented Software Development CIS Today: Object Oriented Analysis

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CMSC201 Computer Science I for Majors

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

CMSC 201 Computer Science I for Majors

OBJECT ORIENTED PROGRAMMING 7

OBJECT ORIENTED PROGRAMMING 7

Inheritance and Polymorphism

Refreshing last time

Programming I. Course 9 Introduction to programming

OBJECT ORIENTED PROGRAMMING

Sorting. Sorting. Selection sort

CS-202 Introduction to Object Oriented Programming

PREPARING FOR THE FINAL EXAM

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

CMSC201 Computer Science I for Majors

ITI Introduction to Computing II

OBJECT ORIENTED PROGRAMMING 6

Iterators & Generators

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

CMSC201 Computer Science I for Majors

COMP 250 Fall inheritance Nov. 17, 2017

CS1210 Lecture 28 Mar. 27, 2019

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

CMSC 202. Generics II

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

ITI Introduction to Computing II

Structured Programming

CMSC201 Computer Science I for Majors

What is an algorithm?

Python for Finance. Advanced Features. Andras Niedermayer

Lecture 19: Subclasses & Inheritance (Chapter 18)

CS1 Lecture 27 Mar. 26, 2018

CME 193: Introduction to Scientific Python Lecture 6: Classes and iterators

At full speed with Python

Object-Oriented Programming Concepts

Lecture 21: The Many Hats of Scala: OOP 10:00 AM, Mar 14, 2018

Lab 2: Object-Oriented Design 12:00 PM, Jan 31, 2018

PREPARING FOR PRELIM 2

Java Object Oriented Design. CSC207 Fall 2014

Outline. Outline. 1 Chapter 2: Data Abstraction

CS108 Lecture 16: User Defined Classes. Overview/Questions

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

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

Inheritance (Outsource: )

Object-Oriented Design

CMSC201 Computer Science I for Majors

CSC207 Week 3. Larry Zhang

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

CS 11 python track: lecture 4

Lecture 20. Subclasses & Inheritance

CSI33 Data Structures

PREPARING FOR THE FINAL EXAM

Generalized Code. Fall 2011 (Honors) 2

Intro. Classes & Inheritance

CS61B Fall 2014 Guerrilla Section 2 This worksheet is quite long. Longer than we ll cover in guerrilla section.

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

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

A Crash Course in Python Part II. Presented by Cuauhtémoc Carbajal ITESM CEM

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Week 8 Lecture: Programming

COMP 110/L Lecture 19. Kyle Dewey

INHERITANCE AND INTERFACES 7

Programming with Python

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

ECE 122. Engineering Problem Solving with Java

OBJECT ORIENTED PROGRAMMING 5

Lab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018

More on Objects in JAVA TM

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Python Objects. Charles Severance. Python for Everybody

Review 2. Classes and Subclasses

CSCA08 Winter 2018 Week 9:Object-Oriented Programming. Marzieh Ahmadzadeh,, Brian Harrington University of Toronto Scarborough

Object-Oriented Programming

Lecture 38: Python. CS 51G Spring 2018 Kim Bruce

Detection of methods (or missing methods): Cleaning up some details of our implementation. Better Method Convention (1) --self

Defining Classes and Methods

Programming in C# Inheritance and Polymorphism

CMSC202 Computer Science II for Majors

Chapter 2: Objects, classes and factories

Object- Oriented Design with UML and Java Part I: Fundamentals

Programming in the Large II: Objects and Classes (Part 1)

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Transcription:

CMSC201 Computer Science I for Majors Lecture 25 Classes All materials copyright UMBC and Dr. Katherine Gibson unless otherwise noted

Run time Last Class We Covered Run time of different algorithms Selection, Bubble, and Quicksort Linear and Binary search Asymptotic Analysis Big O, Ω, and θ What makes an algorithm run in best case time 2

3 Any Questions from Last Time?

Today s Objectives To learn about the principles of OOP (Object-Oriented Programming) Encapsulation Abstraction To learn about classes (in Python) How they work at a high level Cool stuff like inheritance and overriding 4

Note on Today s Topic We are covering classes only at a conceptual level in CMSC 201 You ll learn classes in detail in CMSC 202 (C++) Do not worry about the exact details of how something works or is written in code Python and C++ classes look very different 5

6 Procedural vs OOP

Procedural Programming Procedural programming uses: Data structures (like integers, strings, lists) Functions (like printvendingmachine() ) In procedural programming, information must be passed to the function Functions and data structures are not linked 7

Object-Oriented Programming (OOP) Object-Oriented programming uses Classes! Classes combine the data and their relevant functions into one entity The data types we use are actually classes! Strings have built-in functions like lower(), join(), strip(), etc. 8

Procedural vs OOP Procedural Calculate the area of a circle given the specified radius Sort this class list given a list of students Calculate the student s GPA given a list of courses Object-Oriented Circle, you know your radius, what is your area? Class list, sort your students Transcript, what is this student s GPA? 9

10 Abstraction and Encapsulation

Abstraction All programming languages provide some form of abstraction Hide the details of implementation from the user User doesn t need to know how an engine works in order to drive a car Do you know how append() works? No, but you can still use it! 11 Image from wikimedia.org

Encapsulation Encapsulation is a form of information hiding and abstraction Data and functions that act on that data are located in the same place (inside a class) Class methods are called on a class object They know everything about that object already Remember, classes contain code and data! 12

13 Classes

What is a Class? According to the dictionary: A set, collection, group, or configuration containing members regarded as having certain attributes or traits in common According to OOP principles: A group of objects with similar properties, common behavior, common relationships with other objects, and common semantics 14

Class Vocabulary A class is a special data type which defines how to build a certain kind of object 15 Instances are objects that are created which follow the definition given inside of the class Every instance of a class has both attributes and methods Method is just another word for function, often used when talking about classes

Blueprints Classes are blueprints for creating objects A dog class to create dog objects A car class to create carobjects 16 The blueprint defines The class s attributes (properties) As variables The class s behaviors (functions) As methods

Objects Each instance of a class is called an object of that class type You can create as many instances of a class as you want Just like a regular data type, like int or float There can be more than one dog or one car Multiple dog objects, multiple car objects 17

18 Creating a Class

Defining a Class When we create a new class, we must define its attributes and methods Once we ve done that, we can create instances Think about it in terms of parts of speech Objects are nouns ( my dog, Arun s car ) Attributes are adjectives ( big, brown, old ) Methods are verbs ( speak, reverse, play ) 19

Built-In Functions Classes have two important built-in functions Have double underscores on either side of name 20 init Constructor for the class Initializes and creates attributes str Defines how to turn an instance into a string Used when we call print() with an instance

21 Familiar Objects Objects like integers, lists, and Booleans also have constructors and string representations To create an integer, we could use newint = int() To print a list, we could use print(mylist) This will print it out with square brackets

Constructors Every class must have a constructor How a new object is created A class constructor will Supply default values for attributes Initialize the object and its attributes Constructors are automatically called when an object is created 22

Class Definition Example class animal: def init (self, species, name, age): self.species = species self.name = name self.age = age def speak(self): print("\"" + str(self.species) + \ " noise\"") 23

constructor Class Definition Example class animal: def init (self, species, name, age): self.species = species self.name self.age class name = name = age setting attributes method def speak(self): print("\"" + str(self.species) + \ " noise\"") 24

Class Definition Example class animal: def init (self, species, name, age): self.species = species self.name = name self.age = age def speak(self): print("\"" + str(self.species) + \ " noise\"") Notice that everything is indented under the class animal: line of code 25

Class Usage Example To create an instance of a class (a class object), use the class name, pass it the values for the attributes, and assign to a variable # create an animal object (species: sheep) variable1 = animal("sheep", "Dolly", 6) # create your own animal object! variable2 = animal("dog", "Fido", 7) 26

The self Variable The self variable is how we refer to the current instance of the class In init, self refers to the object that is currently being created In other methods, self refers to the instance the method was called on def speak(self): print("\"" + str(self.species) + " noise\"") 27

28 Inheritance

Inheritance Inheritance is when one class (the child class) is based upon another class (the parent class) The child class inherits most or all of its features from the parent class it is based on It is a very powerful tool available to you with Object-Oriented Programming 29

Inheritance Example For example: computer science students are a specific type of student They share attributes with every other student We can use inheritance to use those already defined attributes and methods of students for our computer science students 30

Inheritance Vocabulary 31 The class that is inherited from is called the Parent class Ancestor Superclass The class that does the inheriting is called a Child class Descendant Subclass

32 Inheritance Code To create a child class, put the name of the parent class in parentheses when you initially define the class class cmscstudent(student): Now the child class cmscstudent has the properties and functions available to the parent class student

Extending a Class We may also say that the child class is extending the functionality of the parent class Child class inherits all of the methods and data attributes of the parent class Also has its own methods and data attributes We can even redefine parent methods! 33

34 Redefining and Extending Methods

Redefining Methods Redefining a method is when a child class implements its own version of that method To redefine a method, include a new method definition with the same name as the parent class s method in the child class Now child objects will use the new method 35

Redefining Example Here, we have an animal class as the parent and a dog class as the child class animal: # rest of class definition def speak(self): print("\"" + self.species + " noise\"") class dog(animal): def speak(self): print("woof woof bark!") 36

Extending Methods Instead of completely overwriting a method, we can also extend it for the child class Want to execute both the original method in the parent class and some new code in the child class To do this, we must explicitly call the parent s version in the child 37

Extending Example Extending the str method for dog Used when we print() an object def str (self): # get the result from parent str msg = animal. str (self) # add information about the breed msg += "\n\ttheir breed is " + str(self.breed) return msg 38

39 Live Code Demo

Why Use Classes? Classes can simplify and streamline your code Imagine if Project 2 had a snack class Attributes: name, price, quantity, code Methods: buyone(), writetofile(), init, etc. Would have let us use 2D lists instead of 3D 40 Side Note: do not use classes for Project 3 The data is simple enough that it s not needed

Announcements Final is when? Friday, May 19th from 6 to 8 PM Start studying now! Review worksheet won t come out until Saturday Project 3 out now Project due on Friday, May 12th @ 8:59:59 PM 41