Exercise: Composition Over Inheritance
This exercise puts the composition over inheritance lesson into code.
Scenario
Manage a fleet of cars, each powered by either a gas or electric engine. A car holds an
Engine object rather than extending a per-engine subclass. Because the engine is a field,
it can be replaced at runtime with a swap command. Each command produces exactly one
output line.
Commands
The program reads one command per line and prints one line of output per command.
| Command | Behavior | Output |
|---|---|---|
["build", name, engine] | Create a car named name with engine type gas or electric | "Built <name> with <engine> engine" |
["start", name] | Start the named car; sound depends on engine type | "<name>: Vroom" (gas) or "<name>: Silent hum" (electric), or "<name> not built" if the car does not exist |
["swap", name, engine] | Replace the car's engine with a new one of the given type | "<name> now has <engine> engine", or "<name> not built" if the car does not exist |
Example
Input
6 build Falcon gas build Nova electric start Falcon start Nova swap Falcon electric start Falcon
Output
Built Falcon with gas engine Built Nova with electric engine Falcon: Vroom Nova: Silent hum Falcon now has electric engine Falcon: Silent hum
Explanation
Two cars are built — Falcon with a gas engine and Nova with electric. Starting Falcon produces "Vroom" (gas sound), starting Nova produces "Silent hum" (electric sound). The `swap` replaces Falcon's engine with an electric one. Starting Falcon again now produces "Silent hum", demonstrating that the engine swap takes effect without reconstructing the car.
Your task
Implement the skeleton in the editor below so the commands produce the output shown in the example above.