Hey people, can anyone help me with this little conundrum? I want to write the following Java program: - I have an existing class - Roman, which has two class constructors, one that creates a Roman No object given a decimal value and a second that creates a Roman No object given a text value Two object methods getRoman() and getDecimal(), return the Roman text and decimal values of a Roman No object. I wish to write a Java program that performs simple arithmetic(add +, subtract -, multiply * and divide /) using Roman numerals. The program should allow the user to enter an input string of a simple roman numeral expression in typical arithmetic format: " Enter Roman Numeral Expression:XXX-XIV " to produce the answer: " Answer is: XVI " For division the program should produce a quotient and remainder: " Enter Roman Numeral Expression:XXX/VIII " to produce the answer: " Answer is: III Rem VI " The program should split up the input string to extract the Roman Numbers and the arithmetic operator. The Roman numbers should not have any inclusive spaces, all other spaces should be treated as white space ie ignored. I need to create object instances of the class Roman, do the actual arithmetic using decimal values, by using the object methods of class Roman to obtain the appropriate Roman or Decimal values as required.
You will have to use a StringTokenizer to break up your argument into the roman numerals and the arithmetic operator. The fist constructor to StringTokenizer takes a string and the delimiters to use for separating the string into tokens. The second version takes only the string to break up (it uses white space for delimiters.) The third version takes a string, delimiters, and a boolean which specifies whether or not to return the delimiters as tokens. The other two versions don't return the delimiters as tokens. You will want to use this third version, because you need to inspect the delimiters and act on them. For example:
Now you have your values in separate variables. You can convert them to integers of floating point numbers or whatever you like and do the operation based on the value in operator. [This message has been edited by Bodie Minster (edited December 11, 2000).]