Facebook Pixel

Exercise: Adapter

This exercise puts the Adapter pattern into code.

Scenario

A heating system expects a thermostat that reports temperature in Fahrenheit. The only sensor available is a CelsiusSensor that exposes a celsius() method returning an integer. Your task is to write a CelsiusToFahrenheitAdapter that implements a FahrenheitThermometer interface, so the driver calls fahrenheit() without ever knowing a Celsius sensor is underneath.

Commands

CommandBehaviorOutput
["reading", celsius]Set the sensor to celsius and return the Fahrenheit equivalent"<f>F"

The conversion formula uses integer division: f = c * 9 // 5 + 32. The result is always a whole number — a reading of 37°C gives 98F, not 98.6°F.

Example
Input
5
reading 0
reading 100
reading -40
reading 20
reading 37
Output
32F
212F
-40F
68F
98F
Explanation
Each `reading` command updates the sensor value and immediately asks the adapter for the Fahrenheit equivalent. 0 °C converts to 32 °F (the freezing point), 100 °C to 212 °F (boiling), and −40 °C to −40 °F, which is the point where the two scales coincide — a useful edge case to verify the formula handles negative inputs correctly.

Your task

Implement the skeleton in the editor below so the commands produce the output described above.

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro