Roman Numerals work differently than our normal Arabic number system. Roman Numerals have symbols, all in capital letters (and sometimes in lower case), which represent Arabic numbers. Roman Numerals have been used for identifying movie sequels (i.e., The Godfather: Part II), for publication copyright dates, for numbering monarchs such as Queen Elizabeth II, and for numbering Super Bowls. See the following table for the Roman Numerals symbols up to 1000.
Roman Numeral | Arabic Number |
---|---|
I or i | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
Usually, numbers are formed by stringing the Roman numerals together and adding them up to make the required number (i.e., II = 2, or XII = 12). If smaller numbers follow larger numbers, the numbers are added (i.e., VIII = 5 + 3 or 8), but if a smaller number precedes a larger number, the smaller number is subtracted from the larger number (i.e., IX = 10 - 1 or 9).
There is shorthand for the case when there are four of the same symbols in a row. Instead of IIII for 4, it is written as IV or 5 - 1 = 4. This only applies to symbols that represent powers of ten. Since our numbers will be less than 4000, this only makes sense for I, X and C. Some people think this means you can write IC for 99 but that is not going to be allowed. When using this shortcut, a symbol can only precede a symbol whose value is 5 or 10 times its own value. For example, X (10) can only precede L (50) or C (100). So XL (40) is acceptable, but XD (490?) is not.
Roman Numeral | Arabic Number |
---|---|
XLVI | 46 |
XCIX | 99 |
MDCCCXIX | 1819 |
DCXLIX | 649 |
MCMLXXXIII | 1983 |
For practice converting to and from Roman numerals, take the Roman Numeral Challenge. For more information about Roman numerals, search the internet or read Modern use of Roman numerals.
Write a class that includes a method to convert Roman numbers into an Arabic numbers and vice versa. Use the method template below and do not change the method names, parameter, or return type. One method should accept a Roman number in the form of a String
and return the int
value of the Roman number. The other method should accept an Arabic number in the form of an int
and return the Roman number as a String
public static String arabicToRoman(int x) {
// Your code here
}
public static int romanToArabic(String x) {
// Your code here
}
Your methods should be static
because there is no reason to create an object just to do the conversion.
You are very highly encouraged to use helper methods to simplify your code and reuse algorithms needed to solve this problem.
Assume that your client gives you valid Roman Numerals that are less than or equal to 4000.
Use these numbers as your test input. Convert them to Arabic and then back to Roman.
I
IV
LXIII
LXIV
DCCXLV
MCMLXXIII
MMMDXCIII
All files must incude your name and period in the format
PX_LastName_FirstName_Title
You must Sign In to submit to this assignment
Last modified: December 12, 2022
Back to Lab 10.4 Carrental