Skip to content

Commit f553565

Browse files
committed
Class Exercises
1 parent 2564174 commit f553565

File tree

3 files changed

+141
-5
lines changed

3 files changed

+141
-5
lines changed

classes/chapter-examples/ClassExamples01.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Astronaut {
66
this.mass = mass;
77
}
88
}
9-
9+
//let wolf = new Astronaut('Wolf', 13, 26)
1010
let fox = new Astronaut('Fox', 7, 12);
1111

1212
console.log(fox);
@@ -17,5 +17,45 @@ fox.color = 'red';
1717

1818
console.log(fox);
1919
console.log(fox.age, fox.color);
20+
//console.log(wolf)
21+
22+
//Try modifying or adding properties below.
23+
24+
let wolf = new Astronaut();
25+
wolf.name = 'Wolfie';
26+
wolf.age = 13;
27+
wolf.mass = 26;
28+
29+
30+
console.log(wolf)
31+
32+
33+
// class Car {
34+
// constructor(make, model, year, color, mpg){
35+
// this.make = make;
36+
// this.model = model;
37+
// this.year = year;
38+
// this.color = color;
39+
// this.mpg = mpg;
40+
// }
41+
// }
42+
43+
//let myCar = new Car('Chevy', 'Astro', 1985, 'gray', 20)
44+
// let myCar = new Car('Tesla', 'Model S', 2019)
45+
// console.log(myCar)
46+
// console.log(typeof myCar.year)
47+
48+
class Car {
49+
constructor(make, model, year, color, mpg){
50+
this.make = make,
51+
this.model = model,
52+
this.year = year,
53+
this.color = color,
54+
this.mpg = mpg
55+
}
56+
}
57+
//let myCar = new Car('Chevy', 'Astro', 1985, 'gray', 20)
58+
let myCar = new Car('Tesla', 'Model S', 2019)
59+
console.log(typeof myCar.year)
60+
console.log(myCar)
2061

21-
//Try modifying or adding properties below.

classes/chapter-examples/ClassExamples02.js

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,58 @@ class Astronaut {
1010
}
1111
}
1212

13-
let tortoise = new Astronaut('Speedy', 120);
1413

15-
console.log(tortoise.name, tortoise.age, tortoise.mass);
14+
// let tortoise = new Astronaut('Speedy', 120);
1615

17-
// What happens if we call Astronaut and pass in MORE than 3 arguments? TRY IT!
16+
// console.log(tortoise.name, tortoise.age, tortoise.mass);
17+
18+
// What happens if we call Astronaut and pass in MORE than 3 arguments? TRY IT!
19+
20+
// Here we assign the method inside the constructor
21+
class AstronautI {
22+
constructor(name, age, mass){
23+
this.name = name;
24+
this.age = age;
25+
this.mass = mass;
26+
this.reportStats = function() {
27+
let stats = `${this.name} is ${this.age} years old and has a mass of ${this.mass} kg.`;
28+
return stats;
29+
}
30+
}
31+
}
32+
33+
// Here we assign the method outside of the constructor
34+
class AstronautO {
35+
constructor(name, age, mass){
36+
this.name = name;
37+
this.age = age;
38+
this.mass = mass;
39+
}
40+
41+
reportStats() {
42+
let stats = `${this.name} is ${this.age} years old and has a mass of ${this.mass} kg.`;
43+
return stats;
44+
}
45+
}
46+
47+
let fox = new AstronautI('Fox', 7, 12);
48+
let hippo = new AstronautO('Hippo', 25, 1000);
49+
let bird = new Astronaut('Bird', 3, 7);
50+
51+
console.log(fox);
52+
console.log(hippo);
53+
console.log(bird);
54+
55+
class Plant {
56+
constructor(type, height) {
57+
this.type = type;
58+
this.height = height;
59+
}
60+
61+
grow() {
62+
this.height = this.height + 1;
63+
let growth = `${this.type} is ${this.height}`;
64+
return growth;
65+
}
66+
}
67+
console.log(grow)

classes/exercises/ClassExercises.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,56 @@
11
// Define your Book class here:
2+
class Book {
3+
constructor(title, author, copyright, ISBN, pages, checkedout, discarded){
4+
this.title = title;
5+
this.author = author;
6+
this.copyright = copyright;
7+
this.ISBN = ISBN;
8+
this.pages = pages;
9+
this.checkedout = checkedout;
10+
this.discarded = discarded;
11+
}
12+
checkout(uses=1){
13+
this.checkedout += uses;
14+
}
15+
16+
}
17+
class Manual extends Book {
18+
constructor(title, author, copyright, ISBN, pages, checkedout, discarded){
19+
super(title, author, copyright, ISBN, pages, checkedout, discarded)
20+
}
21+
dispose(currentYear){
22+
if (currentYear-this.copyright > 5) {
23+
this.discarded = "Yes";
24+
}
25+
}
26+
}
27+
class Novel extends Book {
28+
constructor(title, author, copyright, ISBN, pages, checkedout, discarded){
29+
super(title, author, copyright, ISBN, pages, checkedout, discarded)
30+
}
31+
dispose(checkedout){
32+
if (this.checkedout > 100){
33+
this.discarded = "yes";
34+
} return checkedout
35+
}
36+
}
237

338

439
// Define your Manual and Novel classes here:
540

641

742
// Declare the objects for exercises 2 and 3 here:
43+
let myFav = new Book('Pride and Prejudice', 'Jane Austen', 1813,
44+
'1111111111111', 432, 32, 'No')
845

46+
let buildShip = new Manual('Top Secret Shuttle Building Manual', 'Redacted', 2013,
47+
'0000000000000', 1147, 1, 'No')
948

49+
myFav.checkout(5);
50+
//myFav.dispose();
51+
console.log(myFav)
52+
53+
buildShip.checkout(100);
54+
buildShip.dispose();
55+
console.log(buildShip)
1056
// Code exercises 4 & 5 here:

0 commit comments

Comments
 (0)