Task

Your task is to implement the method fromRoman in the class FromRoman.

Specification

Convert the given romanValue in the method fromRoman into an integer.

The romanValue must be converted as long as it is between 1 (I) and 3999 (MMMCMXCIX). Values outside this limits may cause an IllegalArgumentException.

The romanValue may only contain the upper case characters M, D, C, L, X, V, I [Wikipedia]. Any other character may cause an IllegalArgumentException.

The characters have the following values:

  • I -> 1
  • V -> 5
  • X -> 10
  • L -> 50
  • C -> 100
  • D -> 500
  • M -> 1000

The characters are interpreted in an additive order. The characters CCLXXI are representing the value 271.

The only exceptions to the additive order are that

  • the character I may appear before V or X and is subtracted then.
  • the character X may appear before L or C and is subtracted then.
  • the character C may appear before D or M and is subtracted then.

This means that e.g IX is interpreted as 9 and CD is interpreted as 400.

Examples

Some examples of conversions:

  • I -> 1
  • IV -> 4
  • IX -> 9
  • XLIX -> 49
  • XCIX -> 99
  • CMXCIX -> 999
  • MCMXCIX -> 1999
  • MMXV -> 2015

Support

To support you with implementing the method there are unit tests in FromRomanTest. The unit tests are disabled at the start. To use them you have to enable them.

Further Challenges

The reverse exercise can be found in To Roman.

Development Requirements

To perform this exercise you need a JDK 8 and Maven 3.3.x.

References