Write a program that stores and converts a number entered in Roman numerals to an integer value. Your program must consist of a class named Roman. An object of type Roman must do the following:
1.) Store the number as a Roman numeral,
2.) Convert and store the number also as an integer value,
3.) Print the number as a Roman numeral or integer number as requested by a method call,
4.) Implements a constructor.
So for example, you should be able to write a program that looks something like this:
Roman r = new Roman("MCXIV");
r.printInt(); //prints 1114
r.printRoman(); //prints MCXIV
Roman s = new Roman("CCCLIX");
s.printInt(); //prints 359
The Roman numerals are:
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000© BrainMass Inc. brainmass.com August 21, 2018, 7:12 pm ad1c9bdddf
This looks a little more complicated of a problem than it really is. Storing a Roman numeral really just means that you want to be able to store the character string that was entered, like "MMCCVI". To be able to do arithmetic on a computer with it, though, you also have to be able to convert it into an integer. To my knowledge, there aren't any "Roman numeral" data types in any computer programming language. Though, after this assignment, you will be well on your way to creating one.
Converting a character string into a number that can be used in arithmetic functions is something all languages need to be able to accomplish. There are usually functions of some kind to perform that, or there are built in elements in the language itself, like the ability to "cast" a value into another value like C has.
That's the basic crux of the program that you're writing, to be able to convert a Roman numeral into an integer number that, presumably, could then be used in arithmetic calculations.
I would suggest that you want to treat the character string like it is a list, taking off one letter at a time to process it. The instructions your teacher gave you show the values for each letter, as well as what letters would be considered valid "numbers" (for example: a "G" isn't a Roman numeral). An "M" converts to 1000 in our decimal numbering system, a "C" is 100, and so on. You also need to take into account the short-hand notation that Roman numerals allow, which is the ability to write "CM" instead of "CCCC". The "C" in front of the "M" means that the value of "M" is modified to be "C" less, or 900 instead of 1000. That seems to imply you'll want to keep track of the previously read letter in the list at the same time you are processing a letter.
There are various ways to take off each letter at a time. That seems to be the best place to start off. Write a loop that will take a character string as input and strip off each letter at a time, putting it into a one letter long character string. Then, once you are confident that it works, you can begin the process of converting them into an integer.
In the simple situation, where there are no "modifying" letters preceding another letter (like the example above where "CM" means 900), the algorithm just needs to get the value for the letter from a table that you can create and store as part of the code. In other words, you'll need to have a two dimensional table, or some other type of structure of you're choosing, that you can go to when you process each letter. Again, your teacher has supplied you with the data, you just need to convert that into a table ...
In this document (containing approximately 1,900 words) you will gain insight into the process of breaking down a problem into an analytic solution. That solution can be coded into something a computer can then process.
You will be taught, in steps, how to write a computer algorithm to convert numbers into words. The example used is the problem of converting and storing Roman numerals into integer values. It is a more general solution, however, that can be used to convert any number stored as an integer or floating point into the corresponding English language words. It can be applied to problems such as: Converting integer numbers into the English language words (2 = "two", 156 = "one hundred fifty six", 4,043 = "four thousand forty three"), Converting floating point numbers used to store money into the words printed out on checks ($1,234.56 = "One thousand, two hundred, thirty four dollars and 56 cents").