-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.js
More file actions
332 lines (256 loc) · 11.1 KB
/
task.js
File metadata and controls
332 lines (256 loc) · 11.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//* **************************************************************
//* Day 14 : Classes
//* **************************************************************
//* **************************************************************
//* 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.
// Defines a Person object with properties for name and age, and a method to greet others.
class Person {
// Constructor to initialize the properties
constructor (name, age) {
this.name = name;
this.age = age;
}
// Method to return a greeting message
greet() {
return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
}
}
// Create an instance of the Person class
const person = new Person("Suraj", 26);
console.log(person.greet());
//* Task 2: Add a method to the Person class that updates the age property and logs the updated age.
// Defines a Person object with properties for name and age, and a method to greet others.
class Person {
// Constructor to initialize the properties
constructor (name, age) {
this.name = name;
this.age = age;
}
// Method to return a greeting message
greet() {
return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
}
// Method to update the age and log the updated age
updateAge(newAge) {
this.age = newAge;
console.log(`Updated age : ${this.age}`);
}
}
// Create an instance of the Person class
const person = new Person("Suraj", 25);
console.log(person.greet());
person.updateAge(26);
//* **************************************************************
//* 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.
// Defines a Person object with properties for name and age, and a method to greet others.
class Person {
// Constructor to initialize the properties
constructor (name, age) {
this.name = name;
this.age = age;
}
// Method to return a greeting message
greet() {
return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
}
}
// Define the Student class that extends Person
class Student extends Person {
// Constructor to initialize properties of both Person and Student
constructor(name, age, studentID) {
super(name, age); // Call the constructor of the parent class
this.studentID = studentID; // Initialize the studentID property
}
// Method to return the student ID
getStudentID() {
return `Student ID : ${this.studentID}`;
}
}
// Create an instance of the Student class
const student = new Student("Suraj", 25, "S12345");
console.log(student.getStudentID());
//* Task 4: Override the greeting method in the Student class to include the student ID in the message. Log the overridden greeting message.
// Define the Student class that extends Person
class Student extends Person {
// Constructor to initialize properties of both Person and Student
constructor(name, age, studentID) {
super(name, age); // Call the constructor of the parent class
this.studentID = studentID; // Initialize the studentID property
}
// Method to return the student ID
getStudentID() {
return `Student ID : ${this.studentID}`;
}
// Override the greet method to include student ID
greet() {
return `Hello, my name is ${this.name}, I am ${this.age} years old, and my student ID is ${this.studentID}.`;
}
}
// Create an instance of the Student class
const student = new Student("Srj", 26, "S13579");
console.log(student.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.
// Defines a Person object with properties for name and age, and a method to greet others.
class Person {
// Constructor to initialize the properties
constructor (name, age) {
this.name = name;
this.age = age;
}
// Method to return a greeting message
greet() {
return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
}
// Static method to return a generic greeting message
static genericGreeting() {
return `Hello! This is a message from static method.`;
}
}
// Call the static method without creating an instance of the class
console.log(Person.genericGreeting());
//* 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.
// Define the Student class that extends Person
class Student extends Person {
// Static property to keep track of the number of students created
static studentCount = 0;
// Constructor to initialize properties of both Person and Student
constructor(name, age, studentID) {
super(name, age); // Call the constructor of the parent class
this.studentID = studentID; // Initialize the studentID property
Student.studentCount++; // Increment the student count
}
// Static method to get the student count
static getStudentCount() {
return Student.studentCount;
}
}
// Create instances of the Student class
const student2 = new Student("Suraj", 26, "S12345");
const student3 = new Student("Srj", 26, "S12349");
const student4 = new Student("SRJ", 26, "S12045");
// Log the total number of students after all instances are created
// Call the static method without creating an instance of the class
console.log(`Total number of students : ${Student.getStudentCount()}`);
//* **************************************************************
//* 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.
// Defines a Person object with properties for firstName and lastName, and method greet and fullName.
class Person {
// Constructor to initialize the properties
constructor (firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
// Getter method to return the full name
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
// Method to return a greeting message
greet() {
return `Hello, my name is ${this.fullName}.`;
}
}
// Create instances of the Person class
const person = new Person("Suraj", "Adhikari");
// Log the full name using the getter
console.log(person.fullName);
// Log the greeting message
console.log(person.greet())
//* 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.
// Defines a Person object with properties for firstName and lastName, and method greet and fullName.
class Person {
// Constructor to initialize the properties
constructor (firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
// Getter method to return the full name
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
// Setter method to update the name properties
set fullName(newFullName) {
// Split the new full name into first name and last name based on space
const [newFirstName, newLastName] = newFullName.split(" ");
// Update the firstName property with the new first name
this.firstName = newFirstName;
// Update the lastName property with the new last name
this.lastName = newLastName;
}
// Method to return a greeting message
greet() {
return `Hello, my name is ${this.fullName}.`;
}
}
// Create instances of the Person class
const person = new Person("Suraj", "Adhikari");
// Log the initial full name
console.log(`Initial full name : ${person.fullName}`);
// Update the name using the setter
person.fullName = "Srj Adhikari";
// Log the updated full name
console.log(`Updated full name : ${person.fullName}`);
// Log the greeting message with the updated name
console.log(person.greet());
//* **************************************************************
//* 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.
// Defines a Account object
class Account {
// Private field for balance
#balance;
// Constructor to initialize the balance
constructor(initialBalance) {
// Ensure the initial balance is non-negative
if(initialBalance < 0)
throw new Error(`Balance can not be negative.`)
else
this.#balance = initialBalance;
}
// Method to deposit money into the account
deposit(amount) {
if(amount <= 0)
throw new Error(`Deposit amount must be positive`);
else
this.#balance += amount;
}
// Method to withdraw money from the account
withdraw(amount) {
if(amount <= 0)
throw new Error(`Withdrawal amount must be positive`);
else if(amount > this.#balance)
throw new Error(`Insufficient balance`);
else
this.#balance -= amount;
}
// Method to get the current balance
getbalance() {
return this.#balance;
}
}
//* Task 10: Create an instance of the Account class and test the deposit and withdraw methods, logging the balance after each operation.
// Create instances of the Account class with a balance of 1000
const myAccount = new Account(1000);
console.log(`Initial balance : ${myAccount.getbalance()}`);
myAccount.deposit(500);
console.log(`Balance after deposit : ${myAccount.getbalance()}`);
myAccount.withdraw(700);
console.log(`Balance after withdrawal : ${myAccount.getbalance()}`);
//* **************************************************************
//* Achievement:
//* **************************************************************
//* By the end of these activities, you will:
//* • Define and use classes with properties and methods.
//* • Implement inheritance to extend classes.
//* • Utilize static methods and properties.
//* • Apply getters and setters for encapsulation.
//* • Understand and use private fields in classes (optional).