Sometimes the class you need doesn't fit the code that should use it. Its methods have other names, or they work in other units. Often it comes from a library you can't change. The Adapter pattern puts a small class in between. It has the methods your code expects, and it passes each call on to the other class, translated.
Here you write the sensors of a robot. The robot asks each sensor how far the nearest obstacle is.
The trait Sensor has one method, distanceMm(). It returns that distance in millimetres (mm). A trait lists the methods a class promises to have.
Write three classes:
new LaserSensor(mm)extendsSensor. Itsset(mm)changes the distance it measures. ItsdistanceMm()returns the current one.new SonarAdapter(device)extendsSensortoo. It wraps aSonarDevicefrom the library below. Wrapping means it keeps the device in a field and calls it. Each call of itsdistanceMm()reads the device again. It turns tenths of an inch into mm withtenths * 254 / 100, rounded down.new Robot()keeps its sensors. Itsadd(sensor)adds one. Itsclosest()returns the smallestdistanceMm()of all its sensors. With no sensors, it returns -1.
The sonar's library is already in the code, and you must not change it. Its class SonarDevice has three methods:
new SonarDevice(tenths)makes a sonar that readstenths.update(tenths)changes the reading.readTenths()returns the reading, in tenths of an inch.
The tests make the lasers and the sonar devices themselves. They wrap each device in a SonarAdapter and give every sensor to add. Later they change readings on their own objects. They call set on a laser, or update on a device. So the adapter must read its device at every call.
Each example runs commands on a new robot, one per line:
laser mmmakes a laser that measuresmmand adds it. It writes the sensor's number: 1 for the first, then 2, 3 and so on.sonar tenthsmakes a device that readstenths, wraps it in an adapter and adds it. It writes the sensor's number too.set i valuegives sensoria new reading. For a laser,valueis in mm. For a sonar, it is in tenths of an inch.read iwrites sensori'sdistanceMm().closestwrites the robot'sclosest().
Example 1:
Input: 1. closest 2. laser 500 3. sonar 300 4. read 2 5. closest 6. set 2 150 7. read 2 8. closest 9. set 1 200 10. closest Output: -1 1 2 762 500 381 381 200 Explanation: Command 1 finds no sensor, so it writes -1. Sensor 1 is a laser at 500 mm. Sensor 2 is a sonar that reads 300 tenths. That is 300 * 254 / 100 = 762 mm. So the laser is the closest, at 500 mm. Command 6 changes the reading of the sonar's device to 150 tenths. The adapter reads the device again and gets 381 mm. Now the sonar is the closest. Command 9 moves the laser to 200 mm, so it is the closest again.
Example 2:
Input: 1. sonar 7 2. read 1 3. laser 18 4. closest 5. set 1 8 6. read 1 7. closest Output: 1 17 2 17 20 18 Explanation: The sonar reads 7 tenths, and 7 * 254 / 100 is 17.78. Rounded down, that is 17 mm. The laser at 18 mm is farther, so the closest is 17. Command 5 sets the device to 8 tenths. That is 20.32 mm, rounded down to 20. Now the laser at 18 mm is the closest.
Constraints:
mmis a whole number from 0 to 1,000,000.tenthsis a whole number from 0 to 1,000,000.valueis a whole number from 0 to 1,000,000.- A robot has at most 20 sensors.
iis always the number of a sensor already added.- There are at most 100,000 commands in all.
Note:
Each call should run in O(1) time. A robot has at most 20 sensors, so closest() is O(1) too. The largest tenths * 254 is 254,000,000, which fits in a 32-bit integer.
Free plan: 30 tutor messages. PRO is unlimited.
