Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Lab-10 - Inheritance (Classwork+Homework)

Uploaded by

Ilham Morsalin
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)
17 views

Lab-10 - Inheritance (Classwork+Homework)

Uploaded by

Ilham Morsalin
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/ 11

_+

Course Code: CSE111

Course Title: Programming Language II

Lab No: 10

Topic: OOP (Inheritance)

Number of tasks: 5 Classwork + 4 Homework


** You are not allowed to change any of the code of the tasks
** Use Inheritance to solve all problems

Classwork Part
Task - 1
Given the following classes, write the code for the BBA_Student class so that the
following output is printed:

class Student: Output:


def __init__(self, name='Just a student', dept='nothing'): Name: default Department: BBA
self.__name = name Name: Humpty Dumpty Department: BBA
Name: Little Bo Peep Department: BBA
self.__department = dept
def set_department(self, dept):
self.__department = dept
def get_name(self):
return self.__name
def set_name(self,name):
self.__name = name
def detail(self):
return 'Name: '+self.__name+' Department: '+self.__department

#write your code here

print(BBA_Student().detail())
print(BBA_Student('Humpty Dumpty').detail())
print(BBA_Student('Little Bo Peep').detail())
Task – 2

class Vehicle: OUTPUT:


def __init__(self): Part 1
self.x = 0 ------
self.y = 0 (0 , 0)
(0 , 1)
def moveUp(self):
(-1 , 1)
self.y += 1
(-1 , 0)
def moveDown(self):
(0 , 0)
self.y -= 1
------
def moveRight(self): Part 2
self.x += 1 ------
def moveLeft(self): (0 , 0)
self.x -= 1 (-1 , -1)
def detail(self): False
return '('+str(self.x)+' , '+str(self.y)+')' True
#write your code here

print('Part 1')
print('------')
car = Vehicle()
print(car.detail())
car.moveUp()
print(car.detail())
car.moveLeft()
print(car.detail())
car.moveDown()
print(car.detail())
car.moveRight()
print(car.detail())
print('------')
print('Part 2')
print('------')
car1 = Vehicle2010()
print(car1.detail())
car1.moveLowerLeft()
print(car1.detail())
car2 = Vehicle2010()
car2.moveLeft()
print(car1.equals(car2))
car2.moveDown()
print(car1.equals(car2))

A vehicle assumes that the whole world is a 2-dimensional graph paper. It maintains its
x and y coordinates (both are integers). The vehicle gets manufactured (constructed) at
(0, 0) coordinate.

Subtasks:

1. Design a Vehicle2010 class that inherits movement methods from Vehicle and
adds new methods called move UpperRight, UpperLeft, LowerRight,
LowerLeft. Each of these diagonal move methods must re-use two inherited and
appropriate move methods.
2. Write an “equals” method that tests if significant class properties are the same
(in this case x and y).

Note: All moves are 1 step. That means a single call to any move method changes
value of either x or y or both by 1.

Task - 3
Given the following classes, write the code for the Cricket_Tournament and the
Tennis_Tournment class so that the following output is printed.

class Tournament: OUTPUT:


def __init__(self,name='Default'): Cricket Tournament Name: Default
self.__name = name Number of Teams: 0
def set_name(self,name): Type: No type
self.__name = name -----------------------
def get_name(self): Cricket Tournament Name: IPL
return self.__name Number of Teams: 10
Type: t20
#write your code here -----------------------
Tennis Tournament Name: Roland Garros
ct1 = Cricket_Tournament() Number of Players: 128
print(ct1.detail())
print("-----------------------")
ct2 = Cricket_Tournament("IPL",10,"t20")
print(ct2.detail())
print("-----------------------")
tt = Tennis_Tournament("Roland Garros",128)
print(tt.detail())
Task - 4
Given the following classes, write the code for the Book and the CD class so that the
following output is printed.

class Product: OUTPUT:


def __init__(self,id, title, price): ID: 1 Title: The Alchemist Price: 500
self.__id = id ISBN: 97806 Publisher: HarperCollins
self.__title = title -----------------------
self.__price = price ID: 2 Title: Shotto Price: 300
def get_id_title_price(self): Band: Warfaze Duration: 50 minutes
return "ID: "+str(self.__id)+" Title:"+self.__title+ Genre: Hard Rock
"Price: "+str(self.__price)

#write your code here

book = Book(1,"The Alchemist",500,"97806","HarperCollins")


print(book.printDetail())
print("-----------------------")
cd = CD(2,"Shotto",300,"Warfaze",50,"Hard Rock")
print(cd.printDetail())
Task - 5
Given the following classes, write the code for the Dog and the Cat class so that the following
output is printed.

class Animal: OUTPUT:


def __init__(self,sound): Animal does not make sound
self.__sound = sound meow
bark
def makeSound(self):
return self.__sound

class Printer:
def printSound(self, a):
print(a.makeSound())

#write your code here

d1 = Dog('bark')
c1 = Cat('meow')
a1 = Animal('Animal does not make sound')
pr = Printer()
pr.printSound(a1)
pr.printSound(c1)
pr.printSound(d1)
Homework Part
Task - 1
Given the following classes, write the code for the Triangle and the Trapezoid class so that
the following output is printed.

class Shape: OUTPUT:


Shape name: Default
def __init__(self, name='Default', height=0, base=0): Height: 0, Base: 0
self.area = 0 Area: 0.0
self.name = name ---------------------------
self.height = height Shape name: Triangle
self.base = base Height: 10, Base: 5
Area: 25.0
---------------------------
def get_height_base(self): Shape name: Trapezoid
return "Height: "+str(self.height)+", Base: "+str(self.base) Height: 10, Base: 6, Side_A: 4
Area: 50.0
#write your code here

tri_default = triangle()
tri_default.calcArea()
print(tri_default.printDetail())
print('--------------------------')
tri = triangle('Triangle', 10, 5)
tri.calcArea()
print(tri.printDetail())
print('---------------------------')
trap = trapezoid('Trapezoid', 10, 6, 4)
trap.calcArea()
print(trap.printDetail())
Task - 2
Given the following classes, write the code for the Player and the Manager class so that the
following output is printed. To calculate the match earnings use the following formula:
1. Player: (total_goal * 1000) + (total_match * 10)
2. Manager: match_win * 1000

class SportsPerson: OUTPUT:


Name: Ronaldo, Team Name: Al-Nassr
def __init__(self, team_name, name, role): Team Role: Striker
self.__team = team_name Total Goal: 25, Total Played: 32
self.__name = name Goal Ratio: 0.78125
self.role = role Match Earning: 25320K
self.earning_per_match = 0 ----------------------------------
Name: Zidane, Team Name: Real Madrid
Team Role: Manager
def get_name_team(self): Total Win: 25
return 'Name: '+self.__name+', Team Name: ' +self.__team Match Earning: 25000K

#write your code here

player_one = Player('Al-Nassr', 'Ronaldo', 'Striker', 25, 32)


player_one.calculate_ratio()
player_one.print_details()
print('------------------------------------------')
manager_one = Manager('Real Madrid', 'Zidane', 'Manager', 25)
manager_one.print_details()
Task - 3

The tea company Kazi and Kazi (KK) has decided to produce a new line of flavored teas.
Design the KK_tea (parent) and KK_flavoured_tea (child) classes so that the following output
is produced. The KK_flavoured_tea class should inherit KK_tea. Note that:
● An object of either class represents a single box of teabags.
● Each tea bag weighs 2 grams.
● The status of an object refers to whether it is sold or not
Hint: you should use class methods/variables

t1 = KK_tea(250) OUTPUT:
print("-----------------1-----------------") -----------------1-----------------
t1.product_detail() Name: KK Regular Tea, Weight: 100
print("-----------------2-----------------") Tea Bags: 50, Price: 250
KK_tea.total_sales() Status: False
-----------------2-----------------
print("-----------------3-----------------")
Total sales: {'KK Regular Tea': 0}
t2 = KK_tea(470, 100)
-----------------3-----------------
t3 = KK_tea(360, 75) -----------------4-----------------
KK_tea.update_sold_status_regular(t1, t2, t3) Name: KK Regular Tea, Weight: 150
print("-----------------4-----------------") Tea Bags: 75, Price: 360
t3.product_detail() Status: True
print("-----------------5-----------------") -----------------5-----------------
KK_tea.total_sales() Total sales: {'KK Regular Tea': 3}
print("-----------------6-----------------") -----------------6-----------------
t4 = KK_flavoured_tea("Jasmine", 260, 50) -----------------7-----------------
Name: KK Jasmine Tea, Weight: 100
t5 = KK_flavoured_tea("Honey Lemon", 270, 45)
Tea Bags: 50, Price: 260
t6 = KK_flavoured_tea("Honey Lemon", 270, 45)
Status: False
print("-----------------7-----------------") -----------------8-----------------
t4.product_detail() Name: KK Honey Lemon Tea, Weight: 90
print("-----------------8-----------------") Tea Bags: 45, Price: 270
t6.product_detail() Status: False
print("-----------------9-----------------") -----------------9-----------------
KK_flavoured_tea.update_sold_status_flavoured(t4, -----------------10-----------------
t5, t6) Total sales: {'KK Regular Tea': 3, 'KK
print("-----------------10-----------------") Jasmine Tea': 1, 'KK Honey Lemon Tea': 2}
KK_tea.total_sales()
Task - 4
Given a TwoDVector class, design the ThreeDVector class that inherits 2D vector. You need to
implement the following features:
● Similar to X and Y of 2D vector, there will be Z of 3D vector.
● Write a method add3DVectors() that adds 3D vectors. It must reuse the
add2DVectors() function and be written with the same parameters. The only difference
is that, in 3D vectors, the Z components are added as well.
● Write a multiplyScalar() method that takes an integer as parameter and multiplies it with
all 3 components separately (scalar multiplication).
● Write a calculateLength() that returns the length of the 3D vector using the following
formula:
2 2 2
○ 𝑋 + 𝑌 +𝑍
● Write a print3DVector() similar to the print2DVector() method.
● 2D vector: Xi + Yj
3D vector: Xi + Yj + Zk

class TwoDVector: OUTPUT:


def __init__(self, x, y): ===============
self.x = x 9i + 21j
self.y = y ===============
def add2DVectors(self, *vectors): ===============
14i + 17j -2k
for i in vectors:
===============
self.x += i.x
42i + 51j -6k
self.y += i.y ===============
def print2DVector(self): 66.34003316248794
if self.y >= 0:
y = "+ "+str(self.y)
else:
y = str(self.y)
print(f"{self.x}i {y}j")

TwoDV1 = TwoDVector(5, 6)
TwoDV2 = TwoDVector(3, 7)
TwoDV3 = TwoDVector(1, 8)
print("===============")
TwoDV1.add2DVectors(TwoDV2, TwoDV3)
TwoDV1.print2DVector()
print("===============")
ThreeDV1 = ThreeDVector(5, 6, 1)
ThreeDV2 = ThreeDVector(1, 9, -7)
ThreeDV3 = ThreeDVector(8, 2, 4)
print("===============")
ThreeDV1.add3DVectors(ThreeDV2,ThreeDV3)
ThreeDV1.print3DVector()
print("===============")
ThreeDV1.multiplyScalar(3)
ThreeDV1.print3DVector()
print("===============")
print(ThreeDV1.calculateLength())

You might also like