Extra Practice
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.
1. Creating Classes and Class Instances
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
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()
.Road
class using the new Road()
command.Road()
anywhere in our code.Road
Class definition.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
cityRoad.numLanes
is 4
.What is countryRoad.sidewalk
equal to?
true
false
undefined
countryRoad.sidewalk
is false
.2. Extending Classes
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
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.What does myPlane.fly('north')
return?
"Vehicle FLYIN1 flies north"
"Vehicle HOTWLZ1 rolls north"
"Vehicle FLYIN1 moves north"
undefined
myPlane.fly('north')
is "Vehicle FLYIN1 flies north"
.What is myPlane.numPropellers
equal to?
4
1
2
undefined
myPlane.numPropellers
is 1
because that is the default value and no other value was supplied when the myPlane
instance was created.Visit Content Online
The content on this page has been removed from your PDF or ebook format. You may view the content by visiting this book online.