1.
Завдання 1: Наслідування Shape
10 XP
Є базовий клас:
class Shape {
constructor(color='black') { this.color = color; }
toString() { return `${this.color} фігура`; }
}
Створи клас Circle extends Shape:
- constructor(radius, color)
- метод area() → π * r²
- метод perimeter() → 2 * π * r
- toString() → 'Коло: радіус 5, площа 78.54'
Виведи toString() для Circle(5, 'red').
Вхідні дані (stdin):
▶ Запустити
💡 Підказка
🔓 Розв'язок
💡 Підказка: super(color) в constructor. Округли area() до 2 знаків через toFixed(2)
🔓 Розв'язок:
class Shape {
constructor(color = 'black') { this.color = color; }
toString() { return `${this.color} фігура`; }
}
class Circle extends Shape {
constructor(radius, color) {
super(color);
this.radius = radius;
}
area() { return Math.PI * this.radius ** 2; }
perimeter() { return 2 * Math.PI * this.radius; }
toString() { return `Коло: радіус ${this.radius}, площа ${this.area().toFixed(2)}`; }
}
console.log(new Circle(5, 'red').toString());
2.
Завдання 2: instanceof та поліморфізм
20 XP
Використовуючи клас Shape та Circle з попереднього завдання, додай клас Rectangle extends Shape:
- constructor(width, height, color)
- area() та perimeter()
Створи масив фігур і виведи area() кожної.
Вхідні дані (stdin):
▶ Запустити
💡 Підказка
🔓 Розв'язок
💡 Підказка: const shapes = [new Circle(3), new Rectangle(4, 5)]; shapes.forEach(s => console.log(s.area().toFixed(2)))
🔓 Розв'язок:
class Shape {
constructor(color = 'black') { this.color = color; }
}
class Circle extends Shape {
constructor(r, c) { super(c); this.radius = r; }
area() { return Math.PI * this.radius ** 2; }
}
class Rectangle extends Shape {
constructor(w, h, c) { super(c); this.width = w; this.height = h; }
area() { return this.width * this.height; }
}
const shapes = [new Circle(3), new Rectangle(4, 5)];
shapes.forEach(s => console.log(s.area().toFixed(2)));
3.
Завдання 3: Ієрархія Employee
30 XP
Реалізуй:
class Employee { constructor(name, salary) }
метод getAnnualSalary() → salary * 12
toString() → 'Іван: 50000 грн/міс'
class Manager extends Employee { constructor(name, salary, teamSize) }
getAnnualSalary() → базова + teamSize * 1000 * 12 (бонус за кожного в команді)
toString() → 'Менеджер Іван (команда: 5): 55000 грн/міс'
Виведи toString() та getAnnualSalary() для обох.
Вхідні дані (stdin):
▶ Запустити
💡 Підказка
🔓 Розв'язок
💡 Підказка: super(name, salary) в Manager. getAnnualSalary() перевизначає батьківський через super.getAnnualSalary() + бонус
🔓 Розв'язок:
class Employee {
constructor(name, salary) { this.name = name; this.salary = salary; }
getAnnualSalary() { return this.salary * 12; }
toString() { return `${this.name}: ${this.salary} грн/міс`; }
}
class Manager extends Employee {
constructor(name, salary, teamSize) { super(name, salary); this.teamSize = teamSize; }
getAnnualSalary() { return super.getAnnualSalary() + this.teamSize * 1000 * 12; }
toString() { return `Менеджер ${this.name} (команда: ${this.teamSize}): ${this.salary + this.teamSize * 1000} грн/міс`; }
}
const e = new Employee('Іван', 50000);
console.log(e.toString());
console.log(e.getAnnualSalary());
const m = new Manager('Іван', 50000, 5);
console.log(m.toString());
console.log(m.getAnnualSalary());