Created
April 20, 2015 00:54
-
-
Save thiagovars/3cfe02d942e019792bed to your computer and use it in GitHub Desktop.
Arabic to Roman
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by thiago on 12/04/15. | |
*/ | |
public class Ruler { | |
String[] indexRomans = {"M", "D", "C", "L", "X", "V", "I"}; | |
int[] indexArabic = {1000, 500, 100, 50, 10, 5, 1}; | |
public String convertToRoman(int number){ | |
String result = ""; | |
int index = 0; | |
// find index | |
while (indexArabic[index] <= number) { | |
index++; | |
} | |
while (number >= indexArabic[index]) { | |
result += indexRomans[index]; | |
number -= indexArabic[index]; | |
} | |
return result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import static org.junit.Assert.assertEquals; | |
public class RulerTest { | |
@Test | |
public void TestRetornaIVQuandoRecebe4(){ | |
Ruler arabic = new Ruler(); | |
String roman = arabic.convertToRoman(4); | |
assertEquals("IV", roman); | |
} | |
@Test | |
public void TestRetornaIVQuandoRecebe5(){ | |
Ruler arabic = new Ruler(); | |
String roman = arabic.convertToRoman(5); | |
assertEquals("V", roman); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment