Classes & Methods Homework


Homework!

As you read through this, notice the new content introduced. We encourage you to do your own research (this can be Google, ChatGPT, documentation, etc.). Here, we introduce the _ property (memory safety), extends keyword, and super() method.

To complete the homework, implement the TODO: comments. The example usage should guide you and work if all implementation is done correctly. Have fun!

If anything simply cannot go wrong, it will anyway.

%%js

class Vehicle {
    // Constructor
    constructor(model, capacity, range, terrain) {
        this._model = model; //modle of vehicle
        this._capacity = capacity; //number of people
        this._range = range; // rang on one tank of fuel
        this._terrain = terrain // air,ground,water
    }
    //gets Vehicle modle
    getModel() {
        return this._model;
    }
    //change Vehicle modle
    setModel(newModel) {
        this._model = newModel;
    }
    // get Vehicle fuel 
    getCapacity() {
        return this._capacity;
    }
    // change Vehicle fuel
    setCapacity(newCapacity) {
        this._capacity = newCapacity;
    }
    // get Vehicle range on a full tank
    getRange() {
        return this._range
    }
    //change range
    setRange(newRange) {
        this._range = newRange
    }
    //gets terrain
    getTerrain(){
        return this._terrain
    }
    //return details
    displayDetails() {
        return this
    }
    //compares ranges
    static compareRange(aircraft1, aircraft2) {
        return aircraft1.range - aircraft2.range;
    }
}

class FighterJet extends Vehicle {
    constructor(model, capacity, range, speed, terrain) {
        super(model, capacity, range, "air"); 
        this._speed = speed;
    }

    getSpeed(){
        return this._speed
    }

    setSpeed(newSpeed){
        this._speed = newSpeed
    }

    displayDetails(){
        return this
    }
}

class Boat extends Vehicle{

    constructor(model, capacity, range, speed, terrain){
        super(model, capacity, range, "water"); 
        this._speed = speed;
    }

}
class Car extends Vehicle{

    constructor(model, capacity, range, speed, terrain){
        super(model, capacity, range, "land"); 
        this._speed = speed;
    }

}

// Jets
let f16 = new FighterJet('F-16', 1, 4220, 2400);
let f22 = new FighterJet('F-22', 1, 2960, 2410);

console.log(f16.displayDetails());
console.log(f22.displayDetails());

//Boats
let MultiBoat = new Boat("Multi-Hull Power Boat", 8, 500, 60)
let SigmaBoat = new Boat("Sigma 21", 21, 50000, 6000)
console.log(MultiBoat.displayDetails())
console.log(SigmaBoat.displayDetails())

//Cars
let ford_F_150 = new Car("Ford f-150", 6, 1000, 140)
let lightning = new Car("Lightning McQueen", 1, 1000, 300)

console.log(ford_F_150.displayDetails())
console.log(lightning.displayDetails())

<IPython.core.display.Javascript object>

If you perceive that there are four possible ways in which a procedure can go wrong, and circumvent these, then a fifth way, unprepared for, will promptly develop.

You have no purpose because you fear to seek one. That fear is your failure. Your fear brings you pain. We know pain. Our purpose is its end.