Oops Assignment Solution
Oops Assignment Solution
1. Employee class has 3 data members EmployeeID, Gender (String), Salary and
PerformanceRating(Out of 5) of type int. It has a get() function to get these details from
the user.
2. JoiningDetail class has a data member DateOfJoining of type Date and a function
3. Information Class uses the marks from Employee class and the DateOfJoining date
from the JoiningDetail class to calculate the top 3 Employees based on their Ratings
and then Display, using readData, all the details on these employees in Ascending
Solution:
class Employee:
def __init__(self):
self.EmployeeID = None
self.Gender = None
self.Salary = None
self.PerformanceRating = None
def get(self):
class JoiningDetails:
self.DateofJoining = DateofJoining
1. Class Vehicle with a parameterized function Fare, that takes input value as fare and returns it to
calling Objects.
2. Create five separate variables Bus, Car, Train, Truck and Ship that call the Fare function.
3. Use a third variable TotalFare to store the sum of fare for each Vehicle Type.
Solution:
class Vehicle:
self.fare = fare
def get_fare(self):
return self.fare
class Bus(Vehicle):
pass
class Car(Vehicle):
pass
class Train(Vehicle):
pass
class Truck(Vehicle):
pass
class Ship(Vehicle):
pass
car = Car(40)
train = Train(150)
truck = Truck(80)
ship = Ship(200)
Q3. Consider an ongoing test cricket series. Following are the names of the players and their
Test Match 1 :
Dhoni : 56 , Balaji : 94
Test Match 2 :
Calculate the highest number of runs scored by an individual cricketer in both of the matches.
Create a python function Max_Score (M) that reads a dictionary M that recognizes the player
with the highest total score. This function will return ( Top player , Total Score ) . You can
consider the Top player as String who is the highest scorer and Top score as Integer .
Solution:
def Max_Score(M):
total_scores = {}
max_individual_score = 0
max_score_player = ''
max_individual_score = score
max_score_player = player
# Find the player with the maximum score across all matches
top_score = total_scores[top_player]
# Test
print(Max_Score(M))
Q4. Create a simple Card game in which there are 8 cards which are randomly chosen from a
deck. The first card is shown face up. The game asks the player to predict whether the next card
in the selection will have a higher or lower value than the currently showing card.
For example, say the card that’s shown is a 3. The player chooses “higher,” and the next card is
shown. If that card has a higher value, the player is correct. In this example, if the player had
chosen “lower,” they would have been incorrect. If the player guesses correctly, they get 20
points. If they choose incorrectly, they lose 15 points. If the next card to be turned over has the
Solution:
import random
'A' : 1,
'2' : 2,
'3' : 3,
'4' : 4,
'5' : 5,
'6' : 6,
'7' : 7,
'8' : 8,
'9' : 9,
'10' : 10,
'J' : 11,
'Q' : 12,
'K' : 13
key = list(card.keys())
final_deck = {}
for s in suits:
for k in key:
new_value = card[k]
final_deck[new_key] = new_value
final_deck_keys = list(final_deck.keys())
choice = []
for i in range(8):
chosen_card = random.choice(final_deck_keys)
choice.append(chosen_card)
final_deck_keys.remove(chosen_card)
initial_point = 0
for i in range(1,8):
print('Your current card is: ', choice[i-8])
inp = int(input('Predict the next card is higher or lower(Press 1 for lower, 2 for higher): '))
print('Predict using keys 1 or 2 i.e Press 1 for lower, 2 for higher', inp)
break
else:
flag = 1
else:
flag = 2
if (inp == flag):
initial_point = initial_point + 20
else:
initial_point = initial_point - 15
Q5 Create an empty dictionary called Car_0 . Then fill the dictionary with Keys : color , speed,
X_position and Y_position.
c) Now if the speed is Fast the coordinates of the X_pos gets incremented by 22.
Solution:
car_0 = {
'color' : None,
'speed' : 'medium',
'x_position' : 10,
'y_position' : 72
#car_0['speed'] = 'medium'
#car_0['x_position'] = 10
#car_0['y_position'] = 72
if car_0['speed'] == 'slow':
car_0['x_position'] += 2
car_0['x_position'] += 9
car_0['x_position'] += 22
print(car_0)
Q6. Show a basic implementation of abstraction in python using the abstract classes.
2. Implement abstraction with the other classes and base class as abstract class.
Solution:
class Shape(ABC):
@abstractmethod
def printarea(self):
return 0
class Rectangle(Shape):
self.breadth = breadth
def printarea(self):
print(self.length * self.breadth)
class Square(Shape):
self.side = side
def printarea(self):
8. Given a list of 50 natural numbers from 1-50. Create a function that will take every element from
the list and return the square of each element. Use the python map and filter methods to implement
the function on the given list.
Solution:
n=1
a = []
a.append(n)
n=n+1
def square(a):
b = []
for element in a:
c = element**2
b.append(c)
print(b)
square(a)
9. Create a class, Triangle. Its init() method should take self, angle1, angle2, and angle3 as
arguments.
11. Create a method named check_angles. The sum of a triangle's three angles should return True if
the sum is equal to 180, and False otherwise. The method should print whether the angles belong to
a triangle or not.
11.1 Write methods to verify if the triangle is an acute triangle or obtuse triangle
11.2 Create an instance of the triangle class and call all the defined methods.
11.3 Create three child classes of triangle class - isosceles_triangle, right_triangle and
equilateral_triangle.
right_triangle.
Solution:
class Triangle():
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
def check_angles(self):
if (sum == 180):
return True
else:
else:
class Number_of_Sides(Triangle):
sides = 3
class Isosceles_triangle(Triangle):
self.side1 = side1
self.side2 = side2
self.side3 = side3
def check_iso_triangle(self):
else:
class Right_triangle(Triangle):
def __init__(self, angle1, angle2, angle3):
def check_right_triangle(self):
else:
class Equilateral_triangle(Triangle):
self.side1 = side1
self.side2 = side2
self.side3 = side3
def check_equilateral_triangle(self):
else:
else:
print('The triangle do not belong to Isosceles Right Angle Triangle Triangle family')