-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_assignmt_ust.py
More file actions
169 lines (127 loc) · 4.1 KB
/
Copy pathpy_assignmt_ust.py
File metadata and controls
169 lines (127 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
class Demo:
value = 0 # attribute
def __init__(self, val1, val2):
self.val1 = val1
self.val2 = val2
def Fun(self):
print(f"Value 1: {self.val1}")
def Gun(self):
print(f"Value 2: {self.val2}")
Obj1 = Demo(11, 21) #instances
Obj2 = Demo(51, 101)
# Calling
Obj1.Fun()
Obj2.Fun()
Obj1.Gun()
Obj2.Gun()
#2 BookStore.
class BookStore:
NoOfBooks = 0
def __init__(self, name, author):
self.Name = name
self.Author = author
BookStore.NoOfBooks += 1
def Display(self):
print(f"Book: {self.Name} by {self.Author}. No of books: {BookStore.NoOfBooks}")
Obj1 = BookStore("Linux System Programming", "Robert Love")
Obj1.Display()
Obj2 = BookStore("C Programming", "Dennis Ritchie")
Obj2.Display()
#3 Circle
class Circle:
PI = 3.14
def __init__(self):
self.Radius = 0.0
self.Area = 0.0
self.Circumference = 0.0
def Accept(self):
self.Radius = float(input("Enter the radius of the circle: "))
def CalculateArea(self):
self.Area = Circle.PI * self.Radius**2
def CalculateCircumference(self):
self.Circumference = 2 * Circle.PI * self.Radius
def Display(self):
print(f"Radius: {self.Radius}")
print(f"Area: {self.Area}")
print(f"Circumference: {self.Circumference}")
circle1 = Circle()
circle1.Accept()
circle1.CalculateArea()
circle1.CalculateCircumference()
circle1.Display()
#4 BankAccount
class BankAccount:
ROI = 10.5
def __init__(self, name, amount):
self.Name = name
self.Amount = amount
def Deposit(self):
deposit_amount = float(input("Enter the amount to deposit: "))
self.Amount += deposit_amount
def Withdraw(self):
withdraw_amount = float(input("Enter the amount to withdraw: "))
if withdraw_amount > self.Amount:
print("Insufficient balance!")
else:
self.Amount -= withdraw_amount
def CalculateInterest(self):
interest = (self.Amount * BankAccount.ROI) / 100
print(f"Interest: {interest}")
def Display(self):
print(f"Name: {self.Name}")
print(f"Amount: {self.Amount}")
account1 = BankAccount("John", 1000)
account1.Deposit()
account1.Withdraw()
account1.CalculateInterest()
account1.Display()
#5 Numbers.
class Numbers:
def __init__(self, value):
self.Value = value
def Factors(self):
print(f"Factors of {self.Value}: ", end="")
for i in range(1, self.Value):
if self.Value % i == 0:
print(i, end=" ")
print()
def SumFactors(self):
return sum(i for i in range(1, self.Value) if self.Value % i == 0)
def ChkPrime(self):
if self.Value < 2:
return False
for i in range(2, int(self.Value**0.5) + 1):
if self.Value % i == 0:
return False
return True
def ChkPerfect(self):
return self.SumFactors() == self.Value
number = Numbers(28)
number.Factors()
print(f"Is Prime: {number.ChkPrime()}")
print(f"Is Perfect: {number.ChkPerfect()}")
print(f"Sum of Factors: {number.SumFactors()}")
#6 Numbers.
class Numbers:
def __init__(self):
self.Value1 = 0
self.Value2 = 0
def Accept(self):
self.Value1 = float(input("Enter Value1: "))
self.Value2 = float(input("Enter Value2: "))
def Addition(self):
return self.Value1 + self.Value2
def Subtraction(self):
return self.Value1 - self.Value2
def Multiplication(self):
return self.Value1 * self.Value2
def Division(self):
if self.Value2 == 0:
return "Division by zero is not allowed"
return self.Value1 / self.Value2
operation = Numbers()
operation.Accept()
print(f"Addition: {operation.Addition()}")
print(f"Subtraction: {operation.Subtraction()}")
print(f"Multiplication: {operation.Multiplication()}")
print(f"Division: {operation.Division()}")