Exercise: Value objects for measures of length - 5

We want to work with length measurements in both centimeters and meters. If you are not familiar with the metric system: 1 meter has 100 centimeters. Represent centimeters as an integer and meters as a floating point number. Of course, the Value Object design pattern should be used.

Implement the following requirements step by step and only program exactly the code required for each requirement. It is best to work test-driven, but you should definitely write automated tests.

  1. Where appropriate, we need the methods asFloat() and asInt().

  2. We have decided that negative dimensions should not be possible.

  3. Enable the conversion of centimeters to meters.

  4. Also allow the conversion from meters to centimeters.

  5. Write an equals() method to compare two length measurements with each other.

Solution