Origins of Long Range Dependence Myths and Legends Aleksandar Kuzmanovic 01/08/2001.
EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University
-
Upload
dana-overton -
Category
Documents
-
view
219 -
download
3
Transcript of EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University
![Page 1: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/1.jpg)
EECS 110: Lec 14: Classes and Objects
Aleksandar Kuzmanovic
Northwestern University
http://networks.cs.northwestern.edu/EECS110-s14/
![Page 2: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/2.jpg)
A whole new class of programming
EECS 110 today
Fri., 5/30: recitation Tue., 6/3: recitation, W. lab
HW6 Details
Projects Final!
The Date class
Mon., 6/2: rev. for finalWed., 6/4, final
Lab and HW #6Pr1 (the Date class)Pr2 (the Connect4Board)Pr3 (the Connect4Player (e.c.))
Due Sunday
Fri., 5/23: online
Projects
Wed., 5/28: class
![Page 3: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/3.jpg)
Lists, Tuples, Dictionaries, …
+ lots of computer work for the programmer's work!
T = {'abe' :['homer','herb'], 'jackie':['marge','patty','selma'], 'homer' :['hugo','bart','lisa','maggie'], 'marge' :['hugo','bart','lisa','maggie']}
e.g., the Simpson's dictionary…
![Page 4: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/4.jpg)
Lists, Tuples, Dictionaries, …
+
-
lots of computer work for the programmer's work!
no options as to data organization…
limited to square-bracket naming, e.g.,
fairly generic capabilities, e.g., len, print
A[i]
list int float strA A[0] A[1] A[2]
A = [ 42, 3.1, '!']
42 3.1 '!'
T = {'abe' :['homer','herb'],…}
T.getChildren('abe') NOT POSSIBLE!have to use T['abe']
![Page 5: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/5.jpg)
Lists, Tuples, Dictionaries, …
+
-
lots of computer work for little programmer work!
no options as to data organization…
limited to square-bracket naming, e.g.,
fairly generic capabilities, e.g., len, print
A[i]
Classes and Objects take care of all 3 drawbacks...
list int float strA A[0] A[1] A[2]
A = [ 42, 3.1, '!']
42 3.1 '!'
![Page 6: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/6.jpg)
Classes & Objects
An object-oriented programming language allows you to build your own customized types of variables.
(1) A class is a type of variable.
(2) An object is one such variable.
There will typically be MANY objects of a single class.
![Page 7: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/7.jpg)
Examples…Python's class libraries…
Graphics libraries
http://docs.python.org/lib/
![Page 8: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/8.jpg)
Using objects and classes:
>>> z = 3 + 4j
>>> dir(z)
all of the data members and methods of the complex class (and thus the object z !)
>>> z.imag
4.0
>>> z.conjugate()
3-4j
A particularly complex example…
a data member of all objects of class complex
its value for this object, z
its return value for this object, z
a method (function) within all objects of class complex
![Page 9: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/9.jpg)
Objects
An object is a data structure (like a list), except
(1) Its data elements have names chosen by the programmer.
(2) Data elements are chosen & organized by the programmer
(3) An object can have behaviors built-in by the programmer.
![Page 10: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/10.jpg)
Objects
An object is a data structure (like a list), except
(1) Its data elements have names chosen by the programmer.
(2) Data elements are chosen & organized by the programmer
(3) An object can have behaviors built-in by the programmer.
usually called "methods" instead of functions
![Page 11: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/11.jpg)
Date
this is an object of type Date
>>> d = Date(1,1,2012)
>>> d
1/1/2012
This is a class. It is a user-defined datatype (that you'll build in Lab this week!)
this is a CONSTRUCTOR …
the representation of a particular object of type Date
>>> d.isLeapYear()True
>>> d2 = Date(12,31,2013)
>>> d2
12/31/2013
>>> d2.isLeapYear()
False
the isLeapYear method returns True or False. How does it know what year to check?
How does it know to return False, instead of True in this case ??
Another object of type Date - again, via the constructor.
![Page 12: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/12.jpg)
class Date: """ a blueprint (class) for objects that represent calendar days """ def __init__( self, mo, dy, yr ): """ the Date constructor """ self.month = mo self.day = dy self.year = yr
def __repr__( self ): """ used for printing Dates """ s = "%02d/%02d/%04d" % (self.month, self.day, self.year) return s
def isLeapYear( self ): """ anyone know the rule? """
The Date class
![Page 13: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/13.jpg)
>>> d = Date(1,1,2012)
>>> d
1/1/2012
self
These methods need access to the
object that calls them
>>> d.isLeapYear()True
>>> d2 = Date(12,31,2013)
>>> d2
12/31/2013
>>> d2.isLeapYear()
False
is the specific OBJECT THAT CALLS A METHOD
These methods need access to the
object that calls them
![Page 14: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/14.jpg)
a Leap of faith….
class Date: def __init__( self, mo, dy, yr ): (constructor) def __repr__(self): (for printing)
def isLeapYear( self ): """ here it is """ if self.year%400 == 0: return True if self.year%100 == 0: return False if self.year%4 == 0: return True return False
![Page 15: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/15.jpg)
Date
>>> d = Date(1,1,2014)
>>> d
1/1/2014
always created with the CONSTRUCTOR …
>>> d.yesterday()
>>> d
12/31/2013
>>> d.subNDays(35)
lots of printing…
>>> d
the yesterday method returns nothing at all. Is it doing anything?
Why is this important?
Some methods return a value; others change the object that call it!
d has changed!
![Page 16: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/16.jpg)
Date ids
>>> d = Date(11,10,2014)
>>> d
11/10/2014
>>> d2 = Date(11,11,2014)
>>> d2
11/11/2014
this initializes a different Date!
>>> d == d2?
>>> d2.yesterday()
>>> d == d2
?
![Page 17: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/17.jpg)
Date ids
>>> d = Date(11,10,2014)
>>> d
11/10/2014
>>> d2 = Date(11,11,2014)
>>> d2
11/11/2014
this initializes a different Date!
>>> d == d2False
>>> d2.yesterday()
>>> d == d2
?
![Page 18: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/18.jpg)
Date ids
>>> d = Date(11,10,2014)
>>> d
11/10/2008
>>> d2 = Date(11,11,2014)
>>> d2
11/11/2014
this initializes a different Date!
>>> d == d2False
>>> d2.yesterday()
>>> d == d2
False
![Page 19: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/19.jpg)
Double Date
>>> d = Date(11,10,2014)
>>> d
11/10/2014
>>> d.addNDays(36)
>>> d2
?
>>> d2 = d.copy()
>>> d2 == d
?
>>> d.equals(d2)
?
![Page 20: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/20.jpg)
Double Date
>>> d2 = d
>>> d
11/10/2014
>>> d.addNDays(36)
>>> d2
12/16/2014
>>> d2 = d.copy()
>>> d2 == d
?
>>> d.equals(d2)
?
![Page 21: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/21.jpg)
Double Date
>>> d2 = d
>>> d
11/10/2014
>>> d.addNDays(36)
>>> d2
12/16/2014
>>> d2 = d.copy()
>>> d2 == d
False
>>> d.equals(d2)
?
![Page 22: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/22.jpg)
Double Date
>>> d2 = d
>>> d
11/10/2014
>>> d.addNDays(36)
>>> d2
12/16/2014
>>> d2 = d.copy()
>>> d2 == d
False
>>> d.equals(d2)
True
![Page 23: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/23.jpg)
class Date: def __init__( self, mo, dy, yr ): def __repr__(self): def isLeapYear(self):
def copy(self): """ returns a DIFFERENT object w/SAME date! """
def equals(self, d2): """ returns True if they represent the same date; False otherwise """
More Date
![Page 24: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/24.jpg)
class Date: def __init__( self, mo, dy, yr ): def __repr__(self): def isLeapYear(self):
def copy(self): """ returns a DIFFERENT object w/SAME date! ""“
return Date(self.month, self.day, self.year)
def equals(self, d2): """ returns True if they represent the same date; False otherwise """
More Date
![Page 25: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/25.jpg)
class Date: def __init__( self, mo, dy, yr ): def __repr__(self): def isLeapYear(self):
def copy(self): """ returns a DIFFERENT object w/SAME date! ""“
return Date(self.month, self.day, self.year)
def equals(self, d2): """ returns True if they represent the same date; False otherwise ""“
return self.month == d2.month and self.day == d2.day and self.year == d2.year
More Date
![Page 26: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/26.jpg)
"Quiz"class Date:
def isBefore(self, d2): """ if self is before d2, this should return True; else False """ if self.year < d2.year: return True if self.month < d2.month: return True if self.day < d2.day: return True return False
def tomorrow(self): """ moves the date that calls it ahead 1 day """ DIM = [0,31,28,31,30,31,30,31,31,30,31,30,31]
This method is WRONG! Find why … and suggest how you could fix it!
Write this tomorrow method.
It does not return anything. It just CHANGES the date
object that calls it.
![Page 27: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/27.jpg)
class Date:
def isBefore(self, d2): """ if self is before d2, this should return True; else False """ if self.year < d2.year: return True if self.month < d2.month: return True if self.day < d2.day: return True return False
What's wrong?
![Page 28: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/28.jpg)
def isBefore(self, d2): """ Returns true if self is before d2 """ if self.year < d2.year: return True if self.month < d2.month and self.year == d2.year: return True if self.day < d2.day and d2.month == self.month and \ self.year == d2.year: return True return False
![Page 29: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/29.jpg)
class Date:
def tomorrow(self): """ moves the date that calls it ahead 1 day """ DIM = [0,31,28,31,30,31,30,31,31,30,31,30,31]
# Leap years not accounted forself.day +=1if self.day > DIM[self.month]:
self.day = 1self.month += 1if self.month > 12:
self.month = 1self.year += 1
![Page 30: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/30.jpg)
Add to Date these methods
Lab tomorrow
yesterday(self)
tomorrow(self)
addNDays(self, N)
subNDays(self, N)
isBefore(self, d2)
isAfter(self, d2)
diff(self, d2)
diffDebug(self, d2)
dow(self)
and use your Date class to analyze our calendar a bit…
![Page 31: EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University](https://reader030.fdocuments.net/reader030/viewer/2022032701/56649c7e5503460f94933546/html5/thumbnails/31.jpg)
See you in Lab !