I have is that my entered Roman numerals work only if the numerlas are entered in large to smaller format. For exapmple if I type MMC program will correctly display 2100, but when I enter MCM it will display 2100 instead of 1900. I know that I need to come up with a way to determine what letter comes before so program can subtract it but at this point am little stuck. Here is my code:
package chapt08;
import javax.swing.JOp tionPane;
/* Write a program that converts a number entered in Roman numerals to decimal. Your program should consists
* of a class, say Roman. An objects of the types Roman should do the following:
* a) store the number as Roman numeral
* b) Convert and store the number into decimal
* c) print the number as a Roman numeral or decimal number as requested by the user
*
* The decimal values of the Roman numerals are
* M 1000
* D 500
* C 100
* L 50
* X 10
* V 5
* I 1
*
* d) test your program using the following Roman numerals: MCXIV, CCCLIX and MDCLXVI
*
*/
package chapt08;
import javax.swing.JOp tionPane;
/* Write a program that converts a number entered in Roman numerals to decimal. Your program should consists
* of a class, say Roman. An objects of the types Roman should do the following:
* a) store the number as Roman numeral
* b) Convert and store the number into decimal
* c) print the number as a Roman numeral or decimal number as requested by the user
*
* The decimal values of the Roman numerals are
* M 1000
* D 500
* C 100
* L 50
* X 10
* V 5
* I 1
*
* d) test your program using the following Roman numerals: MCXIV, CCCLIX and MDCLXVI
*
*/
Code:
public class Roman
{
static String romanNumeral;
static int decimalNum;
static char convertToDecimal;
public static void main(String args[])
{
Roman demo = new Roman();
demo.convertToDecimal(decimalNum);
printRoman(romanNumeral);
printDecimal(decimalNum);
setRoman();
toString(romanNumeral);
}
public void convertToDecimal (int other)
{
romanNumeral = JOptionPane.showInputDialog("Enter a Roman numeral to convert to decimal." + "\n\n"+ "Note: Roman numerals are I, V, X, L, C, D and M."+ "\n" + "All letters entered will be treated as capitalized.");
romanNumeral = romanNumeral.toUpperCase();
int x = 0;
do
{
convertToDecimal = romanNumeral.charAt(x); //return a char value from the first character in the string
switch(convertToDecimal)
{
case 'M':
decimalNum += 1000;
break;
case 'D':
decimalNum += 500;
break;
case 'C':
decimalNum += 100;
break;
case 'L':
decimalNum += 50;
break;
case 'X':
decimalNum += 10;
break;
case 'V':
decimalNum += 5;
break;
case 'I':
decimalNum += 1;
break;
}
x++;
}while(x<romanNumeral.length());
//make a condition here...
JOptionPane.showMessageDialog(null, "Roman Numeral Converted to Decimal is: " + decimalNum);// + ch,
//other=decimalNum.getCopy;
}
public static void printRoman (String romanNumeral){
System.out.println ("Roman Numeral stored is: " + romanNumeral);
}
public static void printDecimal (int decimalNum){
System.out.println ("Decimal Number stored is: " + decimalNum);
}
public static void setRoman (){
System.out.println("test Set Roman");
}
// public boolean equals (String romanNumeral, int decimalNum ){
// return (romanNumeral == decimalNum);
// }
public void Other(String temp)
{
temp = "";
}
public Roman getCopy(int decimalNum)
{
Roman temp = new Roman();
Roman.decimalNum = decimalNum;
System.out.println(" temp is: " + temp);
return temp;
}
public static String toString(String str)
{
//str = "test";
if (str==romanNumeral)
System.out.println("Roman numeral " + romanNumeral + " is equal to Decimal Number " + decimalNum);
else
System.out.println("Roman numeral " + romanNumeral + " is not equal to Decimal Number " + str);
return str;
}
}
Comment