Results 1 to 11 of 11
  1. #1
    number1cynic is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default convert from roman numerals to integers and vice versa

    Hi! I have a question. I have to write a class that takes roman numerals and returns their integer value and vice versa. Any and all help would be greatly appreciated.

    Here is what I have so far:


    public class RomanNumerals
    {
    //Return the integer value of a roman numeral digit
    private static int digitValue(char digit)
    {
    switch ( digit )
    {
    case 'I' : return 1;
    case 'V' : return 5;
    case 'X' : return 10;
    case 'L' : return 50;
    case 'C' : return 100;
    case 'D' : return 500;
    case 'M' : return 1000;
    default : throw new RuntimeException("Not a valid Roman Numeral");
    }
    }
    //Read a Roman Numeral string and return the integer value
    public static int decimalValue(String roman)
    {
    int value = 0;
    return value;
    }

    //Read a positive integer and return a string with its value in Roman numerals
    public static String romanValue(int decimal)
    {
    String roman = "";
    int N = number;
    while (N >= 1000) {
    // Move 1000 from N to roman.
    roman += "M";
    N -= 1000;
    }
    while (N >= 900) {
    // Move 900 from N to roman.
    roman += "CM";
    N -= 900;
    }
    //etc
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    number1cynic is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default

    Hi, thank you for the warm welcome!

    My question is... for the following method, I dont know where to begin:

    public static int decimalValue(String roman)
    {
    int value = 0;
    return value;
    }

  4. #4
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    i sense much "if then else" branches in the java force

    try to think like a roman. how your mind would process the roman digits ?
    put that on a paper with a sate flow chart and you got yourself a draft.
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  5. #5
    number1cynic is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default

    Ok I know that when the roman digits are strung together and a smaller digit is before a larger digit, we subtract. For example, IV = 5 -1 = 4... I also know that M = 1000, CM = 900, D = 500, CD = 400, C = 100, XC = 90, etc...

    I just dont know how to translate this into code.

  6. #6
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    okay, before i can help you translate take a pensil and a paper and draw some kind of a state flow chart which covers (hopefully) every possible calculation.

    if you have that you would have done 75% of the work. The rest is "easy" translate every branch from a state to an "if then [else if] else" statement.

    i did such assignment in my first semester... well i remember it was very "dendritic" :)
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  7. #7
    number1cynic is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default

    Is this what you mean?

    M = 1000
    CM = 900
    CCM = 800
    CCCM =700
    CCCM = 600
    D = 500
    CD = 400
    CCD = 300
    CC = 200
    C = 100
    XC = 90
    XXC = 80
    XXXC = 70
    LX = 60
    L = 50
    XL = 40
    XXL = 30
    XX = 20
    X = 10
    IX = 9
    IIX = 8
    IIIX =7
    VI = 6
    V = 5
    IV = 4
    III = 3
    II =2
    I = 1

    Or should I write down all the cases from 1 to 100 ??

    Also, thank u for working with me on this for so long ...

  8. #8
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    nah... i dont mean that. i want you to think about how you really transform roman numbers to integers

    its late here, so i go to bed :-)

    try to understand the following

    Java Code:
           Let arabic = 0
           Let i = 0  // representing a position in the string
           
           while i is a legal position in the string:
               Let ch be the character in position i
               Let N be the numeric equivalent of ch
               i++   // to account for the character, ch
               if there are no addition characters in the string:
                  Add N to arabic
               else:   // Try pairing the ch with the next character
                  Let N2 be the numeric equivalent of the NEXT character
                  If N < N2:  // Evaluate the characters as a pair
                      Add (N2 - N) to arabic
                      i++    // to account for the extra character
                  else:
                      Add N to arabic
    This is from another page; the citations will come later. i dont want spoil the work :-)
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Not quite. Say you are given a number 1974, describe the steps that you take to convert this by hand to Roman Numeral.

    [edit: I'm a little late in getting this post off the block it seems]

  10. #10
    number1cynic is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default

    thanks for ur help

  11. #11
    number1cynic is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default

    Ok I think I got it now... If I were converting by hand from integers to roman numerals, I would first convert the largest digit.

    For example, for 1974, I would first write down M (= 1000), then CM (= 900), then LXX (=70) and finally, IV (= 4)

    1974 = MCMLXXIV

Similar Threads

  1. Convert roman numerals
    By matzahboy in forum New To Java
    Replies: 4
    Last Post: 02-21-2010, 10:06 PM
  2. Replies: 5
    Last Post: 04-16-2009, 06:30 PM
  3. Postfix into prefix and vice versa
    By sfe23 in forum New To Java
    Replies: 9
    Last Post: 02-19-2009, 10:37 PM
  4. Converting to ASCII and vice-versa
    By pheonix in forum New To Java
    Replies: 2
    Last Post: 09-09-2008, 04:43 AM
  5. Convert roman numerals to integers
    By Felissa in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 11:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •