JAVA ROMAN NUMERAL CONVERSION! 1.The program will use a menu selection that will
ID: 3716646 • Letter: J
Question
JAVA ROMAN NUMERAL CONVERSION!
1.The program will use a menu selection that will allow the user to convert from either an integer to a Roman numeral number, or from a Roman numeral number to an integer. 2.Note that all numbers are treated as integers, there are no real numbers.
3.You must use a String to accept a Roman numeral number input from the user, and you must also use that String with String methods to extract individual Roman numerals from the user input while converting it to an integer.
4.The largest value your program will allow is 3999. The Roman numeral number for this is MMMCMXCIX. You program will validate the user input to ensure that the user enters only valid numbers. You must validate the user’s Roman numeral input and the user’s integer input.
6. The Roman Numeral number must use a valid sequence of Roman Numerals in accordance with the FOLLOWING RULES :
M = 1000, D = 500, C = 100, L = 50, X = 10, V = 5, and I = 1
No more than 3 identical Roman Numerals in a row.
DXXX, MCCC, MMM, etc. would be allowed.
LXXXX, XIIII, MMMMCV, etc. would not be allowed.
V, L, and D may never precede a larger number.
XV, CV, CL, etc. would be allowed.
VX, VC, LC, etc. would not be allowed.
V, L, and D may never have more than one of themselves in a number.
XVI, CV, CL, etc. would be allowed.
XVIV, VV, CLL, etc. would not be allowed.
Only one Roman Numeral of a smaller value may precede a larger valued Roman numeral.
IX, CV, CL, etc. would be allowed.
IIV, CCM, XXL, etc. would not be allowed.
A Roman Numeral preceded by Roman Numeral of a smaller value may not be followed by any Roman Numerals of the same value, same smaller value, or a larger value.
IX, XCVI, CLX, etc. would be allowed.
CMC, XCM, CMM etc. would not be allowed.
A smaller Roman Numeral that precedes a larger Roman Numeral must be at least 1/10 the value of the larger Roman Numeral.
For example, IV equals 4, XC equals 90, and CM equals 900
XM is not allowed... X is 1/100 of M, neither is VL... while V is 1/10 the size of L, V is not allowed to precede a larger value as per Rule: 2.
Program requirements:
The program must allow the user to convert from Roman numeral number to an integer, and to a roman numeral number from an integer from a menu selection.
The program must allow the user to repeat the process as many times as they wish. This can be accomplished using a loop structure around the entire program.
The program must validate all user input before proceeding to the next step.
The program will use methods you develop to do the actual conversions. For example, the signature of a method to convert from a Roman numeral number to an integer might look as follows:
converRomanToInteger(String):int
This means that you must have at least two other methods besides the “main” method.
Explanation / Answer
Program
import java.util.*;
public class RomanToNumber {
int value(char r)
{
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;
return -1;
}
int romanToDecimal(String str)
{
// Initialize result
int res = 0;
for (int i=0; i<str.length(); i++)
{
// Getting value of symbol s[i]
int s1 = value(str.charAt(i));
// Getting value of symbol s[i+1]
if (i+1 <str.length())
{
int s2 = value(str.charAt(i+1));
// Comparing both values
if (s1 >= s2)
{
// Value of current symbol is greater
// or equalto the next symbol
res = res + s1;
}
else
{
res = res + s2 - s1;
i++; // Value of current symbol is
// less than the next symbol
}
}
else
{
res = res + s1;
i++;
}
}
return res;
}
String decimalToRoman(Integer number)
{
number = Math.min(3999, Math.max(1, number)); // wraps number between 1-3999
String asRomanNumerals = "";
// Array including numerals in ascending order
String[] RN = {"I", "V", "X", "L", "C", "D", "M" };
int i = 0; // Index used to keep track which digit we are translating
while (number > 0) {
switch(number % 10) {
case 1: asRomanNumerals = RN[i] + asRomanNumerals; break;
case 2: asRomanNumerals = RN[i] + RN[i] + asRomanNumerals; break;
case 3: asRomanNumerals = RN[i] + RN[i] + RN[i] + asRomanNumerals; break;
case 4: asRomanNumerals = RN[i] + RN[i + 1] + asRomanNumerals; break;
case 5: asRomanNumerals = RN[i + 1] + asRomanNumerals; break;
case 6: asRomanNumerals = RN[i + 1] + RN[i] + asRomanNumerals; break;
case 7: asRomanNumerals = RN[i + 1] + RN[i] + RN[i] + asRomanNumerals; break;
case 8: asRomanNumerals = RN[i + 1] + RN[i] + RN[i] + RN[i] +asRomanNumerals; break;
case 9: asRomanNumerals = RN[i] + RN[i + 2] + asRomanNumerals; break;
}
number = (int) number / 10;
i += 2;
}
return asRomanNumerals;
}
public static void main(String[] args)
{
RomanToNumber ob = new RomanToNumber();
Scanner in = new Scanner(System.in);
String s;
do
{
System.out.println("1.Integer to Roman Coversion : 2. Roman to Integer Conversion ");
System.out.println("Enter your choice: ");
int choice=in.nextInt();
if(choice==1)
{
System.out.println("Input an integer < 4000 : ");
System.out.println("The roman numeral version is: " + ob.decimalToRoman(in.nextInt()));
}
else if (choice==2)
{
System.out.println("Input a Roman Numeral : ");
System.out.println("The integer number is : " + ob.romanToDecimal(in.next()));
}
else
{
System.out.println("Invalid Choice");
}
System.out.println("Do you want to continue...(Press Y)");
s=in.next();
}while(s.equals("Y"));
}
}
Output
1.Integer to Roman Coversion : 2. Roman to Integer Conversion
Enter your choice:
1
Input an integer < 4000 :
1904
The roman numeral version is: MCMIV
Do you want to continue...(Press Y)
Y
1.Integer to Roman Coversion : 2. Roman to Integer Conversion
Enter your choice:
2
Input a Roman Numeral :
MCM
The integer number is : 1900
Do you want to continue...(Press Y)
N
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.