Roman | Decimal |
M | 1,000 |
D | 500 |
C | 100 |
L | 50 |
X | 10 |
V | 5 |
I | 1 |
The algorithm for converting a Roman numeral R1, R2, …, Rn to decimal is
1. Set i to 1, where i is the position of the symbol currently being scanned.
2. Set convert to 0. At the conclusion of the algorithm, convert is the decimal value of the Roman numeral.
3. If i = n, add the decimal value of Rn to convert and stop.
4. If Ri ≥ Ri+1 in decimal, add the decimal value of Ri to convert, set i to i+1, and go to step 3.
5. If Ri <>i+1 in decimal, subtract the decimal value of Ri from convert, set i to i+1, and go to step 3.
Example: The decimal value of the Roman numeral XIV is 14. Initially, convert is 0. Because X’s value id greater than I’s, we add 10 to convert so that convert now equals 10. Because I’s value is less than V’s, we subtract 1 from convert, which now equals 9. Because V is the last numeral, we add 5 to convert to obtain 14.
Your program should consist of a class, say RomanType. An object of the type RomanType should do the following:
a. Store the number as a Roman numeral.
b. Convert and store the number into decimal.
c. Print the number as a Roman numeral and decimal number.
Sample Code:
Method in class RomanType is for converting the value of the roman to decimal.
import java.util.*;
import javax.swing.*;
public class RomanType
{//open class RomanType
private String roman;
private char[] num = new char[10];
public RomanType()
{//open constructor
roman = "";
}//close constructor
public void setRom()//method to ask the user to set the roman numeral they want to convert
{//open method setRom()
roman = JOptionPane.showInputDialog(null,"This is a Roman Numeral converter \nPlease enter a valid Roman number:");
}//close method setRom()
public void romToDec()//method to convert roman numeral to decimal
{//open method romToDec()
if (roman.length() == 0)
JOptionPane.showMessageDialog(null,"You leaved a blank. Please fill in a Roman Number.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE);
if (roman.length() > 10)
JOptionPane.showMessageDialog(null,"You have entered more than 10 Roman Numbers \n Please enter a number which is less than 10 numbers in length", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE);
else
{
char[] romArray = roman.toCharArray() ;//convert String to an array of char
for (int i = 0 ; i < style="color: rgb(192, 192, 192);">//assingning array romArray to array num
{
if (romArray[i] == 'm' || romArray[i] == 'M')
num[i] = 1000 ;
else if (romArray[i] == 'd' || romArray[i] == 'D')
num[i] = 500 ;
else if (romArray[i] == 'c' || romArray[i] == 'C')
num[i] = 100 ;
else if (romArray[i] == 'l' || romArray[i] == 'L')
num[i] = 50 ;
else if (romArray[i] == 'x' || romArray[i] == 'X')
num[i] = 10 ;
else if (romArray[i] == 'v' || romArray[i] == 'V')
num[i] = 5 ;
else if (romArray[i] == 'i' || romArray [i] == 'I')
num[i] = 1 ;
else
JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE);
}
}
}// close method romToDec()
public int convert()//method to count the whole converted roman numeral to decimal
{//open method convert()
int convert = 0;
if (num[0] <= num[1] && num[2] > num[1])
JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE) ;
else if (num[1] <= num[2] && num[3] > num[2])
JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE) ;
else if (num[2] <= num[3] && num[4] > num[3])
JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE) ;
else if (num[3] <= num[4] && num[5] > num[4])
JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE) ;
else if (num[4] <= num[5] && num[6] > num[5])
JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE) ;
else if (num[5] <= num[6] && num[7] > num[6])
JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE) ;
else if (num[6] <= num[7] && num[8] > num[7])
JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE) ;
else if (num[7] <= num[8] && num[9] > num[8])
JOptionPane.showMessageDialog(null, "The Roman Numeral you have entered is invalid.", "Invalid Roman Value", JOptionPane.ERROR_MESSAGE) ;
//check the validity of the roman numeral. Example of invalid roman numeral is IIIV
else
{
for (int i = 0; i <= 8; i++)//count the whole roman numeral { if(i != roman.length() - 1)
{
if (num[i] >= num[i + 1])
convert = convert + num[i];
else
convert = convert - num[i];
}
if (i == roman.length() - 1)
convert = convert + num[roman.length() - 1];
}
}
return convert;//return the decimal value of the roman numeral
}//close method convert()
}//close class RomanType
Here is the main program.
import java.util.*;
import javax.swing.*;
public class Application
{//open class Application
public static void main (String [] args)
{//open main
int n = JOptionPane.showConfirmDialog(null, " Enter/Proceed with this program", "Enter/Proceed Program", JOptionPane.YES_NO_OPTION);
//ask user if they want to proceed or not
while(n == JOptionPane.YES_OPTION)
{
String output;
RomanType romanType = new RomanType();
romanType.setRom();
romanType.romToDec();
output = "The conversion from roman numeral to decimal is, " +romanType.convert();
JOptionPane.showMessageDialog(null, output, "Roman Value", JOptionPane.INFORMATION_MESSAGE);//output the decima value of the roman numeral
n = JOptionPane.showConfirmDialog(null, " Enter/Proceed with this program", "Enter/Proceed Program", JOptionPane.YES_NO_OPTION);
}
}//close main
}//close class Application
No comments:
Post a Comment