-4

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
Stidgeon
  • 2,673
  • 8
  • 20
  • 28
Faheem
  • 42
  • 9
  • 4
    Your program is not working at all. You don't do any conversions. And your toString method only returns Roman numerals, not Arabic. – WJS Jan 18 '20 at 23:40

1 Answers1

1

You need to define more structures. Here is a hint on converting from Arabic to Roman numerals.

String[] ones = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
String [] tens = {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
String [] hundreds = {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};

So lets say you have 423. That would be

423 % 10 = 3 and 3 - 1 = 2 to get the ones position.
(423/10) % 10 = 2 and 2 - 1 = 1 to get the tens position.
(423/100) % 10 = 4 and 4 - 1 = 3 to get the hundreds position.

So the roman numeral would be hundreds[3] + tens[1] + ones[2] = CDXXIII

Now you need to generalize that for all numbers.

WJS
  • 36,363
  • 4
  • 24
  • 39