CS104: Chapter 15 CS 104 Students and me. Big Question Q: You say creating a class is how to create a new type. Why would we even want to do this?! A:

Slides:



Advertisements
Similar presentations
Section 6.1 CS 106, Fall The Big Q What do we get by being able to define a class?! Or Do we really need this?!
Advertisements

Python Objects and Classes
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
CS 221 Chapter 2 Excel. In Excel: A1 = 95 A2 = 95 A3 = 80 A4 = 0 =IF(A1
Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.
Copyright © Texas Education Agency, Computer Programming Class Basics.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
Python Programming Chapter 12: Classes and Objects Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2010/2011.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
From Class Diagrams to Databases. So far we have considered “objects” Objects have attributes Objects have operations Attributes are the things you record.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
Classes and Objects, Part 1 Victor Norman CS104. Reading Quiz, Q1 A class definition define these two elements. A. attributes and functions B. attributes.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
11/27/07. >>> Overview * objects * class * self * in-object methods * nice printing * privacy * property * static vs. dynamic * inheritance.
1 Chapter 3 and 6– Classes, Objects and Methods Object-Oriented Programming Concepts – Read it from Java TutorialJava Tutorial Definition: A class is a.
Data Abstraction UW CSE 140 Winter What is a program? – A sequence of instructions to achieve some particular purpose What is a library? – A collection.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
OBJECTS AND CLASSES CITS1001. Concepts for this lecture class; object; instance method; parameter; signature data type multiple instances; state method.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Files Victor Norman CS104. Reading Quiz, Q1 A file must be ___________ before a program can read data from it. A. accessed B. unlocked C. opened D. controlled.
Classes 1 COMPSCI 105 S Principles of Computer Science.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
+ Types of Data IST210 Class Lecture. + What kind of data do you have? Class Schedules Contacts Names Phone Numbers Address Food Specials Movies,
Object Oriented Programing (OOP)
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
CS 350 – Software Design Expanding Our Horizons – Chapter 8 The traditional view of objects is that they are data with methods. Sometimes objects could.
Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!
Classes and Objects, Part 1 Victor Norman CS104. “Records” In Excel, you can create rows that represent individual things, with each column representing.
Chapter 17 Q and A Victor Norman, et al. CS104. What is Object-oriented Programming? Q: What is object-oriented programming? A: It means defining classes/objects,
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Q and A for Sections 6.2, 6.3 Victor Norman CS106.
Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
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.
A High Flying Overview CS139 – Fall 2010 How far we have come.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
Re-Intro to Object Oriented Programming
Databases Chapter 16.
Topics Procedural and Object-Oriented Programming Classes
Objects as a programming concept
The Object-Oriented Thought Process Chapter 1
CS-104 Final Exam Review Victor Norman.
CITA 215 Section 3 Data Modeling.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Object-oriented programming
CS139 – Fall 2010 How far we have come
More Loop Examples Functions and Parameters
Teach A-Level Computer Science: Object-Oriented Programming in Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Arrays
An Introduction to Object Orientated Programming
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Objects (again) Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction.
Which best describes the relationship between classes and objects?
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Object-Oriented Programming
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

CS104: Chapter 15 CS 104 Students and me

Big Question Q: You say creating a class is how to create a new type. Why would we even want to do this?! A: First, to group data together that belongs together. (Second, to group operations on the data with the data. But, more on that, in due course.)

Grouping Data The only data structures we have are lists and tuples. We can store multiple related data items together in a list or tuple, and then keep a list of these lists/tuples. E.g., cars = [ (“Honda”, “Odyssey”, 2001, “silver”, “VIN1523y7380”), (“Toyota”, “Prius”, 2007, “blue”, “VIN X95”), …]

Accessing Data If we need to compute with the i-th car’s color, we do: car = cars[i] # car is a tuple if car[3] == “blue”: # do something… Or, was it car[4]? car[5]? If we have 55 items to keep for a car how do we remember which number is in which place in the tuple of a car? How can we make the code readable?

A better way Would be nice to make a container to hold multiple data items, and each attribute in the structure has a nice name. Kind of like a row in Excel, where you give a name to each column. Then, you could access the cars’ colors, by car = cars[i] if car.color == “blue”: # do something…

So… We need a way to group multiple attributes of something into a single container, and then be able to access, with nice names, the attributes in the container. Enter: the class.

Terminology A class defines the template or recipe for what an object looks like – i.e., what attributes it has. Defining a class does not make any variables (called objects). It just defines what an object would hold if you did create one. You instantiate a class to make an object, aka a variable. An object is also called an instance of a class.

Example: Car class class Car: # “class” is like “def”, but for # classes, not functions. def __init__(self): “””Constructor for a Car.””” self._make = “” self._model = “” self._yr = 1900 self._color = “” # main code: instantiate some car objects car1 = Car() # call __init__, the constructor. car1._make = “Honda” car1._model = “Odyssey” # etc car1._yr = 2001 car1._color = “silver”

Using a car instance Now that we’ve wrapped up multiple attributes into one container (i.e., “object”), and given each a nice readable name, we can deal with each container as one thing. – get/set values in it – pass it as a parameter – return it from a function – sort the objects, without mixing up whose values go with whose…

Technical Details If student is an object (of class Student) with attribute, id, you can access it with student.id. Similar to module.variable or module.func() module != class. Module is a file containing variable, function (and class) definitions. Class is a new type. But both are containers.

Using objects You can pass to functions Return from functions Use in computations, etc.

Q and As Can you talk more about the object diagrams? Can you walk through the example in which you are making/defining the class point on page 3 -- specifically where “p is an alias for blank”? Is there a limit to the amount of attributes a class can have?

Q and As (2) We talk about how objects are mutable, but if we can define the attributes of a created object can we define them to be immutable? Is a “class definition” like a “function definition” with just a different kind of pattern? (Does it work similar to a function definition?)

Q and A (3) Why doesn't the class “Point” clarify what its attributes are? How do you know what the attributes of a class are?