Skip to content

Commit 4afd7f2

Browse files
committed
Oops practice set added
1 parent ef11c96 commit 4afd7f2

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

Oops-practice.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
'''
2+
Create a class programmer for storing information of a few programmers working at Microsoft.'''
3+
4+
class Programmer:
5+
company = "Microsoft"
6+
7+
def __init__(self, name, product):
8+
self.name = name
9+
self.product = product
10+
11+
def getInfo(self):
12+
print(f"The name of the {self.company} programmer is {self.name} and the product is {self.product}")
13+
14+
mahar = Programmer("Mahar", "Skype")
15+
kevin = Programmer("Kevin", "GitHub") # Changed alka to kevin
16+
mahar.getInfo() # Output: The name of the Microsoft programmer is Mahar and the product is Skype
17+
kevin.getInfo() # Output: The name of the Microsoft programmer is Kevin and the product is GitHub
18+
19+
20+
'''
21+
write a class calculator capable of finding square, cube, and the square root of a number.
22+
'''
23+
class Calculator:
24+
def __init__(self, num):
25+
self.number = num
26+
27+
def square(self):
28+
print(f"The value of {self.number} square is {self.number ** 2}")
29+
30+
def squareRoot(self):
31+
print(f"The value of {self.number} square root is {self.number ** 0.5}")
32+
33+
def cube(self):
34+
print(f"The value of {self.number} cube is {self.number ** 3}")
35+
36+
a = Calculator(9)
37+
a.square() # Output: The value of 9 square is 81
38+
a.squareRoot() # Output: The value of 9 square root is 3.0
39+
a.cube() # Output: The value of 9 cube is 729
40+
41+
42+
43+
'''Write a class Train which has methods to book a ticket, get status(no of seats),
44+
and get fare information of trains running under Indian Railways.
45+
'''
46+
class Train:
47+
def __init__(self, name, fare, seats):
48+
# Initialize instance attributes
49+
self.name = name
50+
self.fare = fare
51+
self.seats = seats
52+
53+
def getStatus(self):
54+
# Display train status
55+
print("************")
56+
print(f"The name of the train is {self.name}")
57+
print(f"The seats available in the train are {self.seats}")
58+
print("************")
59+
60+
def fareInfo(self):
61+
# Display fare information
62+
print(f"The price of the ticket is: Rs {self.fare}")
63+
64+
def bookTicket(self):
65+
# Book a ticket if seats are available
66+
if self.seats > 0:
67+
print(f"Your ticket has been booked! Your seat number is {self.seats}")
68+
self.seats -= 1 # Reduce available seats
69+
else:
70+
print("Sorry, this train is full! Kindly try in tatkal")
71+
72+
def cancelTicket(self, seatNo):
73+
pass # To be implemented for canceling tickets
74+
75+
# Create an instance and perform operations
76+
intercity = Train("Intercity Express: 14015", 90, 2)
77+
intercity.getStatus() # Output: The name and available seats of the train
78+
intercity.bookTicket() # Output: Successful ticket booking with seat number
79+
intercity.bookTicket() # Output: Successful ticket booking with seat number
80+
intercity.bookTicket() # Output: The train is full message
81+
intercity.getStatus() # Output: Updated available seats after bookings
82+
83+
84+
'''Can you change the self parameter inside a class to something else (say 'mahar')?
85+
Try changing self to 'slf' or 'mahar' and see the effects'''
86+
class Sample:
87+
def __init__(slf, name):
88+
slf.name = name
89+
90+
obj = Sample("mahar")
91+
print(obj.name) # no proble, ypu can change but its better to use self
92+

0 commit comments

Comments
 (0)