|
| 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