I wrote a program to convert Roman numerals to original numerals and original to Roman. My code is:
import static java.lang.System.*;
public class RomanNumeral
{
private int number;
private String roman;
private final static int[] NUMBERS = { 1000, 900, 500, 400, 100, 90, 50,
40, 10, 9, 5, 4, 1 };
private final static String[] LETTERS = { "M", "CM", "D", "CD", "C", "XC",
"L", "XL", "X", "IX", "V", "IV", "I" };
public RomanNumeral(String roman) {
setRoman(roman);
}
public RomanNumeral(int number) {
setNumber(number);
}
public void setNumber(int number) {
this.number = number;
}
public void setRoman(String roman) {
this.roman = roman;
}
public int getNumber() {
return number;
}
public String toString() {
return roman + "\n";
}
}
It's working good but I have a problem in converting original numeral to Roman numeral. The answers that I get from this code is:
10 is null
100 is null
1000 is null
2500 is null
1500 is null
23 is null
38 is null
49 is null
LXXVII is 49
XLIX is 49
XX is 49
XXXVIII is 49
I do not understand why it gives null for Roman numeral. It has a problem in converting original number to Roman number. It also has a problem to convert from Roman to original. It does not give the correct answers that I need. Help me and please do not change major things that I do not understand. Thank you
The Runner for this code is:
import static java.lang.System.*;
public class Main
{
public static void main( String args[] )
{
RomanNumeral test = new RomanNumeral(10);
out.println("10 is " + test.toString());
test.setNumber(100);
out.println("100 is " + test.toString());
test.setNumber(1000);
out.println("1000 is " + test.toString());
test.setNumber(2500);
out.println("2500 is " + test.toString());
test = new RomanNumeral(1500);
out.println("1500 is " + test.toString());
test.setNumber(23);
out.println("23 is " + test.toString());
test.setNumber(38);
out.println("38 is " + test.toString());
test.setNumber(49);
out.println("49 is " + test.toString());
test.setRoman("LXXVII");
out.println("LXXVII is " + test.getNumber() + "\n");
test.setRoman("XLIX");
out.println("XLIX is " + test.getNumber() + "\n");
test.setRoman("XX");
out.println("XX is " + test.getNumber() + "\n");
test.setRoman("XLIX");
out.println("XLIX is " + test.getNumber() + "\n");
}
}
Please check if runner has any problems.
The correct answers that I need are:
10 is X
100 is C
1000 is M
2500 is MMD
1500 is MD
23 is XXIII
38 is XXXVIII
49 is XLIX
LXXVII is 77
XLIX is 49
XX is 20
XXXVIII is 38