Zhiguang Huo (Caleb)
Tuesday Oct 24th, 2023
class Person:
    def setName(self, aname):
        self.name = aname
    
    def greet(self):
        print("Hi there, my name is " + self.name)
a = Person()
a.setName("Lucas")
a.greet()## Hi there, my name is Lucas
## Hi there, my name is Lucas
## Hi there, my name is Amy
## Hi there, my name is Beth
Create a new participant Amy, who is female and 49 years old.
When we use the display_info(), we expect to see
"Participant Amy, 49 yrs, female".
    
class Participant:
    
    def setName(self,name):
        self.name = name
      
    def setAge(self,age):
        self.age = age
    def setGender(self,sex):
        self.sex = sex
    def display_info(self):
        print(f"Participant {self.name}, {self.age} yrs, {self.sex}")
aParticipant = Participant()
aParticipant.setName("Amy")
aParticipant.setAge(49)
aParticipant.setGender("Sex")
aParticipant.display_info()## Participant Amy, 49 yrs, Sex
class Person:
    
    def __init__(self):
        self.name = "John"
    
    def setName(self, aname):
        self.name = aname
    
    def greet(self):
        print("Hi there, my name is " + self.name)
a = Person()
a.setName("Lucas")
a.greet()## Hi there, my name is Lucas
## Hi there, my name is John
class Person:
    
    def __init__(self, aname):
        self.name = aname
    
    def setName(self, aname):
        self.name = aname
    
    def greet(self):
        print("Hi there, my name is " + self.name)
a = Person("Lucas")
b = Person("Amy")
a.greet()## Hi there, my name is Lucas
## Hi there, my name is Amy
class Person:
    
    def __init__(self, aname="John"):
        self.name = aname
    
    def setName(self, aname):
        self.name = aname
    
    def greet(self):
        print("Hi there, my name is " + self.name)
    
a = Person("Lucas")
a.greet()## Hi there, my name is Lucas
## Hi there, my name is John
Create a new participant Amy, who is female and 49 years old.
When we use the display_info(), we expect to see
"Participant Amy, 49 yrs, female".
    
class Participant:
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex
    
    def setName(self,name):
        self.name = name
      
    def setAge(self,age):
        self.age = age
    def setGender(self,sex):
        self.sex = sex
        
    def display_info(self):
        print(f"Participant {self.name}, {self.age} yrs, {self.sex}")
aParticipant = Participant(name="Amy", age=49, sex="female")
aParticipant.display_info()## Participant Amy, 49 yrs, female
class Secretive:
    def __inaccessible(self):
        print("Bet you can't see me...")
    def accessible(self):
        print("The secret message is:")
        self.__inaccessible()
s = Secretive()
#s.__inaccessible() ## does not work
s.accessible()## The secret message is:
## Bet you can't see me...
class Person:
    
    def __init__(self, aname):
        self.name = aname
    
    def getName(self):
        return(self.name)
    
a = Person("Lucas")## 'Lucas'
## 'Lucas'
class Person:
    
    def __init__(self, aname):
        self.__name = aname
    
    def getName(self):
        return(self.__name)
    
a = Person("Lucas")
a.getName()
#a.__name ## does not work## 'Lucas'
Create a new participant Amy, who is female and 49 years old.
Also create her phone_number (123-456-7890) by using setPhoneNumber() method.
The phone_number is not directly accessible via class.phone_number.
It is accessible via class.show_contact_number()
class Participant:
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex
    
    def setPhoneNumber(self,anumber):
        self.__phone_number = anumber
    def display_info(self):
        print(f"Participant {self.name}, {self.age} yrs, {self.sex}")
    def show_contact_number(self):
        print(self.__phone_number)
      
aParticipant = Participant(name="Amy", age=49, sex="female")
aParticipant.setPhoneNumber("123-456-7890")
aParticipant.show_contact_number()## 123-456-7890
class Car:
    def __init__(self, **kw):
        self.make = kw["make"]
        # self.model = kw["model"]
        self.model = kw.get("model")
my_car = Car(make = "Chevo", model = "Malibu")
print(my_car.model)## Malibu
## None
class Person:
    
    def __init__(self, aname="John"):
        self.name = aname
    
    def setName(self, aname):
        self.name = aname
    
    def getName(self):
        return(self.name)
    
    def greet(self):
        print("Hi there, my name is " + self.name)class Group:
    
    def __init__(self):
        self.persons = []
    
    def add(self, aperson):
        self.persons.append(aperson)
    
    def getNameAll(self):
        res = [aperson.getName() for aperson in self.persons]
        return(res)
        
agroup = Group()
agroup.add(Person("Amy"))
agroup.add(Person("Beth"))
agroup.add(Person("Carl"))
agroup.getNameAll()## ['Amy', 'Beth', 'Carl']
Create a study with 5 participants, Amy, Beth, Carl, Dan, Emily.
Sex are randomly choose from male and female.
Age are randomly selected from 20-60.
And then show the mean age and percentage of females.
Also show the info for all participants
astudy = Study()
names = ["Amy", "Beth", "Carl", "Dan", "Emily"]
genders = ["male", "female"]
import random
for aname in names:
  aage = random.randint(20,80)
  asex = random.choice(genders)
  aparticipant = Participant(aname, aage, asex)
  astudy.enroll(aparticipant)
astudy.get_mean_age()
astudy.get_percent_female()
astudy.display_all()## Participant Amy, 55 yrs, female
## Participant Beth, 56 yrs, male
## Participant Carl, 56 yrs, female
## Participant Dan, 65 yrs, male
## Participant Emily, 74 yrs, male
class Child(Parent):
class Person:
    
    def __init__(self):
        self.name = "John"
    
    def setName(self, aname):
        self.name = aname
    
    def greet(self):
        print("Hi there, my name is " + self.name)## Hi there, my name is John
The child class can have additional attributes and methods compared to the parent class
## Hi there, my name is Levi
astudent.add_course("math", 4.0)
astudent.add_course("physics", 3.8)
astudent.add_course("history", 3.7)
astudent.get_ave_grade()## 3.8333333333333335
## False
## True
class Animal():
  def __init__(self, animal_type):
    print('Animal Type:', animal_type)
    
class Dog(Animal):
  def __init__(self):
    # call superclass
    super().__init__('dog')
    ## super(Dog, self).__init__('dog') ## alternatively
    print('Wolffff')
    
adog = Dog()## Animal Type: dog
## Wolffff
class Participant:
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex
    
    def display_info(self):
        print(f"Participant {self.name}, {self.age} yrs, {self.sex}")
aParticipant = Participant(name="Amy", age=49, sex="female")
aParticipant.display_info()## Participant Amy, 49 yrs, female
def train(self):
    strength += 1
    print(my strength is {strength})
class Athlete(Participant):
    
    def __init__(self, name, age, sex):
        super(Athlete, self).__init__(name, age, sex) ## need this to initialize the parental class
        ## alternatively, super().__init__(name, age, sex)
        self.strength = 100
    
    def train(self):
        self.strength += 1
        print(f"my strength is {self.strength}")## Participant Amy, 49 yrs, female
## 100
## my strength is 101
## 101
class Calculator:
    def calculate(self, expression):
        self.value = eval(expression) ## eval is to evaluate some expression in python
class Talker:
    def talk(self):
        print("Hi, my value is", self.value)
class TalkingCalculator(Calculator, Talker):
    pass## Hi, my value is 2