TypeScript Class Composition
TypeScript Classes and OOP : Exercise-4 with Solution
Write a TypeScript class that defines a base class Car with properties like make, model, and year, along with a start() method that prints a starting message. Now create a class called Engine with the properties horsepower and fuelType. Modify the Car class to include an instance of the Engine class as a property. Implement a method printCarDetails() in the Car class that prints both car and engine details.
Sample Solution:
TypeScript Code:
// Define a class 'Engine'
class Engine {
// Properties
horsepower: number;
fuelType: string;
// Constructor for Engine
constructor(horsepower: number, fuelType: string) {
this.horsepower = horsepower;
this.fuelType = fuelType;
}
}
// Modify the 'Car' class to include an 'Engine' instance
class Car {
// Properties
make: string;
model: string;
year: number;
engine: Engine; // Include an 'Engine' instance as a property
// Constructor for Car
constructor(make: string, model: string, year: number, engine: Engine) {
this.make = make;
this.model = model;
this.year = year;
this.engine = engine; // Initialize the 'engine' property with an 'Engine' instance
}
// Method to start the car
start() {
console.log(`The ${this.make} ${this.model} (Year: ${this.year}) is starting.`);
}
// Method to print car and engine details
printCarDetails() {
console.log(`Car Details:`);
console.log(`Make: ${this.make}`);
console.log(`Model: ${this.model}`);
console.log(`Year: ${this.year}`);
console.log(`Engine Details:`);
console.log(`Horsepower: ${this.engine.horsepower}`);
console.log(`Fuel Type: ${this.engine.fuelType}`);
}
}
// Create an 'Engine' object
const myEngine = new Engine(200, "Gasoline");
// Create a 'Car' object with the 'Engine' instance
const myCar = new Car("Audi", "A3", 2023, myEngine);
// Call the start method to start the car
myCar.start();
// Print car and engine details
myCar.printCarDetails();
Explanations:
In the exercise above -
- First, we define a class "Engine" with properties 'horsepower' and 'fuelType' and a constructor to initialize these properties.
- Modify the "Car" class to include an 'engine' property that represents an instance of the "Engine" class.
- The constructor for the "Car" class now takes an additional parameter, 'engine', and initializes the 'engine' property with an 'Engine' instance.
- Implement a "printCarDetails()" method in the 'Car' class that prints both car and engine details, including make, model, year, horsepower, and fuel type.
- Create an instance of the "Engine" class called 'myEngine' and use it to create a 'Car' object called 'myCar'.
- Finally call the "start()" method to start the car and then print the car and engine details using the "printCarDetails()" method.
Output:
"The Audi A3 (Year: 2023) is starting." "Car Details:" "Make: Audi" "Model: A3" "Year: 2023" "Engine Details:" "Horsepower: 200" "Fuel Type: Gasoline"
TypeScript Editor:
See the Pen TypeScript by w3resource (@w3resource) on CodePen.
Previous: TypeScript Class Inheritance.
Next: TypeScript Basic Constructor.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/typescript-exercises/typescript-class-and-oop-exercise-4.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics