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?
The cityRoad
variable is a(n) ________ of the Road
class.
When is the constructor()
method executed?
What is cityRoad.numLanes
equal to?
What is countryRoad.sidewalk
equal to?
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?
The super()
command does what?
What is myCar.move()
called in class structure terms?
What is myCar.numWheels
called in class structure terms?
What is myCar.description
called in class structure terms?
What does myPlane.fly('north')
return?
What is myPlane.numPropellers
equal to?
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.