Home » questions » can any1 please help me with my project? Its a java program that converts a hindu arabic no. to Rom

can any1 please help me with my project? Its a java program that converts a hindu arabic no. to Rom

2006-08-11 05:00:53, Category: Programming & Design
this project is a java program it we are going to input a hindu arabic number, and the output should be roman numeral. could anyone pls help me. i am just a beginner. i hope there would be someone to help me. thank you very much.

Answers

  1. puckstorm

    On 2006-08-11 08:59:25


    This is an interesting project. Here are a few pointers: 1. Write a class with a main method that validates a command line argument and then calls your class that does all the work (main method should be located inside that same class which you probably know): ArabToRomanConverter converter = new ArabToRomanConverter(); String roman = converter.convert(nbr); System.out.println(roman); 2. Work out a conceptual system of conversion, meaning, think about how you would convert an arabic numeral to roman in your head regardless of any specific programming language. The system is the following: going from right to left and converting each individual number to roman equivalent. For example, if you wanna convert 358, then first convert 8 (VIII), then convert 50 (L), then convert 300 (CCC), and finally, combine the three results in the right order (CCCLVIII). 3. Read up on roman numerals and conversion rules. The most important rules are: numbers starting with digit from 1 to 3 are represented through the corresponding number of symbols of the corresponding degree of 10. For instance, number 10 is X, number 20 is XX, number 30 is XXX, number 100 is C, number 200 is CC, number 300 is CCC. Numbers starting with 5 through 8 are represented through a special '5' number plus whatever was in the previous rule: 50 is L, 60 is LX, 70 is LXX, etc. Numbers starting with 4 or 9 are represented respectively through special '5' number and '10' number prepended with '10' number of a lesser degree: 90 is 'XC', 400 is 'CD', 900 is 'CM', etc. 4. Now you are ready to write the rest of the code. What you need is a loop that goes through digits of the number in reverse order, multiplies it by the right degree of 10, converts it, and prepends the result of conversion to the main result. Something like this: int count = 1; StringBuffer result = new StringBuffer(); while (tmpNbr > 0) { tmpDigit = tmpNbr%10; resultCmp = tmpDigit*count; romanEqv = findRomanEqv(resultCmp); result = result.insert(0, romanEqv); tmpNbr /= 10; count *= 10; } What you also need is a static HashMap of arabic numerals to roman characters: private static Map romanChars; static { romanChars = new TreeMap(); romanChars.put("1", "I"); romanChars.put("5", "V"); romanChars.put("10", "X"); romanChars.put("50", "L"); romanChars.put("100", "C"); romanChars.put("500", "D"); romanChars.put("1000", "M"); } And finally you need to do a conversion for each number in the loop. That you can do yourself, but use the rules from (3). Good luck and may the logic be with you!
  2. TJ

    On 2006-08-11 07:46:03


    This is an exercise in logic and conditional (if/then) statements. So you need to define the rules by which Roman numerals are generated and have them clearly stated on one side - you'll be building your conditional statements from those rules. On the other side, you know that Roman numbers work off a base 10 - so you want to come up with a way to parse the arabic numeral by 10s ("/" and "mod"). With each parse, you go to your particular set of decision rules and print accordingly. It should be very straightforward if you consider it in this fashion.