SlideShare une entreprise Scribd logo
1  sur  21
Computer Science 151
An introduction to the art of
computing
Classes Lecture 2
Rudy Martinez
Class Example
from random import randint
import statistics
'''A simulated 6-sided die that can be rolled and drawn on a canvas.'''
class Die :
4/7/2019CS151SP19
Class Example
from random import randint
import statistics
'''A simulated 6-sided die that can be rolled and drawn on a canvas.'''
class Die :
# Die variable
_value = randint(1, 6)
4/7/2019CS151SP19
Class Example
from random import randint
import statistics
'''A simulated 6-sided die that can be rolled and drawn on a canvas.'''
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
4/7/2019CS151SP19
Class Example
from random import randint
import statistics
'''A simulated 6-sided die that can be rolled and drawn on a canvas.'''
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
4/7/2019CS151SP19
Class Example
from random import randint
import statistics
'''A simulated 6-sided die that can be rolled and drawn on a canvas.'''
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
4/7/2019CS151SP19
Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
Instantiate a 'Die' with the Constructor
Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
Instantiate a 'Die'
myDie = Die()
Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
#Trusty For Loop
for I in range(0 to 10000):
Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
#Trusty For Loop
for I in range(0 to 10000):
mydie.roll()
Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
#Trusty For Loop
for I in range(0 to 10000):
mydie.roll()
val = myDie.faceValue()
Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
#Trusty For Loop
for I in range(0 to 10000):
mydie.roll()
val = myDie.faceValue()
myRolls.append(val)
Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
#Trusty For Loop
for I in range(0 to 10000):
mydie.roll()
val = myDie.faceValue()
myRolls.append(val)
print('Roll number', I, ' value', val)
Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
#Trusty For Loop
for I in range(0 to 10000):
mydie.roll()
val = myDie.faceValue()
myRolls.append(val)
print('Roll number', I, ' value', val)
#Statistics
print('Length of List:', len(myRolls))
Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
#Trusty For Loop
for I in range(0 to 10000):
mydie.roll()
val = myDie.faceValue()
myRolls.append(val)
print('Roll number', I, ' value', val)
#Statistics
print('Length of List:', len(myRolls))
print('Median value:', statistics.median(myRolls))
Class Example
Die Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the random number generator.
def roll(self) :
self._value = randint(1, 6)
Code
4/7/2019CS151SP19
#Instantiate a 'Die' with the Construcutor
myDie = Die()
#Create a list to hold die values from a roll
myRolls = []
#Trusty For Loop
for I in range(0 to 10000):
mydie.roll()
val = myDie.faceValue()
myRolls.append(val)
print('Roll number', I, ' value', val)
#Statistics
print('Length of List:', len(myRolls))
print('Median value:', statistics.median(myRolls))
print('Sigma value:', statistics.stdev(myRolls))
Code
<---snip --- >
print('Length of List:', len(myRolls))
print('Median value:',
statistics.median(myRolls))
print('Sigma value:', statistics.stdev(myRolls))
4/7/2019CS151SP19
Rolling die: 1 5
Rolling die: 2 1
Rolling die: 3 7
.
.
.
Rolling die: 9999 5
Length of List: 10000
Median value: 4.0
Sigma value: 1.704
Dice Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the
random number generator.
def roll(self) :
self._value = randint(1, 6)
4/7/2019CS151SP19
What if we were giant nerds and needed a 10 sided
die?
Dice Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the
random number generator.
def roll(self) :
self._value = randint(1, 6)
4/7/2019CS151SP19
What if we were giant nerds and needed a 10 sided
die?
What changes to the code need to be made?
Dice Class
class Die :
# Die variable
_value = randint(1, 6)
## Constructs the die.
def __init__(self) :
self._value = 1
## Get the face value of the die.
# @return the face value
def faceValue(self) :
return self._value
## Simulates the rolling of the die using the
random number generator.
def roll(self) :
self._value = randint(1, 6)
4/7/2019CS151SP19
• What if we were giant nerds and needed a 10 sided die?
• What changes to the code need to be made?
• We change the 6 to a 10!

Contenu connexe

Similaire à CS 151 Classes lecture 2

python_lab_manual_final (1).pdf
python_lab_manual_final (1).pdfpython_lab_manual_final (1).pdf
python_lab_manual_final (1).pdfkeerthu0442
 
python practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdfpython practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdfrajatxyz
 
Idioms in swift 2016 05c
Idioms in swift 2016 05cIdioms in swift 2016 05c
Idioms in swift 2016 05cKaz Yoshikawa
 
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and JasmineRails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and JasmineRaimonds Simanovskis
 
Can someone please explain what the code below is doing and comment on.pdf
Can someone please explain what the code below is doing and comment on.pdfCan someone please explain what the code below is doing and comment on.pdf
Can someone please explain what the code below is doing and comment on.pdfkuldeepkumarapgsi
 
Need an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdfNeed an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdfactexerode
 
Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Andreas Dewes
 
import os import matplotlib-pyplot as plt import pandas as pd import r.docx
import os import matplotlib-pyplot as plt import pandas as pd import r.docximport os import matplotlib-pyplot as plt import pandas as pd import r.docx
import os import matplotlib-pyplot as plt import pandas as pd import r.docxBlake0FxCampbelld
 
第二讲 Python基礎
第二讲 Python基礎第二讲 Python基礎
第二讲 Python基礎juzihua1102
 
第二讲 预备-Python基礎
第二讲 预备-Python基礎第二讲 预备-Python基礎
第二讲 预备-Python基礎anzhong70
 
project 6cards.pyimport randomclass Card( object ).docx
project 6cards.pyimport randomclass Card( object ).docxproject 6cards.pyimport randomclass Card( object ).docx
project 6cards.pyimport randomclass Card( object ).docxbriancrawford30935
 
Elixir and Dialyzer, Types and Typespecs, using and understanding them
Elixir and Dialyzer, Types and Typespecs, using and understanding themElixir and Dialyzer, Types and Typespecs, using and understanding them
Elixir and Dialyzer, Types and Typespecs, using and understanding themDan Janowski
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdfsrxerox
 
Stupid Awesome Python Tricks
Stupid Awesome Python TricksStupid Awesome Python Tricks
Stupid Awesome Python TricksBryan Helmig
 

Similaire à CS 151 Classes lecture 2 (20)

python_lab_manual_final (1).pdf
python_lab_manual_final (1).pdfpython_lab_manual_final (1).pdf
python_lab_manual_final (1).pdf
 
python practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdfpython practicals-solution-2019-20-class-xii.pdf
python practicals-solution-2019-20-class-xii.pdf
 
Idioms in swift 2016 05c
Idioms in swift 2016 05cIdioms in swift 2016 05c
Idioms in swift 2016 05c
 
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and JasmineRails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
 
Can someone please explain what the code below is doing and comment on.pdf
Can someone please explain what the code below is doing and comment on.pdfCan someone please explain what the code below is doing and comment on.pdf
Can someone please explain what the code below is doing and comment on.pdf
 
C++ L11-Polymorphism
C++ L11-PolymorphismC++ L11-Polymorphism
C++ L11-Polymorphism
 
Need an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdfNeed an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdf
 
Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...
 
Python : Dictionaries
Python : DictionariesPython : Dictionaries
Python : Dictionaries
 
import os import matplotlib-pyplot as plt import pandas as pd import r.docx
import os import matplotlib-pyplot as plt import pandas as pd import r.docximport os import matplotlib-pyplot as plt import pandas as pd import r.docx
import os import matplotlib-pyplot as plt import pandas as pd import r.docx
 
第二讲 Python基礎
第二讲 Python基礎第二讲 Python基礎
第二讲 Python基礎
 
第二讲 预备-Python基礎
第二讲 预备-Python基礎第二讲 预备-Python基礎
第二讲 预备-Python基礎
 
project 6cards.pyimport randomclass Card( object ).docx
project 6cards.pyimport randomclass Card( object ).docxproject 6cards.pyimport randomclass Card( object ).docx
project 6cards.pyimport randomclass Card( object ).docx
 
Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to Groovy
 
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
 
Python-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdfPython-Cheat-Sheet.pdf
Python-Cheat-Sheet.pdf
 
Elixir and Dialyzer, Types and Typespecs, using and understanding them
Elixir and Dialyzer, Types and Typespecs, using and understanding themElixir and Dialyzer, Types and Typespecs, using and understanding them
Elixir and Dialyzer, Types and Typespecs, using and understanding them
 
C# Today and Tomorrow
C# Today and TomorrowC# Today and Tomorrow
C# Today and Tomorrow
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdf
 
Stupid Awesome Python Tricks
Stupid Awesome Python TricksStupid Awesome Python Tricks
Stupid Awesome Python Tricks
 

Plus de Rudy Martinez

Plus de 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
CS 151 Classes lectureCS 151 Classes lecture
CS 151 Classes lecture
 
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
 

Dernier

Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 

Dernier (20)

Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 

CS 151 Classes lecture 2

  • 1. Computer Science 151 An introduction to the art of computing Classes Lecture 2 Rudy Martinez
  • 2. Class Example from random import randint import statistics '''A simulated 6-sided die that can be rolled and drawn on a canvas.''' class Die : 4/7/2019CS151SP19
  • 3. Class Example from random import randint import statistics '''A simulated 6-sided die that can be rolled and drawn on a canvas.''' class Die : # Die variable _value = randint(1, 6) 4/7/2019CS151SP19
  • 4. Class Example from random import randint import statistics '''A simulated 6-sided die that can be rolled and drawn on a canvas.''' class Die : # Die variable _value = randint(1, 6) ## Constructs the die. def __init__(self) : self._value = 1 4/7/2019CS151SP19
  • 5. Class Example from random import randint import statistics '''A simulated 6-sided die that can be rolled and drawn on a canvas.''' class Die : # Die variable _value = randint(1, 6) ## Constructs the die. def __init__(self) : self._value = 1 ## Get the face value of the die. # @return the face value def faceValue(self) : return self._value 4/7/2019CS151SP19
  • 6. Class Example from random import randint import statistics '''A simulated 6-sided die that can be rolled and drawn on a canvas.''' class Die : # Die variable _value = randint(1, 6) ## Constructs the die. def __init__(self) : self._value = 1 ## Get the face value of the die. # @return the face value def faceValue(self) : return self._value ## Simulates the rolling of the die using the random number generator. def roll(self) : self._value = randint(1, 6) 4/7/2019CS151SP19
  • 7. Class Example Die Class class Die : # Die variable _value = randint(1, 6) ## Constructs the die. def __init__(self) : self._value = 1 ## Get the face value of the die. # @return the face value def faceValue(self) : return self._value ## Simulates the rolling of the die using the random number generator. def roll(self) : self._value = randint(1, 6) Code 4/7/2019CS151SP19 Instantiate a 'Die' with the Constructor
  • 8. Class Example Die Class class Die : # Die variable _value = randint(1, 6) ## Constructs the die. def __init__(self) : self._value = 1 ## Get the face value of the die. # @return the face value def faceValue(self) : return self._value ## Simulates the rolling of the die using the random number generator. def roll(self) : self._value = randint(1, 6) Code 4/7/2019CS151SP19 Instantiate a 'Die' myDie = Die()
  • 9. Class Example Die Class class Die : # Die variable _value = randint(1, 6) ## Constructs the die. def __init__(self) : self._value = 1 ## Get the face value of the die. # @return the face value def faceValue(self) : return self._value ## Simulates the rolling of the die using the random number generator. def roll(self) : self._value = randint(1, 6) Code 4/7/2019CS151SP19 #Instantiate a 'Die' with the Construcutor myDie = Die() #Create a list to hold die values from a roll myRolls = []
  • 10. Class Example Die Class class Die : # Die variable _value = randint(1, 6) ## Constructs the die. def __init__(self) : self._value = 1 ## Get the face value of the die. # @return the face value def faceValue(self) : return self._value ## Simulates the rolling of the die using the random number generator. def roll(self) : self._value = randint(1, 6) Code 4/7/2019CS151SP19 #Instantiate a 'Die' with the Construcutor myDie = Die() #Create a list to hold die values from a roll myRolls = [] #Trusty For Loop for I in range(0 to 10000):
  • 11. Class Example Die Class class Die : # Die variable _value = randint(1, 6) ## Constructs the die. def __init__(self) : self._value = 1 ## Get the face value of the die. # @return the face value def faceValue(self) : return self._value ## Simulates the rolling of the die using the random number generator. def roll(self) : self._value = randint(1, 6) Code 4/7/2019CS151SP19 #Instantiate a 'Die' with the Construcutor myDie = Die() #Create a list to hold die values from a roll myRolls = [] #Trusty For Loop for I in range(0 to 10000): mydie.roll()
  • 12. Class Example Die Class class Die : # Die variable _value = randint(1, 6) ## Constructs the die. def __init__(self) : self._value = 1 ## Get the face value of the die. # @return the face value def faceValue(self) : return self._value ## Simulates the rolling of the die using the random number generator. def roll(self) : self._value = randint(1, 6) Code 4/7/2019CS151SP19 #Instantiate a 'Die' with the Construcutor myDie = Die() #Create a list to hold die values from a roll myRolls = [] #Trusty For Loop for I in range(0 to 10000): mydie.roll() val = myDie.faceValue()
  • 13. Class Example Die Class class Die : # Die variable _value = randint(1, 6) ## Constructs the die. def __init__(self) : self._value = 1 ## Get the face value of the die. # @return the face value def faceValue(self) : return self._value ## Simulates the rolling of the die using the random number generator. def roll(self) : self._value = randint(1, 6) Code 4/7/2019CS151SP19 #Instantiate a 'Die' with the Construcutor myDie = Die() #Create a list to hold die values from a roll myRolls = [] #Trusty For Loop for I in range(0 to 10000): mydie.roll() val = myDie.faceValue() myRolls.append(val)
  • 14. Class Example Die Class class Die : # Die variable _value = randint(1, 6) ## Constructs the die. def __init__(self) : self._value = 1 ## Get the face value of the die. # @return the face value def faceValue(self) : return self._value ## Simulates the rolling of the die using the random number generator. def roll(self) : self._value = randint(1, 6) Code 4/7/2019CS151SP19 #Instantiate a 'Die' with the Construcutor myDie = Die() #Create a list to hold die values from a roll myRolls = [] #Trusty For Loop for I in range(0 to 10000): mydie.roll() val = myDie.faceValue() myRolls.append(val) print('Roll number', I, ' value', val)
  • 15. Class Example Die Class class Die : # Die variable _value = randint(1, 6) ## Constructs the die. def __init__(self) : self._value = 1 ## Get the face value of the die. # @return the face value def faceValue(self) : return self._value ## Simulates the rolling of the die using the random number generator. def roll(self) : self._value = randint(1, 6) Code 4/7/2019CS151SP19 #Instantiate a 'Die' with the Construcutor myDie = Die() #Create a list to hold die values from a roll myRolls = [] #Trusty For Loop for I in range(0 to 10000): mydie.roll() val = myDie.faceValue() myRolls.append(val) print('Roll number', I, ' value', val) #Statistics print('Length of List:', len(myRolls))
  • 16. Class Example Die Class class Die : # Die variable _value = randint(1, 6) ## Constructs the die. def __init__(self) : self._value = 1 ## Get the face value of the die. # @return the face value def faceValue(self) : return self._value ## Simulates the rolling of the die using the random number generator. def roll(self) : self._value = randint(1, 6) Code 4/7/2019CS151SP19 #Instantiate a 'Die' with the Construcutor myDie = Die() #Create a list to hold die values from a roll myRolls = [] #Trusty For Loop for I in range(0 to 10000): mydie.roll() val = myDie.faceValue() myRolls.append(val) print('Roll number', I, ' value', val) #Statistics print('Length of List:', len(myRolls)) print('Median value:', statistics.median(myRolls))
  • 17. Class Example Die Class class Die : # Die variable _value = randint(1, 6) ## Constructs the die. def __init__(self) : self._value = 1 ## Get the face value of the die. # @return the face value def faceValue(self) : return self._value ## Simulates the rolling of the die using the random number generator. def roll(self) : self._value = randint(1, 6) Code 4/7/2019CS151SP19 #Instantiate a 'Die' with the Construcutor myDie = Die() #Create a list to hold die values from a roll myRolls = [] #Trusty For Loop for I in range(0 to 10000): mydie.roll() val = myDie.faceValue() myRolls.append(val) print('Roll number', I, ' value', val) #Statistics print('Length of List:', len(myRolls)) print('Median value:', statistics.median(myRolls)) print('Sigma value:', statistics.stdev(myRolls))
  • 18. Code <---snip --- > print('Length of List:', len(myRolls)) print('Median value:', statistics.median(myRolls)) print('Sigma value:', statistics.stdev(myRolls)) 4/7/2019CS151SP19 Rolling die: 1 5 Rolling die: 2 1 Rolling die: 3 7 . . . Rolling die: 9999 5 Length of List: 10000 Median value: 4.0 Sigma value: 1.704
  • 19. Dice Class class Die : # Die variable _value = randint(1, 6) ## Constructs the die. def __init__(self) : self._value = 1 ## Get the face value of the die. # @return the face value def faceValue(self) : return self._value ## Simulates the rolling of the die using the random number generator. def roll(self) : self._value = randint(1, 6) 4/7/2019CS151SP19 What if we were giant nerds and needed a 10 sided die?
  • 20. Dice Class class Die : # Die variable _value = randint(1, 6) ## Constructs the die. def __init__(self) : self._value = 1 ## Get the face value of the die. # @return the face value def faceValue(self) : return self._value ## Simulates the rolling of the die using the random number generator. def roll(self) : self._value = randint(1, 6) 4/7/2019CS151SP19 What if we were giant nerds and needed a 10 sided die? What changes to the code need to be made?
  • 21. Dice Class class Die : # Die variable _value = randint(1, 6) ## Constructs the die. def __init__(self) : self._value = 1 ## Get the face value of the die. # @return the face value def faceValue(self) : return self._value ## Simulates the rolling of the die using the random number generator. def roll(self) : self._value = randint(1, 6) 4/7/2019CS151SP19 • What if we were giant nerds and needed a 10 sided die? • What changes to the code need to be made? • We change the 6 to a 10!