Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

class Rooster:

# init method

def init(self, color, age):

# self

self.color = color

self.age = age

# creating instances for the class Laptop

black_rooster = Rooster("Black", 4)

brown_rooster = Rooster("Brown", 3)

# printing the properties of the instances

print(f"The {black_rooster.color} Rooster has {black_rooster.age} years old")

print(f"The {brown_rooster.color} Rooster has {brown_rooster.age} years old")


Report

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

You might also like