Question: For this lab you will write a Java program that manipulates numbers. The program will ask the user to enter a Roman numeral (or a
For this lab you will write a Java program that manipulates numbers. The program will ask the user to enter a Roman numeral (or a Q to quit) and then convert that numeral into a base 10 integer and display the result. The program should loop until the user enters a "Q" when prompted. For this assignment you must start with the following "skeleton" of Java code. Import this into your Eclipse workspace and fill in the methods as directed. For this assignment you WILL want to add extra methods beyond the methods defined in the skeleton. Feel free to add any methods you find useful, but make sure that you add comments indicating what they do following the form of the rest of the comments in the code. Project09.java: http://web.cse.ohio-state.edu/cse1223/currentsem/projects/Project09.java
Roman Numerals vs. Decimal Numbers
The following table shows the values of individual Roman numerals:
| I | 1 |
| V | 5 |
| X | 10 |
| L | 50 |
| C | 100 |
| D | 500 |
| M | 1000 |
Here is a table of the digits from 1 to 9 in the Roman system:
| I | 1 |
| II | 2 |
| III | 3 |
| IV | 4 |
| V | 5 |
| VI | 6 |
| VII | 7 |
| VIII | 8 |
| IX | 9 |
Note that the Roman system in general is an additive system - the value for "VII" is determined by adding the value for the symbol "V" (5) to the value for the symbols "I" and "I" (1+1) to get a total of 7. The exceptional cases are where a symbol has a value that is smaller than the value of the symbol that comes after it - such as the case for "IV" (4) and "IX" (9). Here the smaller value is subtractedfrom the larger value instead. This rule holds true for all place values. Here is a table of the values between 10 and 90, by tens:
| 10 | X |
| 20 | XX |
| 30 | XXX |
| 40 | XL |
| 50 | L |
| 60 | LX |
| 70 | LXX |
| 80 | LXXX |
| 90 | XC |
And here is a table of the values between 100 and 900, by hundreds:
| 100 | C |
| 200 | CC |
| 300 | CCC |
| 400 | CD |
| 500 | D |
| 600 | DC |
| 700 | DCC |
| 800 | DCCC |
| 900 | CM |
To build more complex values than these, Roman numerals use a system where, like the decimal system, there are "place values" for thousands, hundreds, tens and ones. For example, the decimal number 1986 has a 6 in the "ones" place, an 8 in the "tens" place, a 9 in the "hundreds" place and a 1 in the "thousands" place. To determine the value of the number you add 1x1000 + 9x100 + 8x10 + 6x1. Place values in the Roman system operate a bit differently, because each place can have multiple symbols instead of just one. So to get the equivalent Roman numeral for a number we look at each place individually and concatenate the strings for each position together. For example, if we want to know the Roman numeral representation of 1986 we break it down into its component pieces: 1000 - M +900 - CM + 80 - LXXX + 6 - VI So as a Roman numeral, 1986 would be represented by the String "MCMLXXXVI".
Converting from Roman Numerals to Decimal Numbers
To convert from a Roman numeral to a decimal number, your code will need to account for the fact that position matters for these symbols - a "I" that comes before a "V" means something different than a "I" that comes after a "V". One algorithm for performing this conversion is:
Start with a value of 0 for your decimal number
While your String containing your Roman numeral is not emptyTake the first character from the Roman numeral If there are no other characters on the String after the first character OR if the value of the first character is equal to or larger than the value of its immediate neighbor:
Add its value to the total
Remove the first character from the Roman numeral
If the value of the first character is less than the value of its immediate neighbor:
Add the value of its neighbor to the running total
Subtract the value of the first character from the total
Remove the first two characters from the Roman numeral
Here's an example of the numeral XCVI conversion to a decimal number by applying the algorithm above
Start with Number = 0
The String "XCVI" is not empty
The first character 'X' is 10, the second character 'C' is 100
10<100
Number = Number + 100 - 10
Number = 90
Remove "XC" from the front of the String
The String "VI" is not empty
The first character 'V' is 5, the second character 'I' is 1
5 > 1
Number = Number + 5
Number = 95
Remove "V" from the front of the String
The String "I" is not emptyThe first character 'I' is 1, there are no other characters
Number = Number + 1
Number = 96
Remove "I" from the front of the String
The String "" is empty
Our final result is 96
Project 09 Sample Output
This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program. Enter a roman numeral (Q to quit): MCMLXXXIV The numeral MCMLXXXIV is the decimal number 1984 Enter a roman numeral (Q to quit): MMMCMXCIX The numeral MMMCMXCIX is the decimal number 3999 Enter a roman numeral (Q to quit): MMM The numeral MMM is the decimal number 3000 Enter a roman numeral (Q to quit): Q Goodbye! Note that your output depends on the choices made by the user. You are not required to check to make sure that the user inputs a valid Roman numeral for this assignment, but you ARE required to make sure that the user does not enter an empty line. Enter a roman numeral (Q to quit): ERROR! You must enter a non-empty line! Enter a roman numeral (Q to quit): XX The numeral XX is the decimal number 20 Enter a roman numeral (Q to quit): Q Goodbye!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
