-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMy3rdLabActivityOOPProgramming.js
More file actions
84 lines (70 loc) · 2.76 KB
/
My3rdLabActivityOOPProgramming.js
File metadata and controls
84 lines (70 loc) · 2.76 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
// Task 1: Code a Person class
// WRITE YOUR CODE HERE - Define the Person class
// WRITE YOUR CODE HERE - Add a constructor with default parameters
// WRITE YOUR CODE HERE - Add the sleep() method
// WRITE YOUR CODE HERE - Add the doSomethingFun() method
class Person {
constructor(name = "Tom", age = 20, energy = 100) {
this.name = name;
this.age = age;
this.energy = energy;
}
//sleep(): Increases the energy property by 10.
sleep() {
this.energy += 10;
}
//doSomethingFun(): Decreases the energy property by 10.
doSomethingFun() {
this.energy -= 10;
}
}
// Task 2: Code a Worker class
// WRITE YOUR CODE HERE - Define the Worker class that extends Person
// WRITE YOUR CODE HERE - Add a constructor with additional parameters
// WRITE YOUR CODE HERE - Add the goToWork() method
class Worker extends Person {
constructor(name, age, energy, xp = 0, hourlyWage = 10) {
super(name, age, energy);
this.xp = xp;
this.hourlyWage = hourlyWage;
}
//goToWork(): Increases the xp property by 10.
goToWork() {
this.xp += 10;
}
}
// Task 3: Code an intern object, run methods
function intern() {
// WRITE YOUR CODE HERE - Instantiate the Worker class with the intern properties
// WRITE YOUR CODE HERE - Call the goToWork() method
// WRITE YOUR CODE HERE - Return the intern object
var intern = new Worker("Bob", 21, 110, 0, 10);
intern.goToWork();
return intern;
}
// Task 4: Code a manager object, methods
function manager() {
// WRITE YOUR CODE HERE - Instantiate the Worker class with the manager properties
// WRITE YOUR CODE HERE - Call the doSomethingFun() method
// WRITE YOUR CODE HERE - Return the manager object
var manager = new Worker("Alice", 30, 120, 100, 30);
manager.doSomethingFun();
return manager;
}
console.log(intern());
console.log(manager());
/*Output:
Worker { name: 'Bob', age: 21, energy: 110, xp: 10, hourlyWage: 10 }
Worker { name: 'Alice', age: 30, energy: 110, xp: 100, hourlyWage: 30 } */
/*Key takeaways:
Use the class keyword to define a blueprint for objects. Constructors initialize properties with default or custom
values.
Use the extends keyword to create a subclass, inheriting
all properties and methods from a parent class. Subclasses can have their own unique properties and
methods.
Instantiate classes to create specific objects with unique attributes. Use constructor arguments to customize the properties of each instance.
Methods define specific behaviours that operate on the object's properties, encapsulating functionality within
the class.
OOP makes code modular, reusable, and easier to
understand.
*/