Pure Programmer
Blue Matrix


Cluster Map

Project: Duration Class

Write a mutable class to represent a time duration. Your class should represent time durations in the following units: ns, µs, ms, s, min, hour, day, week, month, year. Use an enumeration to represent the time duration units. You should have a default constructor that sets the internal value to 0. Write a setter that takes a value and an enumeration value for units. Include a getter that takes an enumeration value for units and returns the value in that measurement unit.

Output
$ javac -Xlint DurationClass.java $ java -ea DurationClass 1.23000e+09 ns 1.23000e+06 µs 1230.00 ms 1.23000 sec 0.0205000 min 0.000341667 hour 1.42361e-05 day 2.03373e-06 week 4.67716e-07 month 3.89763e-08 year 3.15576e+16 ns 3.15576e+13 µs 3.15576e+10 ms 3.15576e+07 sec 525960 min 8766.00 hour 365.250 day 52.1786 week 12.0000 month 1.00000 year 1.00000 ns 0.00100000 µs 1.00000e-06 ms 1.00000e-09 sec 1.66667e-11 min 2.77778e-13 hour 1.15741e-14 day 1.65344e-15 week 3.80257e-16 month 3.16881e-17 year

Solution