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

Lab 13 OOP

The document outlines a lab focused on Object-Oriented Programming (OOP) in Python, specifically through the creation of a Car class and an Account class. It details the steps for defining classes, creating methods, and managing private attributes, emphasizing encapsulation. The lab concludes with a practical application of the concepts learned, showcasing how OOP enhances code organization and maintainability.

Uploaded by

ams216617
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 views5 pages

Lab 13 OOP

The document outlines a lab focused on Object-Oriented Programming (OOP) in Python, specifically through the creation of a Car class and an Account class. It details the steps for defining classes, creating methods, and managing private attributes, emphasizing encapsulation. The lab concludes with a practical application of the concepts learned, showcasing how OOP enhances code organization and maintainability.

Uploaded by

ams216617
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/ 5

--------------------------------------------

| Introduction to Programming |
| Ali Dumlu |
| Email: [email protected] |
--------------------------------------------

# Lab 13: Object-Oriented Programming

Objective: The objective of this lab is to gain practical experience with


the fundamental concepts of Object-Oriented Programming (OOP) in Python. This
included: defining classes and objects, utilizing attributes and methods,
and understanding encapsulation.

Part 1: Class Car


In this part, you will create a simple class called Car to represent
different cars. Each car object will have attributes such as make, model,
year, and methods to display information about the car.

Step 1: Defining the Class

Start by defining the class using the class keyword followed by the
class name Car. Inside the class, define the __init__ method to
initialize the object's attributes.

class Car:

def __init__(self, make, model, year):

self.make = make

self.model = model

self.year = year

self.odometer_reading = 0

Step 2: Creating Methods

1. Provide a method like get_description() to retrieve a description


of the car, including its make, model, and year.

def get_description(self):

return f"This {self.make} {self.model} is made in {self.year}


and it has {self.odometer_reading} miles on it."
Step 3: Demonstrate Usage

Outside of the class create two instances of the class and demonstrate
how to use the methods to access and modify the private attribute.

1. Create car objects.

car1 = Car("Toyota", "Camry", 2020)


car2 = Car("Honda", "Accord", 2018)

2. Display car objects’ description.

print(car1.get_description())
print(car2.get_description())

3. Display the odometer for each car object.

print(f"This car has {car1.odometer_reading} miles on it.")


print(f"This car has {car2.odometer_reading} miles on it.")

4. Increase odometer by different numbers of miles for each car object.

car1.odometer_reading += 100
car2.odometer_reading -= 250

5. Display the odometer for each car object after update.

print(f"This car has {car1.odometer_reading} miles on it.")


print(f"This car has {car2.odometer_reading} miles on it.")

Step 4: Making the odometer_reading Attribute Private

As you can notice, accessing odometer_reading directly could cause a


serious problem by changing its value to illegal value. To avoid this
problem use a double underscore “__” prefix to make the
odometer_reading attribute private to control how the attribute is
accessed and modified. This convention in Python hides the attribute
from outside access, effectively making it private to the class.

self.__odometer_reading = 0

Step 5: Add Methods to Handle Private Attribute

1. Define methods within the class to provide controlled access to the


private attribute.
2. Create methods like read_odometer() to retrieve the odometer reading
and increase_odometer() to modify it.

def read_odometer(self):

return f"This car has {self.__odometer_reading} miles on it."

def increase_odometer(self, miles):


if miles > 0:
self.__odometer_reading += miles
else:
print("You can’t decrease the odometer. Please provide a
positive value for miles.")
3. Modify get_description method to use read_odometer method to get
odometer_reading value.

def get_description(self):

return f"This {self.make} {self.model} is made in {self.year}


and {self.read_odometer()}"

Step 6: Check The Code Updates

Update the test program to make use of the changes which has been made.

1. Create car objects.

car1 = Car("Toyota", "Camry", 2020)


car2 = Car("Honda", "Accord", 2018)

2. Display car objects’ description.

print(car1.get_description())
print(car2.get_description())

3. Display the odometer for each car object.

print(f"car1.read_odometer()}")
print(f"car2.read_odometer()}")

4. Increase odometer by different numbers of miles for each car object.

car1.increase_odometer(100)
car2.increase_odometer(250)

5. Display the odometer for each car object after update.

print(car1.read_odometer())
print(car2.read_odometer())
Questions

Q1: Define a new class called Account to represent a type of bank


account.

1. When the class is instantiated, you should provide the account


number, the name of the account holder, an opening balance and the
type of account (which can be a string representing 'current',
'deposit' or 'investment' etc.). This means that there must be an
__init__ method and you will need to store the data within the object.
Make sure to make the balance attribute private.

2. Provide three instance methods for the Account; deposit(amount),


withdraw(amount) and get_balance().
The behaviour of these methods should be as expected:
• deposit: increases the balance if the input is a positive number,
otherwise it will show a message to indicate that you can’t
deposit negative value.
• withdraw: decreases the balance if the input is a positive
number, otherwise it will show a message to indicate that you
can’t deposit negative value. Also you need to check if the
balance is greater than the withdraw amount or not.
• get_balance: returns the current balance.

3. Define a simple test application to verify the behavior of your


Account class. Create several accounts, display their information,
apply some deposit and withdraw operations on them then print the
information again to check the effect of the operations.

The following output illustrates what the result of running this test
application might look like:

Before the operations:

Account: 1457 - Ahmad, current account = 1346.50

Account: 3164 - Khalid, savings account = 2334.55

Account: 8461 - Turki, investment account = 1284.45

----------------------------------------------------------------

After the operations:

Account: 1457 - Ahmad, current account = 3215.41

Account: 3164 - Khalid, savings account = 1346.78

Account: 8461 - Turki, investment account = 2643.61


Conclusion:
This lab provided a hands-on introduction to OOP principles in Python.
You successfully created classes, defined object attributes and
methods, and explored the concept of encapsulation. By implementing
these principles, you observed how OOP can improve code organization,
reusability, and maintainability.

You might also like