SlideShare a Scribd company logo
1 of 28
Computer Science 151
An introduction to the art of
computing
Classes
Rudy Martinez
Classes
• A class describes a set of objects with the same behavior.
• String class
• Constants:
• Ascii_letters – all ascii_lowercase and ascii_uppercase letters
• Ascii_lowercase - 'abcdefghijklmnopqrstuvwxyz’
• Ascii_uppercase - 'ABCDEFGHIJKLMNOPQRSTUVWXYZ’
• Digits - '0123456789’
myString = 'Hello Cruel_World!'
for i in myString:
#myString[i] = random.choice(string.ascii_letters)
print(random.choice(string.ascii_letters))
4/7/2019CS151SP19
Class Methods
• The set of all methods provided by a class, together with a description of
their behavior, is called the public interface of the class.
• You can drive a car by operating the steering wheel and pedals, without knowing how
the engine works. Similarly, you use an object through its methods. The
implementation is hidden.
• String methods
• isapha() – returns bool if all characters are letters
• isdigit() – returns bool if characters are digits (numbers)
• islower() – returns bool if all characters are lowercase
import string
myString = 'Hello Cruel_World!'
for i in myString:
if i.isalpha:
print( i, ' is a Letter!')
else:
print(i, ' is not a letter!')
4/7/2019CS151SP19
Class Constructors
• A constructor defines and initializes the instance variables of an
object. The constructor is automatically called whenever an object is
created.
• The constructor is responsible for defining and initializing all of the
instance variables that are to be contained in the object. After the
constructor completes its work, a reference to the newly created and
initialized object is returned. The reference is saved in a variable so
we can later call methods on the object.
4/7/2019CS151SP19
Constructor Example
Class Definition
class Canine:
''' A simple dog class implementation '''
name = ''
breed = []
def __init__(self, name):
self.name = name
self.breed = []
Example Code
mydog = Canine('fido’)
• This creates an object of the class
Canine and specifies the name fido
• Does it specify the breed?
• Do we need to?
4/7/2019CS151SP19
Methods
Class Definition
• Need method to get name and
breed!
def get_name(self):
print(self.name)
def get_breed(self):
print(self.breed)
Example
pet = myDog.get_name()
• Hey you didn’t set the breed
anywhere?
4/7/2019CS151SP19
Set Method
Class Definition
• Lets define set_breed()
def add_breed(self, breed):
self.breed.append(breed)
Code
myDog.add_breed(‘Poodle’)
myDog.add_breed(‘Labrador’)
So what does our class look like
now?
4/7/2019CS151SP19
Class Example
class Canine:
''' A simple dog class implementation '''
name = ''
breed = []
def __init__(self, name):
self.name = name
self.breed = []
def add_breed(self, breed):
self.breed.append(breed)
def get_breed(self):
print(self.breed)
def get_name(self):
print(self.name)
• Name = ‘fido’
• Breed = [‘Poodle’, ‘Labrador’]
• The class is complete but it does very
little.
• Can we add dog age?
• Where would we put it?
• What accessor methods do we need?
4/7/2019CS151SP19
Class Use
• You know what a class is.
4/7/2019CS151SP19
Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
4/7/2019CS151SP19
Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
• You know how to create a simple class.
4/7/2019CS151SP19
Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
• You know how to create a simple class.
• Use keyword class followed by the class name.
4/7/2019CS151SP19
Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
• You know how to create a simple class.
• Use keyword class followed by the class name.
• Create private data objects
4/7/2019CS151SP19
Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
• You know how to create a simple class.
• Use keyword class followed by the class name.
• Create private data objects
• Create accessor methods to get and set private data objects
4/7/2019CS151SP19
Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
• You know how to create a simple class.
• Use keyword class followed by the class name.
• Create private data objects
• Create accessor methods to get and set private data objects
• When would you use a class?
4/7/2019CS151SP19
Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
• You know how to create a simple class.
• Use keyword class followed by the class name.
• Create private data objects
• Create accessor methods to get and set private data objects
• When would you use a class?
• Can you think of any reasons?
4/7/2019CS151SP19
Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
• You know how to create a simple class.
• Use keyword class followed by the class name.
• Create private data objects
• Create accessor methods to get and set private data objects
• When would you use a class?
• Can you think of any reasons?
• How about you have to write a program to control inventory in a factory?
4/7/2019CS151SP19
Class Use
• You know what a class is.
• A class describes a set of objects with the same behavior.
• You know how to create a simple class.
• Use keyword class followed by the class name.
• Create private data objects
• Create accessor methods to get and set private data objects
• When would you use a class?
• Can you think of any reasons?
• How about you have to write a program to control inventory in a factory?
• Why would you use a class?
• Can’t all this be programmed using what we already know?
4/7/2019CS151SP19
Deconstructing the problem?
• Writing good programs requires you to analyze the problem.
• What is the problem?
• Can the problem be solved incrementally?
• Is it even solvable, if not can you estimate an answer with the data on hand?
• What is the data?
• Is it being collected or given to you?
• What is common about the data?
4/7/2019CS151SP19
Deconstructing the problem?
• Writing good programs requires you to analyze the problem.
• What is the problem?
• Can the problem be solved incrementally?
• Is it even solvable, if not can you estimate an answer with the data on hand?
• What is the data?
• Is it being collected or given to you?
• What is common about the data?
4/7/2019CS151SP19
Deconstructing the problem?
• Classes are useful but as all tools are not the holy grail of
programming.
• As in our example all dogs have more in common then differences.
• What if we had Cats in our data not just dogs?
4/7/2019CS151SP19
Deconstructing the problem?
• Classes are useful but as all tools are not the holy grail of
programming.
• As in our example all dogs have more in common then differences.
• What if we had Cats in our data not just dogs?
• We could create a Cats class.
4/7/2019CS151SP19
Deconstructing the problem?
• Classes are useful but as all tools are not the holy grail of
programming.
• As in our example all dogs have more in common then differences.
• What if we had Cats in our data not just dogs?
• We could create a Cats class.
• Now we have to write two classes and these classes have similarities.
• Breed
4/7/2019CS151SP19
Deconstructing the problem?
• Classes are useful but as all tools are not the holy grail of
programming.
• As in our example all dogs have more in common then differences.
• What if we had Cats in our data not just dogs?
• We could create a Cats class.
• Now we have to write two classes and these classes have similarities.
• Breed
• Color
4/7/2019CS151SP19
Deconstructing the problem?
• Classes are useful but as all tools are not the holy grail of
programming.
• As in our example all dogs have more in common then differences.
• What if we had Cats in our data not just dogs?
• We could create a Cats class.
• Now we have to write two classes and these classes have similarities.
• Breed
• Color
• Name
4/7/2019CS151SP19
Deconstructing the problem?
• Classes are useful but as all tools are not the holy grail of
programming.
• As in our example all dogs have more in common then differences.
• What if we had Cats in our data not just dogs?
• We could create a Cats class.
• Now we have to write two classes and these classes have similarities.
• Breed
• Color
• Name
• We could reconfigure our class from Canine to Pet!
4/7/2019CS151SP19
Deconstructing the problem?
• Classes are useful but as all tools are not the holy grail of
programming.
• As in our example all dogs have more in common then differences.
• What if we had Cats in our data not just dogs?
• We could create a Cats class.
• Now we have to write two classes and these classes have similarities.
• Breed
• Color
• Name
• We could reconfigure our class from Canine to Pet
• Adding a private data type to hold, cat or dog
4/7/2019CS151SP19
Pet Class
class Pet:
name = ‘’
breed = []
animal=‘’
def __init__(self, name, animal):
self.name = name
self.breed = []
self.animal=‘’
def add_breed(self, breed):
self.breed.append(breed)
def get_breed(self):
print(self.breed)
def get_animal(self):
print(self.animal)
4/7/2019CS151SP19

More Related Content

Similar to CS 151 Classes lecture

Class 1 about the dataviz class
Class 1  about the dataviz classClass 1  about the dataviz class
Class 1 about the dataviz classJournovationSU
 
Me, my self and IPython
Me, my self and IPythonMe, my self and IPython
Me, my self and IPythonJoel Klinger
 
About the VR Storytelling Class
About the VR Storytelling ClassAbout the VR Storytelling Class
About the VR Storytelling ClassDan Pacheco
 
Virtual Reality Storytelling - Class 1
Virtual Reality Storytelling - Class 1Virtual Reality Storytelling - Class 1
Virtual Reality Storytelling - Class 1JournovationSU
 
Design It! Build It! Test It! Engineering Design in the Classroom
Design It! Build It! Test It! Engineering Design in the ClassroomDesign It! Build It! Test It! Engineering Design in the Classroom
Design It! Build It! Test It! Engineering Design in the Classroomkendricktm
 
2015 Lagniappe Tiered Assignments
2015 Lagniappe Tiered Assignments2015 Lagniappe Tiered Assignments
2015 Lagniappe Tiered AssignmentsLiz Fogarty
 
Interactive Data Visualization
Interactive Data VisualizationInteractive Data Visualization
Interactive Data VisualizationJournovationSU
 
T.E.A.C.H. Academy Course 11
T.E.A.C.H. Academy Course 11T.E.A.C.H. Academy Course 11
T.E.A.C.H. Academy Course 11Jimmy Keng
 
Predict oscars (4:17)
Predict oscars (4:17)Predict oscars (4:17)
Predict oscars (4:17)Thinkful
 
Teacher toolkit Pycon UK Sept 2018
Teacher toolkit Pycon UK Sept 2018Teacher toolkit Pycon UK Sept 2018
Teacher toolkit Pycon UK Sept 2018Sue Sentance
 
intership summary
intership summaryintership summary
intership summaryJunting Ma
 
Unit 1 Webinar Slides
Unit 1 Webinar Slides Unit 1 Webinar Slides
Unit 1 Webinar Slides jwalts
 
Creativity to Innovation
Creativity to Innovation Creativity to Innovation
Creativity to Innovation Mike Cardus
 

Similar to CS 151 Classes lecture (20)

Class 1 about the dataviz class
Class 1  about the dataviz classClass 1  about the dataviz class
Class 1 about the dataviz class
 
Revised vicencio differentiated instruction
Revised vicencio differentiated instructionRevised vicencio differentiated instruction
Revised vicencio differentiated instruction
 
Me, my self and IPython
Me, my self and IPythonMe, my self and IPython
Me, my self and IPython
 
C# by Zaheer Abbas Aghani
C# by Zaheer Abbas AghaniC# by Zaheer Abbas Aghani
C# by Zaheer Abbas Aghani
 
C# by Zaheer Abbas Aghani
C# by Zaheer Abbas AghaniC# by Zaheer Abbas Aghani
C# by Zaheer Abbas Aghani
 
Inclusive design (oe global action lab)
Inclusive design (oe global action lab)Inclusive design (oe global action lab)
Inclusive design (oe global action lab)
 
About the VR Storytelling Class
About the VR Storytelling ClassAbout the VR Storytelling Class
About the VR Storytelling Class
 
Artificial intelligence
Artificial intelligenceArtificial intelligence
Artificial intelligence
 
[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects
 
Virtual Reality Storytelling - Class 1
Virtual Reality Storytelling - Class 1Virtual Reality Storytelling - Class 1
Virtual Reality Storytelling - Class 1
 
Design It! Build It! Test It! Engineering Design in the Classroom
Design It! Build It! Test It! Engineering Design in the ClassroomDesign It! Build It! Test It! Engineering Design in the Classroom
Design It! Build It! Test It! Engineering Design in the Classroom
 
2015 Lagniappe Tiered Assignments
2015 Lagniappe Tiered Assignments2015 Lagniappe Tiered Assignments
2015 Lagniappe Tiered Assignments
 
Interactive Data Visualization
Interactive Data VisualizationInteractive Data Visualization
Interactive Data Visualization
 
T.E.A.C.H. Academy Course 11
T.E.A.C.H. Academy Course 11T.E.A.C.H. Academy Course 11
T.E.A.C.H. Academy Course 11
 
VIRTUAL DEMO.pptx
VIRTUAL DEMO.pptxVIRTUAL DEMO.pptx
VIRTUAL DEMO.pptx
 
Predict oscars (4:17)
Predict oscars (4:17)Predict oscars (4:17)
Predict oscars (4:17)
 
Teacher toolkit Pycon UK Sept 2018
Teacher toolkit Pycon UK Sept 2018Teacher toolkit Pycon UK Sept 2018
Teacher toolkit Pycon UK Sept 2018
 
intership summary
intership summaryintership summary
intership summary
 
Unit 1 Webinar Slides
Unit 1 Webinar Slides Unit 1 Webinar Slides
Unit 1 Webinar Slides
 
Creativity to Innovation
Creativity to Innovation Creativity to Innovation
Creativity to Innovation
 

More from Rudy Martinez

More from Rudy Martinez (18)

CS 151Exploration of python
CS 151Exploration of pythonCS 151Exploration of python
CS 151Exploration of python
 
CS 151 Graphing lecture
CS 151 Graphing lectureCS 151 Graphing lecture
CS 151 Graphing lecture
 
CS 151 Classes lecture 2
CS 151 Classes lecture 2CS 151 Classes lecture 2
CS 151 Classes lecture 2
 
CS151 Deep copy
CS151 Deep copyCS151 Deep copy
CS151 Deep copy
 
CS 151 Standard deviation lecture
CS 151 Standard deviation lectureCS 151 Standard deviation lecture
CS 151 Standard deviation lecture
 
CS 151 Midterm review
CS 151 Midterm reviewCS 151 Midterm review
CS 151 Midterm review
 
Cs 151 dictionary writer
Cs 151 dictionary writerCs 151 dictionary writer
Cs 151 dictionary writer
 
CS 151 homework2a
CS 151 homework2aCS 151 homework2a
CS 151 homework2a
 
CS 151 dictionary objects
CS 151 dictionary objectsCS 151 dictionary objects
CS 151 dictionary objects
 
CS 151 CSV output
CS 151 CSV outputCS 151 CSV output
CS 151 CSV output
 
CS 151 Date time lecture
CS 151 Date time lectureCS 151 Date time lecture
CS 151 Date time lecture
 
CS151 FIle Input and Output
CS151 FIle Input and OutputCS151 FIle Input and Output
CS151 FIle Input and Output
 
CS151 Functions lecture
CS151 Functions lectureCS151 Functions lecture
CS151 Functions lecture
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture01
Lecture01Lecture01
Lecture01
 
Lecture02
Lecture02Lecture02
Lecture02
 
Lecture03
Lecture03Lecture03
Lecture03
 
Lecture01
Lecture01Lecture01
Lecture01
 

Recently uploaded

KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 

Recently uploaded (20)

KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 

CS 151 Classes lecture

  • 1. Computer Science 151 An introduction to the art of computing Classes Rudy Martinez
  • 2. Classes • A class describes a set of objects with the same behavior. • String class • Constants: • Ascii_letters – all ascii_lowercase and ascii_uppercase letters • Ascii_lowercase - 'abcdefghijklmnopqrstuvwxyz’ • Ascii_uppercase - 'ABCDEFGHIJKLMNOPQRSTUVWXYZ’ • Digits - '0123456789’ myString = 'Hello Cruel_World!' for i in myString: #myString[i] = random.choice(string.ascii_letters) print(random.choice(string.ascii_letters)) 4/7/2019CS151SP19
  • 3. Class Methods • The set of all methods provided by a class, together with a description of their behavior, is called the public interface of the class. • You can drive a car by operating the steering wheel and pedals, without knowing how the engine works. Similarly, you use an object through its methods. The implementation is hidden. • String methods • isapha() – returns bool if all characters are letters • isdigit() – returns bool if characters are digits (numbers) • islower() – returns bool if all characters are lowercase import string myString = 'Hello Cruel_World!' for i in myString: if i.isalpha: print( i, ' is a Letter!') else: print(i, ' is not a letter!') 4/7/2019CS151SP19
  • 4. Class Constructors • A constructor defines and initializes the instance variables of an object. The constructor is automatically called whenever an object is created. • The constructor is responsible for defining and initializing all of the instance variables that are to be contained in the object. After the constructor completes its work, a reference to the newly created and initialized object is returned. The reference is saved in a variable so we can later call methods on the object. 4/7/2019CS151SP19
  • 5. Constructor Example Class Definition class Canine: ''' A simple dog class implementation ''' name = '' breed = [] def __init__(self, name): self.name = name self.breed = [] Example Code mydog = Canine('fido’) • This creates an object of the class Canine and specifies the name fido • Does it specify the breed? • Do we need to? 4/7/2019CS151SP19
  • 6. Methods Class Definition • Need method to get name and breed! def get_name(self): print(self.name) def get_breed(self): print(self.breed) Example pet = myDog.get_name() • Hey you didn’t set the breed anywhere? 4/7/2019CS151SP19
  • 7. Set Method Class Definition • Lets define set_breed() def add_breed(self, breed): self.breed.append(breed) Code myDog.add_breed(‘Poodle’) myDog.add_breed(‘Labrador’) So what does our class look like now? 4/7/2019CS151SP19
  • 8. Class Example class Canine: ''' A simple dog class implementation ''' name = '' breed = [] def __init__(self, name): self.name = name self.breed = [] def add_breed(self, breed): self.breed.append(breed) def get_breed(self): print(self.breed) def get_name(self): print(self.name) • Name = ‘fido’ • Breed = [‘Poodle’, ‘Labrador’] • The class is complete but it does very little. • Can we add dog age? • Where would we put it? • What accessor methods do we need? 4/7/2019CS151SP19
  • 9. Class Use • You know what a class is. 4/7/2019CS151SP19
  • 10. Class Use • You know what a class is. • A class describes a set of objects with the same behavior. 4/7/2019CS151SP19
  • 11. Class Use • You know what a class is. • A class describes a set of objects with the same behavior. • You know how to create a simple class. 4/7/2019CS151SP19
  • 12. Class Use • You know what a class is. • A class describes a set of objects with the same behavior. • You know how to create a simple class. • Use keyword class followed by the class name. 4/7/2019CS151SP19
  • 13. Class Use • You know what a class is. • A class describes a set of objects with the same behavior. • You know how to create a simple class. • Use keyword class followed by the class name. • Create private data objects 4/7/2019CS151SP19
  • 14. Class Use • You know what a class is. • A class describes a set of objects with the same behavior. • You know how to create a simple class. • Use keyword class followed by the class name. • Create private data objects • Create accessor methods to get and set private data objects 4/7/2019CS151SP19
  • 15. Class Use • You know what a class is. • A class describes a set of objects with the same behavior. • You know how to create a simple class. • Use keyword class followed by the class name. • Create private data objects • Create accessor methods to get and set private data objects • When would you use a class? 4/7/2019CS151SP19
  • 16. Class Use • You know what a class is. • A class describes a set of objects with the same behavior. • You know how to create a simple class. • Use keyword class followed by the class name. • Create private data objects • Create accessor methods to get and set private data objects • When would you use a class? • Can you think of any reasons? 4/7/2019CS151SP19
  • 17. Class Use • You know what a class is. • A class describes a set of objects with the same behavior. • You know how to create a simple class. • Use keyword class followed by the class name. • Create private data objects • Create accessor methods to get and set private data objects • When would you use a class? • Can you think of any reasons? • How about you have to write a program to control inventory in a factory? 4/7/2019CS151SP19
  • 18. Class Use • You know what a class is. • A class describes a set of objects with the same behavior. • You know how to create a simple class. • Use keyword class followed by the class name. • Create private data objects • Create accessor methods to get and set private data objects • When would you use a class? • Can you think of any reasons? • How about you have to write a program to control inventory in a factory? • Why would you use a class? • Can’t all this be programmed using what we already know? 4/7/2019CS151SP19
  • 19. Deconstructing the problem? • Writing good programs requires you to analyze the problem. • What is the problem? • Can the problem be solved incrementally? • Is it even solvable, if not can you estimate an answer with the data on hand? • What is the data? • Is it being collected or given to you? • What is common about the data? 4/7/2019CS151SP19
  • 20. Deconstructing the problem? • Writing good programs requires you to analyze the problem. • What is the problem? • Can the problem be solved incrementally? • Is it even solvable, if not can you estimate an answer with the data on hand? • What is the data? • Is it being collected or given to you? • What is common about the data? 4/7/2019CS151SP19
  • 21. Deconstructing the problem? • Classes are useful but as all tools are not the holy grail of programming. • As in our example all dogs have more in common then differences. • What if we had Cats in our data not just dogs? 4/7/2019CS151SP19
  • 22. Deconstructing the problem? • Classes are useful but as all tools are not the holy grail of programming. • As in our example all dogs have more in common then differences. • What if we had Cats in our data not just dogs? • We could create a Cats class. 4/7/2019CS151SP19
  • 23. Deconstructing the problem? • Classes are useful but as all tools are not the holy grail of programming. • As in our example all dogs have more in common then differences. • What if we had Cats in our data not just dogs? • We could create a Cats class. • Now we have to write two classes and these classes have similarities. • Breed 4/7/2019CS151SP19
  • 24. Deconstructing the problem? • Classes are useful but as all tools are not the holy grail of programming. • As in our example all dogs have more in common then differences. • What if we had Cats in our data not just dogs? • We could create a Cats class. • Now we have to write two classes and these classes have similarities. • Breed • Color 4/7/2019CS151SP19
  • 25. Deconstructing the problem? • Classes are useful but as all tools are not the holy grail of programming. • As in our example all dogs have more in common then differences. • What if we had Cats in our data not just dogs? • We could create a Cats class. • Now we have to write two classes and these classes have similarities. • Breed • Color • Name 4/7/2019CS151SP19
  • 26. Deconstructing the problem? • Classes are useful but as all tools are not the holy grail of programming. • As in our example all dogs have more in common then differences. • What if we had Cats in our data not just dogs? • We could create a Cats class. • Now we have to write two classes and these classes have similarities. • Breed • Color • Name • We could reconfigure our class from Canine to Pet! 4/7/2019CS151SP19
  • 27. Deconstructing the problem? • Classes are useful but as all tools are not the holy grail of programming. • As in our example all dogs have more in common then differences. • What if we had Cats in our data not just dogs? • We could create a Cats class. • Now we have to write two classes and these classes have similarities. • Breed • Color • Name • We could reconfigure our class from Canine to Pet • Adding a private data type to hold, cat or dog 4/7/2019CS151SP19
  • 28. Pet Class class Pet: name = ‘’ breed = [] animal=‘’ def __init__(self, name, animal): self.name = name self.breed = [] self.animal=‘’ def add_breed(self, breed): self.breed.append(breed) def get_breed(self): print(self.breed) def get_animal(self): print(self.animal) 4/7/2019CS151SP19