Assignment 2 Oops
Assignment 2 Oops
Assignment 2-oops
ANS -
from datetime import date
class Employee:
def __init__(self):
self.EmployeeID = 0
self.Gender = ""
self.Salary = 0
self.PerformanceRating = 0
def get(self):
self.EmployeeID = int(input("Enter Employee ID: "))
self.Gender = input("Enter Gender: ")
self.Salary = float(input("Enter Salary: "))
self.PerformanceRating = int(input("Enter Performance Rating (out of
class JoiningDetail:
def __init__(self):
self.DateOfJoining = date.today()
def getDoJ(self):
year = int(input("Enter Year of Joining: "))
month = int(input("Enter Month of Joining: "))
day = int(input("Enter Day of Joining: "))
self.DateOfJoining = date(year, month, day)
if __name__ == "__main__":
num_employees = int(input("Enter the number of employees: "))
info = Information()
employees = info.readData(num_employees)
top_3_employees = info.top3Employees(employees)
info.displayTop3(top_3_employees)
Top 3 Employees:
Employee 1
Employee ID: 100
Gender: make
Salary: 5000.0
Performance Rating: 4
Date of Joining: 2015-02-01
Employee 2
Employee ID: 101
Gender: female
Salary: 57800.0
Performance Rating: 5
Date of Joining: 2020-05-01
class Vehicle:
def __init__(self, vehicle_type):
self.vehicle_type = vehicle_type
localhost:8888/nbconvert/html/Assignment2.ipynb?download=false 2/9
Downloaded by keerthu krish ([email protected])
lOMoARcPSD|42225480
In [3]: Q3. Consider an ongoing test cricket series. Following are the names of the
scores in the test1 and 2.
Test Match 1 :
Dhoni : 56 , Balaji : 94
Test Match 2 :
Balaji : 80 , Dravid : 105
Calculate the highest number of runs scored by an individual cricketer in bo
Create a python function Max_Score (M) that reads a dictionary M that recogn
with the highest total score. This function will return ( Top player , Total
consider the Top player as String who is the highest scorer and Top score as
Input : Max_Score({‘test1’:{‘Dhoni’:56, ‘Balaji : 85}, ‘test2’:{‘Dhoni’ 87,
Output : (‘Balaji ‘ , 200)
def Max_Score(scores):
top_player = ""
top_score = 0
# Example input
matches = {
'test1': {'Dhoni': 56, 'Balaji': 85},
'test2': {'Dhoni': 87, 'Balaji': 200}
}
result = Max_Score(matches)
print(result) # Output: ('Balaji', 200)
('Balaji', 200)
In [ ]: 4. Create a simple Card game in which there are 8 cards which are randomly c
deck. The first card is shown face up. The game asks the player to predict w
in the selection will have a higher or lower value than the currently showin
For example, say the card that’s shown is a 3. The player chooses “higher,”
shown. If that card has a higher value, the player is correct. In this examp
chosen “lower,” they would have been incorrect. If the player guesses correc
localhost:8888/nbconvert/html/Assignment2.ipynb?download=false 3/9
Downloaded by keerthu krish ([email protected])
lOMoARcPSD|42225480
import random
if next_card == current_card:
print("The next card has the same value. You are incorrect.")
score -= 15
elif (user_prediction == "higher" and next_card > current_card) or (user
print(f"The next card is {next_card}. You are correct.")
score += 20
else:
print(f"The next card is {next_card}. You are incorrect.")
score -= 15
In [1]: Q5. Create an empty dictionary called Car_0 . Then fill the dictionary with
, X_position and Y_position.
car_0 = {'x_position': 10, 'y_position': 72, 'speed': 'medium'} .
a) If the speed is slow the coordinates of the X_pos get incremented by 2.
b) If the speed is Medium the coordinates of the X_pos gets incremented by 9
c) Now if the speed is Fast the coordinates of the X_pos gets incremented by
Print the modified dictionary
In [2]: Q6. Show a basic implementation of abstraction in python using the abstract
1. Create an abstract class in python.
2. Implement abstraction with the other classes and base class as abstract c
@abstractmethod
def area(self):
pass
# Concrete subclass 1
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.1415 * self.radius * self.radius
# Concrete subclass 2
class Square(Shape):
def __init__(self, side_length):
self.side_length = side_length
def area(self):
return self.side_length * self.side_length
class Animal:
def __init__(self, name, age=0): # Make 'age' optional with a default v
self._name = name # Protected member using a single underscore
self.__age = age # Private member using double underscore
def make_sound(self):
pass
def display_info(self):
localhost:8888/nbconvert/html/Assignment2.ipynb?download=false 5/9
Downloaded by keerthu krish ([email protected])
lOMoARcPSD|42225480
class Dog(Animal):
def make_sound(self):
return "Woof!"
class Cat(Animal):
def make_sound(self):
return "Meow!"
class Bird(Animal):
def make_sound(self):
return "Chirp!"
# Demonstrate polymorphism
animals = [dog, cat, bird]
In [8]: 8. Given a list of 50 natural numbers from 1-50. Create a function that will
from the list and return the square of each element. Use the python map and
implement the function on the given list.
# Use map to apply the square function to each element in the list
squared_numbers = list(map(square, numbers))
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 3
24, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024,
1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 202
5, 2116, 2209, 2304, 2401, 2500]
In [9]: Q9. Create a class, Triangle. Its init() method should take self, angle1, an
arguments.
class Triangle:
def __init__(self, angle1, angle2, angle3):
self.angle1 = angle1
self.angle2 = angle2
localhost:8888/nbconvert/html/Assignment2.ipynb?download=false 6/9
Downloaded by keerthu krish ([email protected])
lOMoARcPSD|42225480
Angle 1: 60
Angle 2: 60
Angle 3: 60
In [10]: Q10. Create a class variable named number_of_sides and set it equal to 3.
class Triangle:
number_of_sides = 3 # Class variable
In [ ]: Q11. Create a method named check_angles. The sum of a triangle's three angle
True if the sum is equal to 180, and False otherwise. The method should prin
angles belong to a triangle or not.
11.1 Write methods to verify if the triangle is an acute triangle or obtuse
11.2 Create an instance of the triangle class and call all the defined metho
11.3 Create three child classes of triangle class - isosceles_triangle, righ
equilateral_triangle.
11.4 Define methods which check for their properties.
11-
class Triangle:
def __init__(self, angle1, angle2, angle3):
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
def check_angles(self):
if self.angle1 + self.angle2 + self.angle3 == 180:
print("The angles belong to a triangle.")
return True
else:
print("The angles do not belong to a triangle.")
return False
localhost:8888/nbconvert/html/Assignment2.ipynb?download=false 7/9
Downloaded by keerthu krish ([email protected])
lOMoARcPSD|42225480
11.1
class Triangle:
# ... (previous code) ...
def is_acute(self):
if all(angle < 90 for angle in [self.angle1, self.angle2, self.angle
return True
return False
def is_obtuse(self):
if any(angle > 90 for angle in [self.angle1, self.angle2, self.angle
return True
return False
11.2-
# Create an instance of the Triangle class
triangle = Triangle(60, 60, 60)
11.3-
class IsoscelesTriangle(Triangle):
def __init__(self, angle1, angle2):
super().__init__(angle1, angle2, 180 - (angle1 + angle2))
class RightTriangle(Triangle):
def __init__(self, angle1, angle2):
super().__init__(angle1, angle2, 90)
class EquilateralTriangle(Triangle):
def __init__(self, angle):
super().__init__(angle, angle, angle)
11.4 -
class Triangle:
# ... (previous code) ...
def is_equilateral(self):
if self.angle1 == self.angle2 == self.angle3:
return True
return False
class IsoscelesTriangle(Triangle):
def is_isosceles(self):
localhost:8888/nbconvert/html/Assignment2.ipynb?download=false 8/9
Downloaded by keerthu krish ([email protected])
lOMoARcPSD|42225480
class RightTriangle(Triangle):
def is_right_triangle(self):
if any(angle == 90 for angle in [self.angle1, self.angle2, self.angl
return True
return False
class Triangle:
def __init__(self, angle1, angle2, angle3):
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
class IsoscelesTriangle(Triangle):
def is_isosceles(self):
if self.angle1 == self.angle2 or self.angle2 == self.angle3 or self.
return True
return False
class RightTriangle(Triangle):
def is_right_triangle(self):
if any(angle == 90 for angle in [self.angle1, self.angle2, self.angl
return True
return False
def is_isosceles_right_triangle(self):
return self.is_isosceles() and self.is_right_triangle()
localhost:8888/nbconvert/html/Assignment2.ipynb?download=false 9/9
Downloaded by keerthu krish ([email protected])