-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.js
More file actions
65 lines (55 loc) · 2.11 KB
/
classes.js
File metadata and controls
65 lines (55 loc) · 2.11 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
// 1. Copy and paste your prototype in here and refactor into class syntax.
// Base Constructor
class newCuboidMaker {
constructor(attributes) {
this.length = attributes.length;
this.width = attributes.width;
this.height = attributes.height;
}
volume() { // Volume Method
return (this.length * this.width * this.height);
}
surfaceArea() { // Surface Area Method
return 2 * (this.length * this.width + this.length * this.height + this.width * this.height);
}
}
// Create a new object that uses CuboidMaker
const new_cuboid = new newCuboidMaker({
length: 4,
width: 5,
height: 5
});
// Test your volume and surfaceArea methods by uncommenting the logs below:
console.log("Test your volume and surfaceArea methods for new_cuboid.");
console.log(new_cuboid.volume()); // 100
console.log(new_cuboid.surfaceArea()); // 130
// Stretch Task: Extend the base class CuboidMaker with a sub class called CubeMaker.
// Find out the formulas for volume and surface area for cubes and create those methods using the dimension properties from CuboidMaker.
// Test your work by logging out your volume and surface area.
class CubeMaker extends newCuboidMaker { // A cube is the same on all sides so you only have to have one side
constructor(attributes) {
super(attributes);
}
cube_volume() { // Volume of a cube -- Volume = side^3
return Math.pow(this.length, 3);
}
cube_surfaceArea() { // Surface Area Method -- Area = 6 * a^2
return 6 * Math.pow(this.length, 2);
}
}
const cube = new CubeMaker({
length: 5,
width: 5,
height: 5
});
// Test volume and surfaceArea methods for the cube
console.log("Test volume and surfaceArea methods for the cube.");
console.log(cube.volume()); // 100
console.log(cube.surfaceArea()); // 130
// Also see the this would work with cube functions if only had length
console.log("Also see the this would work with cube functions if only had length.");
const new_cube = new CubeMaker({
length: 5
});
console.log(cube.cube_volume()); // 100
console.log(cube.cube_surfaceArea()); // 130