-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_14.js
154 lines (103 loc) · 4.51 KB
/
day_14.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
// 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 {
constructor(name, age) {
this.name = name;
this.age = age;
}
greeting() {
console.log(`Hi there, my name is ${this.name} and I am ${this.age} years old!. Nice to meet ya :)`);
}
}
const prince = new Person("Prince", 21);
prince.greeting();
// Task 2 Add a method to the Person class that updates the age property and logs the updated age.
Person.prototype.setAge = function (age) {
this.age = age;
}
Person.prototype.sayName = function () {
console.log(`${this.name}`);
}
const rohan = new Person("Rohan", 34);
rohan.setAge(30);
rohan.greeting();
rohan.sayName();
// 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 extends Person {
static numOfStudent = 0;
constructor(name, age, studentId) {
super(name, age);
this.studentId = studentId;
// this.numOfStudent++;
console.log(`Number of student - ${++Student.numOfStudent}`);
}
}
const mohit = new Student("Mohit", 23, "20BCS9847");
console.log(`Mohit's student ID is ${mohit.studentId}.`);
// Task 4 Override the greeting method in the Student class to include the student ID in the message. Log the overridden greeting message.
Student.prototype.greeting = function () {
console.log(`Hi there, I am ${this.name}, my student id is ${this.studentId} and I am ${this.age} years old.`);
}
mohit.greeting();
// 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.
Person.genericGreeting = function () {
console.log("This is an static method in Person class.");
}
// mohit.genericGreeting() // this line give the error genericGreeting not defined
Person.genericGreeting(); // static methods are called directly by class name
// 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.
const student1 = new Student("Pushpraj", 22, "20BCS9866");
console.log(student1);
const student2 = new Student("Prince", 21, "20BCS1058");
console.log(student2);
// console.log(Student.numOfStudent);
// 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.
// 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.
Person.FirstName = "";
Person.LastName = "";
Person.prototype.setFullName = function (FirstName, LastName) {
Person.FirstName = FirstName;
Person.LastName = LastName;
}
Person.prototype.getFullName = function () {
console.log(`My full name is ${Person.FirstName} ${Person.LastName}.`);
}
const person1 = new Person("Radha", 20);
person1.setFullName("Radha", "Rani");
person1.getFullName();
// 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 {
#balance = 0;
deposite = (amount) => {
if(amount > 0) {
this.#balance += amount;
console.log(`${amount} debited to your balance.`);
}
else {
console.log("amount must be in positive for deposite.")
}
}
withdraw = (amount) => {
if(amount > 0 && amount <= this.#balance) {
this.#balance -= amount;
console.log(`${amount} credited from your balance.`);
}
else {
console.log("Insufficient balance")
}
}
getBalance() {
console.log(`Your current balance is ${this.#balance}.`);
}
}
// Task 10: Create an instance of the Account class and test the deposit and withdraw methods, logging the balance after each operation.
const account1 = new Account();
account1.getBalance();
account1.deposite(100);
account1.getBalance();
account1.withdraw(40);
account1.getBalance();