OOPL1 · Easy
Encapsulation: Car – Engine – Wheel
Problem
Model a Car that owns an Engine and 4 Wheels. Practice proper encapsulation: fields should be private, state should only change through controlled methods. A car should start only if the engine is healthy, and should not allow direct mutation of its internal parts.
Requirements
- ▸Car has a private Engine and private list of Wheels
- ▸Engine has state: IDLE, RUNNING, OVERHEATED
- ▸Car.start() — fails if engine is already running or overheated
- ▸Car.accelerate(speed) — only works when engine is RUNNING
- ▸Car.stop() — transitions engine back to IDLE
- ▸Wheel has pressure (psi) — Car.checkTires() returns list of under-inflated wheels (< 30 psi)
- ▸No public setters that allow bypassing business rules
Constraints
- –Do NOT expose raw fields (no public engine, no public wheels[])
- –All state transitions must go through methods, not direct assignment
✓ Saved