-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 14 classes.js
205 lines (144 loc) · 6.25 KB
/
Day 14 classes.js
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Activity 1: Class Definition
// Task 1: Define a class `Person` with properties `name` and `age`, and a method to return a greeting message. Create an instance of the class and log the greeting message.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, my name is {self.name} and I am {self.age} years old."
//Create an instance of Person
person1 = Person("John", 30)
// Log the greeting message
print(person1.greet())
//Task 2: Add a method to the `Person` class that updates the age property and logs the updated age.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, my name is {self.name} and I am {self.age} years old."
def update_age(self, new_age):
self.age = new_age
print(f"Updated age: {self.age}")
//Create an instance of Person
person1 = Person("John", 30)
// Update age and log the updated age
person1.update_age(31)
// Activity 2: Class Inheritance
//Task 3:Define a class `Student` that extends the `Person` class. Add a property `studentId` and a method to return the student ID. Create an instance of the `Student` class and log the student ID.
class Student(Person):
def __init__(self, name, age, studentId):
super().__init__(name, age)
self.studentId = studentId
def get_student_id(self):
return f"Student ID: {self.studentId}"
// Create an instance of Student
student1 = Student("Jane", 20, "S12345")
// Log the student ID
print(student1.get_student_id())
//Task 4:Override the greeting method in the `Student` class to include the student ID in the message. Log the overridden greeting message.
class Student(Person):
def __init__(self, name, age, studentId):
super().__init__(name, age)
self.studentId = studentId
def greet(self):
return f"Hello, my name is {self.name}, I am {self.age} years old, and my student ID is {self.studentId}."
//Create an instance of Student
student1 = Student("Jane", 20, "S12345")
//Log the overridden greeting message
print(student1.greet())
//Activity 3: Static Methods and Properties
//Task 5:Add a static method to the `Person` class that returns a generic greeting message. Call this static method without creating an instance of the class and log the message.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, my name is {self.name} and I am {self.age} years old."
def update_age(self, new_age):
self.age = new_age
print(f"Updated age: {self.age}")
@staticmethod
def generic_greet():
return "Hello, I am a person."
//Call the static method
print(Person.generic_greet())
//Task 6:Add a static property to the `Student` class to keep track of the number of students created. Increment this property in the constructor and log the total number of students.
class Student(Person):
student_count = 0
def __init__(self, name, age, studentId):
super().__init__(name, age)
self.studentId = studentId
Student.student_count += 1
def greet(self):
return f"Hello, my name is {self.name}, I am {self.age} years old, and my student ID is {self.studentId}."
// Create instances of Student
student1 = Student("Jane", 20, "S12345")
student2 = Student("Doe", 22, "S67890")
//Log the total number of students
print(f"Total number of students: {Student.student_count}")
// Activity 4: Getters and Setters
//Task 7:Add a getter method to the `Person` class to return the full name (assume a `firstName` and `lastName` property). Create an instance and log the full name using the getter.
class Person:
def __init__(self, firstName, lastName, age):
self.firstName = firstName
self.lastName = lastName
self.age = age
def greet(self):
return f"Hello, my name is {self.firstName} {self.lastName} and I am {self.age} years old."
def update_age(self, new_age):
self.age = new_age
print(f"Updated age: {self.age}")
@property
def full_name(self):
return f"{self.firstName} {self.lastName}"
//Create an instance of Person
person1 = Person("John", "Doe", 30)
//Log the full name using the getter
print(person1.full_name)
//Task 8:Add a setter method to the `Person` class to update the name properties (`firstName` and `lastName`). Update the name using the setter and log the updated full name.
class Person:
def __init__(self, firstName, lastName, age):
self.firstName = firstName
self.lastName = lastName
self.age = age
def greet(self):
return f"Hello, my name is {self.firstName} {self.lastName} and I am {self.age} years old."
def update_age(self, new_age):
self.age = new_age
print(f"Updated age: {self.age}")
@property
def full_name(self):
return f"{self.firstName} {self.lastName}"
@full_name.setter
def full_name(self, name):
self.firstName, self.lastName = name.split()
//Create an instance of Person
person1 = Person("John", "Doe", 30)
//Update the name using the setter
person1.full_name = "Jane Smith"
//Log the updated full name
print(person1.full_name)
//Activity 5: Private Fields (Optional)
//Task 9:Define a class `Account` with private fields for `balance` and a method to deposit and withdraw money. Ensure that the balance can only be updated through these methods.
class Account:
def __init__(self, balance=0):
self.__balance = balance
def deposit(self, amount):
if amount > 0:
self.__balance += amount
print(f"Deposited {amount}. New balance: {self.__balance}")
def withdraw(self, amount):
if 0 < amount <= self.__balance:
self.__balance -= amount
print(f"Withdrew {amount}. New balance: {self.__balance}")
def get_balance(self):
return self.__balance
//Task 10:Create an instance of the `Account` class and test the deposit and withdraw methods, logging the balance after each operation.
// Create an instance of Account
account1 = Account(100)
// Test deposit and withdraw methods
account1.deposit(50)
account1.withdraw(30)
// Log the balance
print(f"Final balance: {account1.get_balance()}")