0% found this document useful (0 votes)
6 views7 pages

Lab - Week9 AI

Uploaded by

xibejir727
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Lab - Week9 AI

Uploaded by

xibejir727
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Object Oriented Programming

Author: Dr. Ayah Helal

Object Oriented
Programming
As we dive into the complexities of programming, we
are introduced to OOP. OOP is large and very complex
topic, but today we will work on understand the basic
points of OOP, such as classes, objects, encapsulation,
inheritance, and polymorphism.

We will understand how to create a class and object,


how to include them in different parts of your
application.

Manchester Metropolitan University OOP : Week 08


Introduction [ 1]

Welcome to the OOP Lab!


In this lab session, you will introduce Object-Oriented Programming
(OOP) in Python through a series of fun, game-inspired challenges.

You'll create heroes, warriors, and dragons, learning key OOP


concepts like classes, objects, encapsulation, inheritance, and
polymorphism.

By the end of this session, you will:


Understand and implement OOP principles in Python
Build interactive classes with real-world functionality
Apply encapsulation, inheritance, and polymorphism in a practical
scenario

Manchester Metropolitan University OOP : Week 08


Object Creation [1]

In this exercise, you will define a Hero class, create attributes for your character, and
display their stats. This will help you understand the fundamentals of classes and
objects in Python. Make sure to check the lectures for guidance on syntax.

[1] Create a class Hero that represents a character in the game.

[2] Define three attributes: name (string), level (integer), and health (integer).

[3] Define the initialization function (__init__) with these variables and assign them
values from the function parameters.

[4] Implement a method display_stats() that prints the hero’s details.

[5] Outside the class (directly to the left of the line without any indentation), create a
function game(), instantiate a hero object, and call display_stats() to showcase the
hero’s attributes.

[6] Call the game() function directly below where it is defined. This should be right
up against the left line with no indentation if you done everything correct.

Manchester Metropolitan University OOP : Week 08


Encapsulation [2]

A hero must manage their resources wisely. In this exercise, you will learn about
encapsulation by creating a private attribute _gold, ensuring controlled access
through methods.

[1] Modify the Hero class to include a private attribute _gold initialized to 0.

[2] Implement a method earn_gold(amount) that increases the hero’s gold by a


given amount.

[3] Implement a method check_gold() that returns the hero’s current gold balance.

As I said in the lecture, the use of _ is a convention, but we enforce when using
python to do encapsulation.

[5] In the game() function, your hero encounter a villager, who ask him to do some
work. After finishing the work, the hearo will earn_gold(), and print the total gold
balance using check_gold().

Manchester Metropolitan University OOP : Week 08


Inheritance [3]

In this exercise, you will use inheritance to create a Warrior class that extends the
Hero class and adds a weapon attribute.

[1] Create a subclass Warrior that inherits from Hero.

[2] Add a new attribute weapon to the Warrior class.

[3] Modify the __init__ method to accept the weapon parameter and initialize it.
When we initialise an inherited object, we have to run the super __init__ . Check
the lecture for example.

[4] Implement a method show_weapon() that prints the warrior's weapon.

[5] In the game() function, instantiate a Warrior object and call display_stats() and
show_weapon().

Manchester Metropolitan University OOP : Week 08


Polymorphism [4]

In this exercise, you will explore polymorphism, ensuring different characters can
have unique attack methods.

[1] Modify Hero and Warrior classes to also include an attack() method with
different attack styles.

[2] Create a list of fighters that includes instances of Hero, and Warrior.

[3] Use a loop to call attack() on each object, demonstrating polymorphism.

Manchester Metropolitan University OOP : Week 08


Next Steps [5]

Hopefully, these exercises have helped you think about how you can
In
A hero
earlier
must
incorporate weeks,
manage
thisespecially
intotheir
yourresources
up until we’ve
wisely.
assessment covered
to In this
get youaround
exercise,
going.40%youofwill
thelearn
marks,
about
we will
encapsulation
guide them a touchby creating
more. Inalater
private
weeks
attribute
(likely_gold,
weekensuring
5 and onwards),
controlled
weaccess
will advise
them on methods.
through implementations rather than saying do this, then this.

More leading advice would be:


Some
[1] ideas:
Modify the Hero class to include a private attribute _gold initialized to 0.
[1] We understand how to check the position of an object on the grid.
• EnemiesUsing
canconditional statements
be a class, andcan
where you their respective
have operators,
multiple differentfind
types.
a way to stop the player from moving outside of the grid bounds.
• Implement a battle system where heroes and enemies interact.

The[2]
• above
Add gives
an them the
inventory
Implement 40-49%
system
a method band,heroes
where and is better
earn_gold(amount) wordedand
can increases
that collect for the
theuse later criteria
items.
hero’s gold by a(less
guidance).
given amount.

[3] Implement a method check_gold() that returns the hero’s current gold balance.

Done and dusted?


[4] Prevent direct access to _gold, ensuring it can only be modified through class
methods.
Add anything you want here. Supplementary reading, or links to suitable topics on W3.

As I said in the lecturer, the use of _ is a convention, but we enforce when using
python to do encapsulation.

Manchester Metropolitan University OOP : Week 08

You might also like