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

CSE111 Lab Assignment 8

This document contains 7 programming tasks related to creating classes in Python. The tasks involve creating classes to represent complex numbers, bank accounts, fruits, exams, shapes, and Pokémon. Code snippets are provided that are meant to test the classes by producing specific output. The tasks range from basic class creation to adding additional methods and attributes to subclasses.

Uploaded by

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

CSE111 Lab Assignment 8

This document contains 7 programming tasks related to creating classes in Python. The tasks involve creating classes to represent complex numbers, bank accounts, fruits, exams, shapes, and Pokémon. Code snippets are provided that are meant to test the classes by producing specific output. The tasks range from basic class creation to adding additional methods and attributes to subclasses.

Uploaded by

Alam jovi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Course Title: Programming Language II

Course Code: CSE111


Lab Assignment no: 8
Task - 1
Let’s Play with Numbers!!!

Write the ComplexNumber class so that the following code generates the output
below.

class RealNumber: OUTPUT:


RealPart: 1.0
def __init__(self, r=0): ImaginaryPart: 1.0
self.__realValue = r --------------------
def getRealValue(self): RealPart: 5.0
return self.__realValue ImaginaryPart: 7.0
def setRealValue(self, r):
self.__realValue = r
def __str__(self):
return 'RealPart: '+str(self.getRealValue())

cn1 = ComplexNumber()
print(cn1)
print('---------')
cn2 = ComplexNumber(5,7)
print(cn2)
Task - 2

Write the ComplexNumber class so that the following code generates the output
below.

class RealNumber: OUTPUT:


def __init__(self, number=0): 8
self.number = number 2 + 1i
def __add__(self, anotherRealNumber): 3 + 5i
return self.number + anotherRealNumber.number 5 + 6i
def __sub__(self, anotherRealNumber): -1 - 4i
return self.number - anotherRealNumber.number
def __str__(self):
return str(self.number)

r1 = RealNumber(3)
r2 = RealNumber(5)
print(r1+r2)
cn1 = ComplexNumber(2, 1)
print(cn1)
cn2 = ComplexNumber(r1, 5)
print(cn2)
cn3 = cn1 + cn2
print(cn3)
cn4 = cn1 - cn2
print(cn4)
Task - 3

Write the CheckingAccount class so that the following code generates the output
below:

class Account: OUTPUT:


def __init__(self, balance): Number of Checking
self._balance = balance Accounts: 0
Account Balance: 0.0
def getBalance(self): Account Balance: 100.00
return self._balance Account Balance: 200.00
Number of Checking
Accounts: 3

print('Number of Checking Accounts: ', CheckingAccount.numberOfAccount)


print(CheckingAccount())
print(CheckingAccount(100.00))
print(CheckingAccount(200.00))
print('Number of Checking Accounts: ', CheckingAccount.numberOfAccount)
Task - 4
Write the Mango and the Jackfruit classes so that the following code generates the output
below:

class Fruit: OUTPUT:


def __init__(self, formalin=False, name=''): ----Printing Detail-----
self.__formalin = formalin Do not eat the Mango.
self.name = name Mangos are bad for you
----Printing Detail-----
def getName(self): Eat the Jackfruit.
return self.name Jackfruits are good for you

def hasFormalin(self):
return self.__formalin

class testFruit:
def test(self, f):
print('----Printing Detail----')
if f.hasFormalin():
print('Do not eat the',f.getName(),'.')
print(f)
else:
print('Eat the',f.getName(),'.')
print(f)

m = Mango()
j = Jackfruit()
t1 = testFruit()
t1.test(m)
t1.test(j)
Task - 5
Write the ScienceExam class so that the following code generates the output below:

class Exam: OUTPUT:


def __init__(self,marks): Marks: 100 Time: 90 minutes Number of
self.marks = marks Parts: 4
self.time = 60 ----------------------------------
Maths , English , Physics , HigherMaths
def examSyllabus(self): Part 1 - Maths
return "Maths , English" Part 2 - English
def examParts(self): Part 3 - Physics
return "Part 1 - Maths\nPart 2 - English\n" Part 4 - HigherMaths
==================================
Marks: 100 Time: 120 minutes Number of
Parts: 5
engineering = ScienceExam(100,90,"Physics","HigherMaths")
----------------------------------
print(engineering)
Maths , English , Physics , HigherMaths
print('----------------------------------')
, Drawing
print(engineering.examSyllabus()) Part 1 - Maths
print(engineering.examParts()) Part 2 - English
print('==================================') Part 3 - Physics
architecture = Part 4 - HigherMaths
ScienceExam(100,120,"Physics","HigherMaths","Drawing") Part 5 - Drawing
print(architecture)
print('----------------------------------')
print(architecture.examSyllabus())
print(architecture.examParts())
Task - 6

Given the following class, write the code for the Sphere and the Cylinder class so that
the following output is printed.

class Shape3D: OUTPUT:


Shape name: Sphere, Area Formula: 4 * pi * r
pi = 3.14159 * r
def __init__(self, name = 'Default', radius = 0): ----------------------------------
self._area = 0 Radius: 5, Height: No need
self._name = name Area: 314.159
self._height = 'No need' ==================================
self._radius = radius Shape name: Cylinder, Area Formula: 2 * pi *
r * (r + h)
----------------------------------
def calc_surface_area(self):
Radius: 5, Height: 10
return 2 * Shape3D.pi * self._radius
Area: 471.2385
def __str__(self):
return "Radius: "+str(self._radius)

sph = Sphere('Sphere', 5)
print('----------------------------------')
sph.calc_surface_area()
print(sph)
print('==================================')
cyl = Cylinder('Cylinder', 5, 10)
print('----------------------------------')
cyl.calc_surface_area()
print(cyl)
Task - 7
Write the PokemonExtra class so that the following code generates the output below:

class PokemonBasic: OUTPUT:

def __init__(self, name = 'Default', hp = 0, ------------Basic Info:--------------


weakness = 'None', type = 'Unknown'): Name: Default, HP: 0, Weakness: None
self.name = name Main type: Unknown
self.hit_point = hp Basic move: Quick Attack
self.weakness = weakness
self.type = type ------------Pokemon 1 Info:--------------
Name: Charmander, HP: 39, Weakness: Water
def get_type(self): Main type: Fire
return 'Main type: ' + self.type Basic move: Quick Attack

------------Pokemon 2 Info:--------------
def get_move(self):
Name: Charizard, HP: 78, Weakness: Water
return 'Basic move: ' + 'Quick Attack'
Main type: Fire, Secondary type: Flying
Basic move: Quick Attack
def __str__(self): Other move: Fire Spin, Fire Blaze
return "Name: " + self.name + ", HP: " +
str(self.hit_point) + ", Weakness: " + self.weakness

print('\n------------Basic Info:--------------')
pk = PokemonBasic()
print(pk)
print(pk.get_type())
print(pk.get_move())

print('\n------------Pokemon 1 Info:-------------')
charmander = PokemonExtra('Charmander', 39, 'Water',
'Fire')
print(charmander)
print(charmander.get_type())
print(charmander.get_move())

print('\n------------Pokemon 2 Info:-------------')
charizard = PokemonExtra('Charizard', 78, 'Water',
'Fire', 'Flying', ('Fire Spin', 'Fire Blaze'))
print(charizard)
print(charizard.get_type())
print(charizard.get_move())

You might also like