0% found this document useful (0 votes)
2 views

EXP 8

This document outlines a Python program that demonstrates the use of classes and objects. It includes examples of creating classes such as 'string', 'car', and 'individual', with methods for manipulating and accessing their attributes. The program successfully implements various functionalities including string manipulation, car speed adjustments, and character interactions.

Uploaded by

Aristo Vince
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

EXP 8

This document outlines a Python program that demonstrates the use of classes and objects. It includes examples of creating classes such as 'string', 'car', and 'individual', with methods for manipulating and accessing their attributes. The program successfully implements various functionalities including string manipulation, car speed adjustments, and character interactions.

Uploaded by

Aristo Vince
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Ex-8_Classes and Objects

AIM:-
To create a python program to perform operations of class and objects.
THEORY:-
A class is a user-defined blueprint or prototype from which objects are
created. Classes provide a means of bundling data and functionality together.
Creating a new class creates a new type of object, allowing new instances of
that type to be made. Each class instance can have attributes attached to it
for maintaining its state. Class instances can also have methods (defined by
their class) for modifying their state.

Class creates a user-defined data structure, which holds its own data
members and member functions, which can be accessed

1. Write a Python class which has two methods get_String and print_String. get_String
accept a string from the user and print_String print the string in upper case.
CODE:-
class string:
def __init__(self, value = ""):
self.string = value
def get_string(self, str):
self.string = str
def print_string(self):
return self.string

s1 = string("Hello")
print(s1.print_string())
s1.get_string("Hello Python")
print(s1.print_string())

OUTPUT:-

Hello

Hello Python
2. Write a class named Car that has the following data attributes:
● __year_model (for the car’s year model)
● __make (for the make of the car)
● __speed (for the car’s current speed)

The Car class should have an _ _init_ _ method that accepts the car’s year, model and
make, as arguments. These values should be assigned to the object’s __year, __model
and __make data attributes. It should also assign 0 to the __speed data attribute.

The class should also have the following methods:


accelerate
The accelerate method should add 5 to the speed data attribute each time it is
called.
brake
The brake method should subtract 5 from the speed data attribute each time it is
called.

get_speed
The get_speed method should return the current speed.

Next, design a program that creates a Car object, and then calls the accelerate method
five times. After each call to the accelerate method, get the current speed of the car and
display it. Then call the brake method five times. After each call to the brake method, get
the current speed of the car and display it.
CODE:-
class car:
def __init__(self, year = 0, model = "", make = ""):
self.__year = year
self.__model = model
self.__make = make
self.__speed = 0
def accelerate(self):
self.__speed += 5
def brake(self):
self.__speed -= 5
def get_speed(self):
return self.__speed

c1 = car(2020, "Ferrari 812 Superfast", "Italy")


c1.accelerate()
c1.accelerate()
c1.accelerate()
c1.accelerate()
print("Speed is:" , c1.get_speed(), "Km/h")
c1.brake()
c1.brake()
print("Speed is:", c1.get_speed(), "Km/h")

OUTPUT:-
Speed is: 20 Km/h
Speed is: 10 Km/h

3. i) Define a simple class called Individual.

ii) Add an initialisation method which initializes the self.character_name instance attribute.

iii) Add an access method to the class that returns self.character_name. Call this method
get_character_name().

iv) Create an instance of the character class and assign it to the variable individual1. This class
instance should be assigned the character_name ‘Laurel’ on initialisation.

v) Create another instance, which should be assigned to the variable individual2. Set the name
to ‘Hardy’.

vi) Print the character name of individual1 and individual2 to the screen using the appropriate
method.

vii) Save this to a script called oop1.py.

CODE:-

class individual:
def __init__(self, name = ""):
self.__name = name
def get_attributes(self):
return self.__name

individual1 = individual("Laurel")
individual2 = individual("Hardy")
print(individual1.get_attributes())
print(individual2.get_attributes())

OUTPUT:-

Laurel

Hardy
4. Use the class, Individual, created in the previous exercise.

i) On initialisation, set the instance attribute self.happy to True. This should be done by default
(i.e. no parameter needs to be passed on instantiation in order to do this.)

ii) Create a modification method named switch_mood()that changes self.happy from True to
False (and vice versa).

iii) Create a method called speak() that returns “Hello, I am [self.name]” or ‘Go away!’,
depending on whether self.happy is set to True or False respectively.

iv) Create individual3 with character name initialised to ‘Lucille’

v) Write some code to try out these methods / attributes of Buster and Tobias.

vi) Save all this code to a script called oop2.py.

CODE:-
class Individual:
def __init__(self, name = ""):
self.name = name
self.happy = True
def switch_mood(self):
if self.happy == True:
self.happy = False
else:
self.happy = True
def speak(self):
if self.happy == True:
return "Hello, I am {}".format(self.name)
else:
return "Go away!"

individual3 = Individual("Lucille")
individual3.switch_mood()
print(individual3.speak())
individual4 = Individual("Buster")
print(individual4.speak())
individual5 = Individual("Tobias")
individual5.switch_mood()
print(individual5.speak())
OUTPUT:-
Go away!
Hello, I am Buster
Go away!

RESULT:-
The above program is implemented and executed successfully.

You might also like