Remember: Learning to program takes practice! It helps to see concepts over and over, and it's always good to try things more than once. We learn much more the second time we do something. Use the exercises and additional self-checks below to practice.
Review the code, then answer the questions below.
class Road {
constructor(directions=['east','west'],sidewalk=true){
this.directions = directions;
this.numLanes = directions.length;
this.sidewalk = sidewalk;
}
}
let cityRoad = new Road(['north','north','south','south'], true);
let countryRoad = new Road(['east','west'], false);
What is the name of the Class?
class
constructor
Road
road
The name of the Class is Road
.
The cityRoad
variable is a(n) ________ of the Road
class.
cityRoad
variable is an instance of the Road
class.
When is the constructor()
method executed?
Road.constructor()
.
When we create an instance of the Road
class using the new Road()
command.
When we call Road()
anywhere in our code.
When the interpreter reads the Road
Class definition.
The constructor()
method is executed when we create a new instance of the Road
class using the new Road()
command.
What is cityRoad.numLanes
equal to?
2
4
3
0
The value of cityRoad.numLanes
is 4
.
What is countryRoad.sidewalk
equal to?
true
false
undefined
The value of countryRoad.sidewalk
is false
.
Review the code, then answer the questions below.
class Vehicle {
constructor(license='ES64EVA'){
this.license = license;
}
move(direction){
console.log(`Vehicle ${this.license} moves ${direction}`);
}
get description(){
return `Vehicle ${this.license} is a shapeless carriage.`;
}
}
class Car extends Vehicle {
constructor(license, numAxles=2, numWheels=4){
super(license);
this.numAxles = numAxles;
this.numWheels = numWheels;
}
move(direction){
return `Vehicle ${this.license} rolls ${direction}`;
}
get description(){
return `Vehicle ${this.license} is a car with ${this.numAxles} axles and ${this.numWheels} wheels.`;
}
}
class Airplane extends Vehicle {
constructor(license, numWings=2, numPropellers=1){
super(license);
this.numWings = numWings;
this.numPropellers = numPropellers;
}
move(direction){
return `Vehicle ${this.license} flies ${direction}`;
}
get description(){
return `Vehicle ${this.license} is an airplane with ${this.numWings} wings and ${this.numPropellers} propellers.`;
}
}
let myCar = new Car('HOTWLZ1');
let myPlane = new Airplane('FLYIN1', 4);
What is the name of the base class that does not extend any other classes?
Airplane
Car
Vehicle
Road
The name of the base class is Vehicle
. The other two classes extend that base Class.
The super()
command does what?
super()
command calls the version of the current method that exists on the base class, executing all of the code contained in the base class method.
What is myCar.move()
called in class structure terms?
myCar.move()
function is called a "method" of the Car class.
What is myCar.numWheels
called in class structure terms?
myCar.numWheels
is called a "property" of the Car class.
What is myCar.description
called in class structure terms?
myCar.description
is called a "dynamic property" of the Car class because it executes a function to determine its value.
<question>
<p>What does <code>myPlane.fly('north')</code> return?</p>
<answer correct><code>"Vehicle FLYIN1 flies north"</code></answer>
<answer><code>"Vehicle HOTWLZ1 rolls north"</code></answer>
<answer><code>"Vehicle FLYIN1 moves north"</code></answer>
<answer><code>undefined</code></answer>
<explanation>The returned value of <code>myPlane.fly('north')</code> is <code>"Vehicle FLYIN1 flies north"</code>.</explanation>
</question>
<question>
<p>What is <code>myPlane.numPropellers</code> equal to?</p>
<answer><code>4</code></answer>
<answer correct><code>1</code></answer>
<answer><code>2</code></answer>
<answer><code>undefined</code></answer>
<explanation>The value of <code>myPlane.numPropellers</code> is <code>1</code> because that is the default value and no other value was supplied when the <code>myPlane</code> instance was created.</explanation>
</question>