-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOP-practice.js
More file actions
57 lines (52 loc) · 1.96 KB
/
OOP-practice.js
File metadata and controls
57 lines (52 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//Write a JavaScript program to create a class called "Person" with properties for name, age and country. Include a method to display the person's details. Create two instances of the 'Person' class and display their details.
class Person {
constructor(name, age, country) {
this.name = name;
this.age = age;
this.country = country;
}
displayDetails() {
return this
}
}
let person1 = new Person("Akpan", 21, "Nigeria");
let person2 = new Person("Nath", 19, "Nigeria");
//console.log(person1.displayDetails());
//console.log(person2.displayDetails());
//Write a JavaScript program to create a class called 'Rectangle' with properties for width and height. Include two methods to calculate rectangle area and perimeter. Create an instance of the 'Rectangle' class and calculate its area and perimeter.
class Rectangle{
constructor(width, height){
this.width=width;
this.height=height
}
calcArea(){
return this.width * this.height
}
}
let newRectangle = new Rectangle(40, 10)
console.log(newRectangle.calcArea());
//Write a JavaScript program that creates a class called 'Vehicle' with properties for make, model, and year. Include a method to display vehicle details. Create a subclass called 'Car' that inherits from the 'Vehicle' class and includes an additional property for the number of doors. Override the display method to include the number of doors.
class Vehicle{
constructor(make, model, year){
this.make = make
this.year=year
this.model = model
}
displayDetails(){
return this
}
}
class Car extends Vehicle{
constructor(noOfDoors){
super(make, model, year)
this.noOfDoors = noOfDoors
}
displayDetails(){
super.displayDetails();
console.log("Doors:", this.noOfDoors)
}
}
const vehicle = new Vehicle('benz', 'GLK-350', 2019)
console.log(vehicle.displayDetails())
const car = new Car('Honda', "F-1250", 2020, 4)
console.log(car.displayDetails());