Hw5_ipynb_2_
%%js
class Triangle {
// Constructor
constructor(height, width) {
this.height = height;
this.width = width;
}
// Getter
get area() {
return this.calcArea();
}
// Method
calcArea() {
return this.height * this.base;
}
// Generator
*getSides() {
yield this.height;
yield this.base;
yield this.height;
}
}
const square = new Triangle(10, 10);
console.log(square.area); // 100
console.log([...square.getSides()]); // [10, 10, 10, 10]
Uncomment this section once you have implemented the Triangle class
const triangle = new Triangle(10, 5);
console.log(triangle.calcArea()); // 25
console.log(triangle.area); // 25
console.log([...triangle.getSides()]); // [10, 5, 10]
popcorn 2
%%js
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
// Mutator method to set the name
setName(newName) {
this.name = newName; // Update the value of the 'name' property
}
// Mutator method to set the age
setAge(newAge) {
if (newAge >= 0) { // Validate the age
this.age = newAge; // Update the value of the 'age' property
} else {
console.log("Please enter a valid age.");
}
}
// Accessor method to get the name
getName() {
return this.name;
}
// Accessor method to get the age
getAge() {
return this.age;
}
}
// Creating an instance of the Person class
let person = new Person("Kian", 17);
// Using the mutator methods
person.setName("Bloodborne"); // Change the name to "bloodborne"
person.setAge(33); // Change the age to 33
console.log(person.getName()); // Output: "Bloodborne"
console.log(person.getAge()); // Output: 33
// Trying to set an invalid age
// person.setAge(-5); // Output: "Please enter a valid age."
popcorn 3
%%js
class Value {
static MULTIPLIER = 1.5; // Static variable for tax rate (8%)
static totalNumber = 0; // Static variable to track the total number of products
constructor(value) {
this.value = value; // instance property
totalNumber++
}
// Instance method to calculate the price including tax
calculateValueWithMultiplier() {
return value * MULTIPLIER
}
// Static method to get the total number of products
static getTotalNumbers() {
return totalNumber
}
}
// Creating individual product instances
const value1 = new Value(5);
const value2 = new Value(10)
console.log("Value 1: " + value1.value);
console.log("Value 2: " + value2.value);
// Using an instance method to calculate prices with tax
console.log("Value 1 with multiplier: " + value1.calculateValueWithMultiplier()); // Output: 1296 (1200 + 8% tax)
console.log("Value 2 with multiplier: " + value2.calculateValueWithMultiplier()); // Output: 864 (800 + 8% tax)
// Accessing the static variable directly from the class
console.log("Multiplier: " + Value.MULTIPLIER); // Output: 1.5
// Accessing the static method to get the total number of products
console.log("Total number of values: " + Value.getTotalNumbers()); // Output: 2
Homework
%%js
class Aircraft {
// Constructor
constructor(model, capacity, range) {
this._model = model; // _model is a convention to indicate that it's a private property
this._capacity = capacity; // _capacity is a convention to indicate that it's a private property
this._range = range; // _range is a convention to indicate that it's a private property
}
// Getter for model
get model() {
return this._model;
}
// Setter for model
set model(newModel) {
this._model = newModel;
}
// Getter for capacity
get capacity() {
return this._capacity;
}
// Setter for capacity
set capacity(newCapacity) {
this._capacity = newCapacity;
}
// Getter for range
get range() {
return this._range;
}
// Setter for range
set range(newRange) {
this._range=newRange;
}
// Method to display aircraft details
displayDetails() {
yield (model, capacity, range)
}
// Static method to compare range
static compareRange(aircraft1, aircraft2) {
return aircraft1.range - aircraft2.range;
}
}
// Example usage
let boeing = new Aircraft('Boeing 747', 416, 13800);
let airbus = new Aircraft('Airbus A380', 853, 15700);
console.log(boeing.displayDetails());
console.log(airbus.displayDetails());
console.log(`Range difference: ${Aircraft.compareRange(boeing, airbus)} km`);
// New content in the HW! Lo and behold, the FighterJet class!
// For 1) do research. What does the extend keyword do in JavaScript?
// Here's a quick rundown if you're in a hurry:
// the extends keyword creates a child class (FighterJet) that inherits from a parent class (Aircraft).
class FighterJet extends Aircraft {
constructor(model, capacity, range, speed) {
super(model, capacity, range); // We call the parent class constructor with super
this._speed = speed;
}
get speed(){
return this._speed;
}
// TODO: Getter for speed
// Remember to use the _speed property for memory safety
set speed(newSpeed){
this._speed = newSpeed;
}
// TODO: Setter for speed
// Remember to use the _speed property and add a function paramater.
// TODO: Method to display fighter jet details (model, capacity, range, speed)
}
// Example usage
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());
console.log(`Range difference: ${Aircraft.compareRange(f16, f22)} km`);