Pure Programmer
Blue Matrix

Cluster Map

Project: Length Class

Write an immutable class to represent length. You will need to setup an enumeration for the different possible units of mm, cm, m, km, in, ft, yd, and mile. Pick one of the units for your internal representations and provide a private constructor that takes the length value and a unit enumeration. Then provide a static factory method that takes the same arguments and returns a length object. The factory method will store objects it creates in a map so that it can use the map to return previously cached objects that match. This helps to keep the number of objects created down. Provide toString type method that returns a reasonable representation of the length object(value plus nearest unit) in SI and one for English units.

Provide add and subtract methods that take a length object as an argument and returns the sum or difference of the calling object plus the argument object. Also provide multiply and divide methods that take a non-dimensional double argument and return a length object that is the product or quotient of the calling object and the non-dimensional double.

See See [[Unit_of_length|Unit of Length]]

$ g++ -std=c++17 LengthClass.cpp -o LengthClass -lfmt $ ./LengthClass 60.960 cm + 1.500 m = 2.110 m 2.000 ft + 1.640 yd = 2.307 yd 60.960 cm * 10000 = 6.096 km 2.000 ft * 10000 = 3.788 mi 60.960 cm / 10000 = 0.150 mm 2.000 ft / 10000 = 0.006 in