Skip to content

Commit 0f35d61

Browse files
committed
Class Chapter Examples and Exercises
1 parent d5f9309 commit 0f35d61

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

classes/chapter-examples/ClassExamples01.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//Try adding new properties inside constructor.
22
class Astronaut {
3-
constructor(name, age, mass){
3+
constructor(name, age, mass, eyeColor){
44
this.name = name;
55
this.age = age;
66
this.mass = mass;
7+
this.eyeColor = eyeColor;
78
}
89
}
910

@@ -18,4 +19,11 @@ fox.color = 'red';
1819
console.log(fox);
1920
console.log(fox.age, fox.color);
2021

21-
//Try modifying or adding properties below.
22+
//Try modifying or adding properties below.
23+
24+
let beaver = new Astronaut('Beaver', 6, 14);
25+
console.log(beaver);
26+
console.log(Astronaut); //[class Astronaut]
27+
28+
beaver.color = "brown";
29+
console.log(beaver);

classes/chapter-examples/ClassExamples02.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ let tortoise = new Astronaut('Speedy', 120);
1414

1515
console.log(tortoise.name, tortoise.age, tortoise.mass);
1616

17-
// What happens if we call Astronaut and pass in MORE than 3 arguments? TRY IT!
17+
// What happens if we call Astronaut and pass in MORE than 3 arguments? TRY IT!
18+
19+
//It says undefined

classes/chapter-examples/Inheritance.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ class Tiger extends Panthera {
2020

2121
let tigger = new Tiger();
2222

23-
console.log(tigger);
23+
console.log(tigger);
24+
25+
let tony = new Tiger();
26+
tony.brand = "Frosty Flakes";
27+
console.log(tony);

classes/exercises/ClassExercises.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,50 @@
11
// Define your Book class here:
2-
2+
class Book {
3+
constructor(title, author, copyrightDate, isbn, numOfPages, checkOutCount, isDiscarded) {
4+
this.title = title;
5+
this.author = author;
6+
this.copyrightDate = copyrightDate;
7+
this.isbn = isbn;
8+
this.numOfPages = numOfPages;
9+
this.checkOutCount = checkOutCount;
10+
this.isDiscarded = isDiscarded;
11+
}
12+
dispose(currentYear) {
13+
if (currentYear - this.copyrightDate > 5) {
14+
this.isDiscarded = true;
15+
}
16+
if (this.checkOutCount > 100) {
17+
this.isDiscarded = true;
18+
}
19+
}
20+
}
321

422
// Define your Manual and Novel classes here:
23+
class Manual extends Book {
24+
constructor(title, author, copyrightDate, isbn, numOfPages, checkOutCount, isDiscarded) {
25+
super(title, author, copyrightDate, isbn, numOfPages, checkOutCount, isDiscarded);
26+
}
27+
checkout(num) {
28+
this.checkOutCount += num;
29+
}
30+
}
531

32+
class Novel extends Book {
33+
constructor(title, author, copyrightDate, isbn, numOfPages, checkOutCount, isDiscarded) {
34+
super(title, author, copyrightDate, isbn, numOfPages, checkOutCount, isDiscarded);
35+
}
36+
}
637

738
// Declare the objects for exercises 2 and 3 here:
39+
let prideAndPrej = new Novel('Pride and Prejudice', 'Jane Austen', 1813, '1111111111111', 432, 32, false);
40+
41+
let topSecretManual = new Manual('Top Secret Shuttle Building Manual', 'Redacted', 2013, '0000000000000', 1147, 1, false);
42+
43+
console.log(prideAndPrej,topSecretManual);
44+
// Code exercises 4 & 5 here:
45+
46+
topSecretManual.checkout(5);
47+
topSecretManual.dispose(2024);
848

949

10-
// Code exercises 4 & 5 here:
50+
console.log(prideAndPrej, topSecretManual);

0 commit comments

Comments
 (0)