Professional Documents
Culture Documents
Activity 2
Activity 2
# init method
# self
self.color = color
self.age = age
black_rooster = Rooster("Black", 4)
brown_rooster = Rooster("Brown", 3)
The given Python program defines a class called "Hooster" which has two instance attributes:
"color" and "age". These attributes are passed in as arguments to the class's constructor method,
__init__, and are stored as instance variables using the self keyword.
The program then creates two objects, "black_rooster" and "brown_rooster", and assigns the
values "black" and "4" to the "color" and "age" attributes of the first object, and "brown" and "3" to
the second object.
Finally, the program uses string formatting and the dot notation to access the attributes of each
object and print out the color and age of each rooster.
Overall this program is defining a class with attributes and creating objects from the class and
printing the attributes of the objects which is standard practice in Object Oriented
Programming.In this example, a new attribute called is_male is added to the class and is
assigned a value of True during object instantiation. Two new methods, crow() and lay_egg() are
also added. The crow() method returns a string "Cock-a-doodle-doo!", and the lay_egg() method
returns a string "Roosters can't lay eggs." You can access these methods and attributes by
calling them on the objects of the class