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 The name of the Class is Road.

The cityRoad variable is a(n) ________ of the Road class.

attribute parameter element instance The cityRoad variable is an instance of the Road class.

When is the constructor() method executed?

When we call 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.

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 The name of the base class is Vehicle. The other two classes extend that base Class.

The super() command does what?

Calls the version of the current method found on the base class. Makes the current method magic. Copies lines of code from the base class into the current method. Provides a placeholder for additional development. The 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?

a function a method an attribute a property The myCar.move() function is called a "method" of the Car class.

What is myCar.numWheels called in class structure terms?

a function a method a property a parameter myCar.numWheels is called a "property" of the Car class.

What is myCar.description called in class structure terms?

a function a method a dynamic property a property 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 The returned value of myPlane.fly('north') is "Vehicle FLYIN1 flies north".

What is myPlane.numPropellers equal to?

4 1 2 undefined The value of 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.

results matching ""

    No results matching ""