Skip to content

Commit c5e99f9

Browse files
committed
exercises
1 parent 78ec6fb commit c5e99f9

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

classes/chapter-examples/ClassExamples02.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Next, set default values for 1 or more of the parameters in constructor.
44

55
class Astronaut {
6-
constructor(name, age, mass){
6+
constructor(name, age, mass = 54){
77
this.name = name;
88
this.age = age;
99
this.mass = mass;
@@ -12,6 +12,8 @@ class Astronaut {
1212

1313
let tortoise = new Astronaut('Speedy', 120);
1414

15-
console.log(tortoise.name, tortoise.age, tortoise.mass);
15+
let mrMouse = new Astronaut('Gonzalez', 15, .0001);
16+
17+
console.log(tortoise, mrMouse);
1618

1719
// What happens if we call Astronaut and pass in MORE than 3 arguments? TRY IT!

classes/exercises/ClassExercises.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,48 @@
11
// Define your Book class here:
2-
3-
2+
class Book {
3+
constructor(title, author, copyright, isbn, pages, timesCheckedOut, discarded){
4+
this.title = title;
5+
this.author = author;
6+
this.copyright = copyright;
7+
this.isbn = isbn;
8+
this.pages = pages;
9+
this.timesCheckedOut = timesCheckedOut;
10+
this.discarded = discarded;
11+
}
12+
13+
checkout(uses=1) {
14+
this.timesCheckedOut += uses;
15+
}
16+
}
417
// Define your Manual and Novel classes here:
518

6-
19+
class Manual extends Book {
20+
constructor(title, author, copyright, isbn, pages, timesCheckedOut, discarded){
21+
super(title, author, copyright, isbn, pages, timesCheckedOut, discarded);
22+
}
23+
24+
dispose(currentYear){
25+
if (currentYear-this.copyright > 5) {
26+
this.discarded = 'Yes';
27+
}
28+
}
29+
}
30+
31+
class Novel extends Book {
32+
constructor(title, author, copyright, isbn, pages, timesCheckedOut, discarded){
33+
super(title, author, copyright, isbn, pages, timesCheckedOut, discarded);
34+
}
35+
36+
dispose(){
37+
if (this.timesCheckedOut > 100) {
38+
this.discarded = 'Yes';
39+
}
40+
}
41+
}
742
// Declare the objects for exercises 2 and 3 here:
43+
let englishBook = new Novel("Pride and Prejudice", "Jane Austen", 1813, 1111111111111, 432, 32, "No");
44+
let buildingManual = new Manual("Top Secret Shuttle Building Manual", "Redacted", 2013, 0000000000000, 1147, 1, "No");
845

9-
10-
// Code exercises 4 & 5 here:
46+
// Code exercises 4 & 5 here:
47+
goodRead.checkout(5);
48+
goodRead.dispose(2023);

0 commit comments

Comments
 (0)