Lab 10

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

BE108 Programming for Engineers

Laboratory 10: Objects and methods

Submission Due: End of laboratory class, submit the file on Moodle at least 10 minutes before
the end of laboratory class.

Total Marks = 10 marks for 10 weeks

Instructions:

Students are instructed to go through the given Lab and attempt each question.

Task 1: Methods are functions defined inside the body of a class. They are used to define the behaviors
of an object

1. Look at the sample class Parrot.


2. How many methods does it have?
3. List the attributes in this class.
4. What object can you notice in this code?
5. What is the output of

obj=Parrot()

obj.name

obj.age

Describe why you see the results as they are.

BE108 Programming for Engineers


class Parrot:

# instance attributes
def __init__(self, name, age):
self.name = name
self.age = age

# instance method
def sing(self, song):
return "{} sings {}".format(self.name, song)

def dance(self):
return "{} is now dancing".format(self.name)

# instantiate the object


blu = Parrot("Blu", 10)

# call our instance methods


print(blu.sing("'Happy'"))
print(blu.dance())

Task 2:
Add a new attribute color to the class Parrot.
Define the main method with the following requirements:
a. Create a 5 new objects of type Parrot. Store the objects into a list. The list will have 5
objects. Using for loop, print the color of all the parrots in the list. (Create a display
method inside class Parrot)
b. Search for parrots that are Orange colored. (Create a search function)

==============

BE108 Programming for Engineers

You might also like