Skip to content

Commit dbdb3dd

Browse files
Merge pull request #973 from suhascv/example7
Added Car Instances example |Prof Harris |Open@RIT
2 parents d0f2b12 + c04d9d3 commit dbdb3dd

File tree

4 files changed

+328
-0
lines changed

4 files changed

+328
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* @name Car Instances
3+
* @frame 400,400
4+
* @description contributed by <a href="https://www.rit.edu/directory/wmhics-w-michelle-harris">
5+
<b>Prof WM Harris,</b></a> <b>How </b>to create three instances of Car Class and
6+
invoke class methods.<br/>
7+
A function is created for the canvas setup, and
8+
3 car instances are initialized with different colors and canvas
9+
positions. The speed of each car is set by passing value to the
10+
instance’s start method. A second function calls class methods to
11+
display and move the cars.
12+
*/
13+
class Car {
14+
/* Constructor expects parameters for
15+
fill color, x and y coordinates that
16+
will be used to initialize class properties.
17+
*/
18+
constructor(cColor, x, y) {
19+
this.color = cColor;
20+
this.doors = 4;
21+
this.isConvertible = false;
22+
this.x = x;
23+
this.y = y;
24+
this.speed = 0;
25+
}
26+
27+
start(speed) { // method expects parameter!
28+
this.speed = speed;
29+
}
30+
31+
display() { // method!
32+
fill(this.color);
33+
rect(this.x, this.y, 20, 10);
34+
}
35+
36+
move() { // method!
37+
this.x += this.speed;
38+
// Wrap x around boundaries
39+
if (this.x < -20) {
40+
this.x = width;
41+
} else if (this.x > width) {
42+
this.x = -20;
43+
}
44+
}
45+
} //end class Car
46+
47+
let rav4;
48+
let charger;
49+
let nova;
50+
51+
function setup() {
52+
createCanvas(200, 400);
53+
/* Construct the 3 Cars */
54+
//constructor expects cColor, x, y
55+
rav4 = new Car("silver", 100, 300);
56+
charger = new Car("gold", 0, 200);
57+
nova = new Car("blue", 200, 100);
58+
nova.doors = 2; //update nova's doors property
59+
60+
console.log("rav4", rav4);
61+
console.log("charger", charger);
62+
console.log("nova", nova);
63+
64+
//call start methods of Car instances
65+
//the start method expects a number for speed
66+
rav4.start(2.3);
67+
charger.start(-4);
68+
nova.start(random(-1, 1));
69+
}
70+
71+
function draw() {
72+
background("beige");
73+
74+
//display and move all 3 Cars
75+
rav4.display();
76+
charger.display();
77+
nova.display();
78+
79+
rav4.move();
80+
charger.move();
81+
nova.move();
82+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* @name Car Instances
3+
* @frame 400,400
4+
* @description contributed by <a href="https://www.rit.edu/directory/wmhics-w-michelle-harris">
5+
<b>Prof WM Harris,</b></a> <b>How </b>to create three instances of Car Class and
6+
invoke class methods.<br/>
7+
A function is created for the canvas setup, and
8+
3 car instances are initialized with different colors and canvas
9+
positions. The speed of each car is set by passing value to the
10+
instance’s start method. A second function calls class methods to
11+
display and move the cars.
12+
*/
13+
class Car {
14+
/* Constructor expects parameters for
15+
fill color, x and y coordinates that
16+
will be used to initialize class properties.
17+
*/
18+
constructor(cColor, x, y) {
19+
this.color = cColor;
20+
this.doors = 4;
21+
this.isConvertible = false;
22+
this.x = x;
23+
this.y = y;
24+
this.speed = 0;
25+
}
26+
27+
start(speed) { // method expects parameter!
28+
this.speed = speed;
29+
}
30+
31+
display() { // method!
32+
fill(this.color);
33+
rect(this.x, this.y, 20, 10);
34+
}
35+
36+
move() { // method!
37+
this.x += this.speed;
38+
// Wrap x around boundaries
39+
if (this.x < -20) {
40+
this.x = width;
41+
} else if (this.x > width) {
42+
this.x = -20;
43+
}
44+
}
45+
} //end class Car
46+
47+
let rav4;
48+
let charger;
49+
let nova;
50+
51+
function setup() {
52+
createCanvas(200, 400);
53+
/* Construct the 3 Cars */
54+
//constructor expects cColor, x, y
55+
rav4 = new Car("silver", 100, 300);
56+
charger = new Car("gold", 0, 200);
57+
nova = new Car("blue", 200, 100);
58+
nova.doors = 2; //update nova's doors property
59+
60+
console.log("rav4", rav4);
61+
console.log("charger", charger);
62+
console.log("nova", nova);
63+
64+
//call start methods of Car instances
65+
//the start method expects a number for speed
66+
rav4.start(2.3);
67+
charger.start(-4);
68+
nova.start(random(-1, 1));
69+
}
70+
71+
function draw() {
72+
background("beige");
73+
74+
//display and move all 3 Cars
75+
rav4.display();
76+
charger.display();
77+
nova.display();
78+
79+
rav4.move();
80+
charger.move();
81+
nova.move();
82+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* @name Car Instances
3+
* @frame 400,400
4+
* @description contributed by <a href="https://www.rit.edu/directory/wmhics-w-michelle-harris">
5+
<b>Prof WM Harris,</b></a> <b>How </b>to create three instances of Car Class and
6+
invoke class methods.<br/>
7+
A function is created for the canvas setup, and
8+
3 car instances are initialized with different colors and canvas
9+
positions. The speed of each car is set by passing value to the
10+
instance’s start method. A second function calls class methods to
11+
display and move the cars.
12+
*/
13+
class Car {
14+
/* Constructor expects parameters for
15+
fill color, x and y coordinates that
16+
will be used to initialize class properties.
17+
*/
18+
constructor(cColor, x, y) {
19+
this.color = cColor;
20+
this.doors = 4;
21+
this.isConvertible = false;
22+
this.x = x;
23+
this.y = y;
24+
this.speed = 0;
25+
}
26+
27+
start(speed) { // method expects parameter!
28+
this.speed = speed;
29+
}
30+
31+
display() { // method!
32+
fill(this.color);
33+
rect(this.x, this.y, 20, 10);
34+
}
35+
36+
move() { // method!
37+
this.x += this.speed;
38+
// Wrap x around boundaries
39+
if (this.x < -20) {
40+
this.x = width;
41+
} else if (this.x > width) {
42+
this.x = -20;
43+
}
44+
}
45+
} //end class Car
46+
47+
let rav4;
48+
let charger;
49+
let nova;
50+
51+
function setup() {
52+
createCanvas(200, 400);
53+
/* Construct the 3 Cars */
54+
//constructor expects cColor, x, y
55+
rav4 = new Car("silver", 100, 300);
56+
charger = new Car("gold", 0, 200);
57+
nova = new Car("blue", 200, 100);
58+
nova.doors = 2; //update nova's doors property
59+
60+
console.log("rav4", rav4);
61+
console.log("charger", charger);
62+
console.log("nova", nova);
63+
64+
//call start methods of Car instances
65+
//the start method expects a number for speed
66+
rav4.start(2.3);
67+
charger.start(-4);
68+
nova.start(random(-1, 1));
69+
}
70+
71+
function draw() {
72+
background("beige");
73+
74+
//display and move all 3 Cars
75+
rav4.display();
76+
charger.display();
77+
nova.display();
78+
79+
rav4.move();
80+
charger.move();
81+
nova.move();
82+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* @name Car Instances
3+
* @frame 400,400
4+
* @description contributed by <a href="https://www.rit.edu/directory/wmhics-w-michelle-harris">
5+
<b>Prof WM Harris,</b></a> <b>How </b>to create three instances of Car Class and
6+
invoke class methods.<br/>
7+
A function is created for the canvas setup, and
8+
3 car instances are initialized with different colors and canvas
9+
positions. The speed of each car is set by passing value to the
10+
instance’s start method. A second function calls class methods to
11+
display and move the cars.
12+
*/
13+
class Car {
14+
/* Constructor expects parameters for
15+
fill color, x and y coordinates that
16+
will be used to initialize class properties.
17+
*/
18+
constructor(cColor, x, y) {
19+
this.color = cColor;
20+
this.doors = 4;
21+
this.isConvertible = false;
22+
this.x = x;
23+
this.y = y;
24+
this.speed = 0;
25+
}
26+
27+
start(speed) { // method expects parameter!
28+
this.speed = speed;
29+
}
30+
31+
display() { // method!
32+
fill(this.color);
33+
rect(this.x, this.y, 20, 10);
34+
}
35+
36+
move() { // method!
37+
this.x += this.speed;
38+
// Wrap x around boundaries
39+
if (this.x < -20) {
40+
this.x = width;
41+
} else if (this.x > width) {
42+
this.x = -20;
43+
}
44+
}
45+
} //end class Car
46+
47+
let rav4;
48+
let charger;
49+
let nova;
50+
51+
function setup() {
52+
createCanvas(200, 400);
53+
/* Construct the 3 Cars */
54+
//constructor expects cColor, x, y
55+
rav4 = new Car("silver", 100, 300);
56+
charger = new Car("gold", 0, 200);
57+
nova = new Car("blue", 200, 100);
58+
nova.doors = 2; //update nova's doors property
59+
60+
console.log("rav4", rav4);
61+
console.log("charger", charger);
62+
console.log("nova", nova);
63+
64+
//call start methods of Car instances
65+
//the start method expects a number for speed
66+
rav4.start(2.3);
67+
charger.start(-4);
68+
nova.start(random(-1, 1));
69+
}
70+
71+
function draw() {
72+
background("beige");
73+
74+
//display and move all 3 Cars
75+
rav4.display();
76+
charger.display();
77+
nova.display();
78+
79+
rav4.move();
80+
charger.move();
81+
nova.move();
82+
}

0 commit comments

Comments
 (0)