Skip to content

Commit 99c4e53

Browse files
committed
Pets, Part A through D
1 parent 6c460db commit 99c4e53

4 files changed

Lines changed: 760 additions & 0 deletions

File tree

classes/Pets-Part-A.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
##
2+
# Pets, Part A Lecture
3+
##
4+
5+
# -*- coding: utf-8 -*-
6+
7+
# Define a class
8+
class Pet:
9+
10+
# Define a function which refers to the class in order to initiliaze the attributes of the class
11+
def __init__(self,n,a,h,p):
12+
# Define an attribute and assign the value of the n argument
13+
self.name = n
14+
# Define an attribute and assign the value of the a argument
15+
self.age = a
16+
# Define an attribute and assign the value of the h argument
17+
self.hunger = h
18+
# Define an attribute and assign the value of the p argument
19+
self.playful = p
20+
21+
# Define a class
22+
class Pet:
23+
24+
# Define a function which refers to the class in order to initiliaze the attributes of the class
25+
def __init__(self,name,a,h,p):
26+
# Define an attribute and assign the value of the name argument
27+
self.name = name
28+
# Define an attribute and assign the value of the a argument
29+
self.age = a
30+
# Define an attribute and assign the value of the h argument
31+
self.hunger = h
32+
# Define an attribute and assign the value of the p argument
33+
self.playful = p
34+
35+
# getters
36+
# Define a function to return an attribute of the class
37+
def getName(self):
38+
# The function will return the name attribute
39+
return self.name
40+
41+
# setters
42+
# Define a function which assigns a value to an attribute of the class
43+
def setName(self,name):
44+
self.name = name
45+
46+
# Define a class
47+
class Pet:
48+
49+
# Define a function which refers to the class in order to initiliaze the attributes of the class
50+
def __init__(self,name,a,h,p):
51+
# Define an attribute and assign the value of the name argument
52+
self.name = name
53+
# Define an attribute and assign the value of the a argument
54+
self.age = a
55+
# Define an attribute and assign the value of the h argument
56+
self.hunger = h
57+
# Define an attribute and assign the value of the p argument
58+
self.playful = p
59+
60+
# getters
61+
# Define a function to return an attribute of the class
62+
def getName(self):
63+
# The function will return the name attribute
64+
return self.name
65+
66+
# setters
67+
# Define a function which assigns a value to an attribute of the class
68+
def setName(self,x):
69+
self.name = x
70+
71+
72+
# Define a class
73+
class Pet:
74+
75+
# Define a function which refers to the class in order to initiliaze the attributes of the class
76+
def __init__(self,name,a,h,p):
77+
# Define an attribute and assign the value of the name argument
78+
self.name = name
79+
# Define an attribute and assign the value of the a argument
80+
self.age = a
81+
# Define an attribute and assign the value of the h argument
82+
self.hunger = h
83+
# Define an attribute and assign the value of the p argument
84+
self.playful = p
85+
86+
# getters
87+
# Define a function to return an attribute of the class
88+
def getName(self):
89+
# The function will return the name attribute
90+
return self.name
91+
92+
# Define a function to return an attribute of the class
93+
def getAge(self):
94+
# The function will return the age attribute
95+
return self.age
96+
97+
# Define a function to return an attribute of the class
98+
def getHunger(self):
99+
# The function will return the hunger attribute
100+
return self.hunger
101+
102+
# Define a function to return an attribute of the class
103+
def getPlayful(self):
104+
# The function will return the playful attribute
105+
return self.playful
106+
107+
# setters
108+
# Define a function which assigns a value to an attribute of the class
109+
def setName(self,xname):
110+
self.name = xname
111+
112+
# Define a function which assigns a value to an attribute of the class
113+
def setAge(self,Age):
114+
self.age = Age
115+
116+
# Define a function which assigns a value to an attribute of the class
117+
def setHunger(self,hunger):
118+
self.hunger = hunger
119+
120+
# Define a function which assigns a value to an attribute of the class
121+
def setPlayful(self,play):
122+
self.playful = play
123+
124+
# Create an instance of the Pet class and assign values to the attributes
125+
Pet1 = Pet("Jim",3,False,True)
126+
127+
# Print the value returned by the getName() function of the Pet1 instance
128+
# This will print "Jim"
129+
print(Pet1.getName())
130+
#Print the value returned by the getPlayful() function of the Pet1 instance
131+
# This will print True
132+
print(Pet1.getPlayful())
133+
134+
# Call the setName(xname) function of the Pet1 instance
135+
# This will assign a new value to the name attribute of the Pet1 instance
136+
Pet1.setName("Snowball")
137+
# Print the value returned by the getName() function of the Pet1 instance
138+
# This will print "Snowball"
139+
print(Pet1.getName())
140+
141+
# Access and print the name attribute of the Pet1 instance
142+
# This will print "Snowball"
143+
print(Pet1.name)
144+
145+
# Assign the value "Jim" to the name attribute of the Pet1 instance
146+
Pet1.name = "Jim"
147+
# Access and print the name attribute of the Pet1 instance
148+
# This will print "Jim"
149+
print(Pet1.name)

classes/Pets-Part-B.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
##
2+
# Pets, Part B Lecture
3+
##
4+
5+
# -*- coding: utf-8 -*-
6+
7+
8+
# Define a class
9+
class Pet:
10+
11+
# Define a function which refers to the class in order to initiliaze the attributes of the class
12+
def __init__(self,name,a,h,p):
13+
# Define an attribute and assign the value of the name argument
14+
self.name = name
15+
# Define an attribute and assign the value of the a argument
16+
self.age = a
17+
# Define an attribute and assign the value of the h argument
18+
self.hunger = h
19+
# Define an attribute and assign the value of the p argument
20+
self.playful = p
21+
22+
# getters
23+
# Define a function to return an attribute of the class
24+
def getName(self):
25+
# The function will return the name attribute
26+
return self.name
27+
28+
# Define a function to return an attribute of the class
29+
def getAge(self):
30+
# The function will return the age attribute
31+
return self.age
32+
33+
# Define a function to return an attribute of the class
34+
def getHunger(self):
35+
# The function will return the hunger attribute
36+
return self.hunger
37+
38+
# Define a function to return an attribute of the class
39+
def getPlayful(self):
40+
# The function will return the playful attribute
41+
return self.playful
42+
43+
# setters
44+
# Define a function which assigns a value to an attribute of the class
45+
def setName(self,xname):
46+
self.name = xname
47+
48+
# Define a function which assigns a value to an attribute of the class
49+
def setAge(self,Age):
50+
self.age = Age
51+
52+
# Define a function which assigns a value to an attribute of the class
53+
def setHunger(self,hunger):
54+
self.hunger = hunger
55+
56+
# Define a function which assigns a value to an attribute of the class
57+
def setPlayful(self,play):
58+
self.playful = play
59+
60+
61+
# The class is commented becuse two errors exist. One is in line 65 where the self argument is missing
62+
# and the second error is in line 81 where the code should be self.FavoriteToy
63+
# Define a class which inherits the Pet class
64+
#class Dog(Pet):
65+
#
66+
# # Define a function which refers to the class in order to initiliaze the attributes of the class
67+
# def __init__(self,name,age,hunger,playful,breed,FavoriteToy):
68+
# # Call the initializer of the parent class with the proper parameters
69+
# # Error - the self argument is missing
70+
# Pet.__init__(name,age,hunger,playful)
71+
#
72+
# # The following line will return an error if uncommented
73+
# #self.__init__(name,age,hunger,playful)
74+
#
75+
# # Define an attribute and assign the value "None"
76+
# self.breed = breed
77+
# self.FavoriteToy = FavoriteToy
78+
#
79+
# # Define unction which refers to the class
80+
# def wantsToPlay(self):
81+
#
82+
# # IF condition is True
83+
# if self.playful == True:
84+
# # Define the string which the function returns
85+
# # Error - It should be self.FavoriteToy
86+
# return("Dog wants to play with " + FavoriteToy)
87+
#
88+
# # ELSE condition
89+
# else:
90+
# # Define the string which the function returns
91+
# return("Dog doesn't want to play")
92+
#
93+
# Create an instance of the Dog class and assign values to the attributes
94+
#huskyDog = Dog("Snowball",5,False,True,"Husky","Stick")
95+
#
96+
# Assign to a variable the result returned by the wantsToPlay() function of the huskyDog instance
97+
#Play = huskyDog.wantsToPlay()
98+
#
99+
# Print the value of the Play variable
100+
# This will print "Dog wants to play with Stick"
101+
#print(Play)
102+
103+
104+
105+
# Define a class which inherits the Pet class
106+
class Dog(Pet):
107+
108+
# Define a function which refers to the class in order to initiliaze the attributes of the class
109+
def __init__(self,name,age,hunger,playful,breed,FavoriteToy):
110+
# Call the initializer of the parent class with the proper parameters
111+
Pet.__init__(self,name,age,hunger,playful)
112+
113+
# The following line will return an error if uncommented
114+
#self.__init__(name,age,hunger,playful)
115+
116+
# Define an attribute and assign the value "None"
117+
self.breed = breed
118+
self.FavoriteToy = FavoriteToy
119+
120+
# Define unction which refers to the class
121+
def wantsToPlay(self):
122+
123+
# IF condition is True
124+
if self.playful == True:
125+
# Define the string which the function returns
126+
return("Dog wants to play with " + self.FavoriteToy)
127+
128+
# ELSE condition
129+
else:
130+
# Define the string which the function returns
131+
return("Dog doesn't want to play")
132+
133+
# Create an instance of the Dog class and assign values to the attributes
134+
huskyDog = Dog("Snowball",5,False,True,"Husky","Stick")
135+
136+
# Assign to a variable the result returned by the wantsToPlay() function of the huskyDog instance
137+
Play = huskyDog.wantsToPlay()
138+
139+
# Print the value of the Play variable
140+
# This will print "Dog wants to play with Stick"
141+
print(Play)
142+
143+
# Assign the value False to the playful attribute of the huskyDog instance
144+
huskyDog.playful = False
145+
146+
# Assign to a variable the result returned by the wantsToPlay() function of the huskyDog instance
147+
Play = huskyDog.wantsToPlay()
148+
149+
# Print the value of the Play variable
150+
# This will print "Dog doesn't want to play"
151+
print(Play)

0 commit comments

Comments
 (0)