For this lab you will write a Java program that manipulates numbers. The program
ID: 3681224 • Letter: F
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 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
Roman Numerals vs. Decimal Numbers
The following table shows the values of individual Roman numerals:
Here is a table of the digits from 1 to 9 in the Roman system:
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 subtracted from 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:
And here is a table of the values between 100 and 900, by hundreds:
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!
Explanation / Answer
import java.util.Scanner;
public class Project09 {
public static void main(String[] args) {
boolean flag = true;
while (flag) {
Scanner input = new Scanner(System.in);
String numeral = promptUserForNumeral(input);
if (numeral.charAt(0) == 'Q' || numeral.charAt(0) == 'q') {
System.out.println("Goodbye!");
flag = false;
} else {
int dec = convertNumeralToNumber(numeral);
System.out.println("The numeral " + numeral + " is the decimal number " + dec);
System.out.println();
}
}
}
// Given a Scanner as input, prompts the user to input a Roman numeral. Checks to make
// sure that the user does not enter an empty String. If the user does enter an
// empty String, report an error and ask for a new String until a non-empty String is
// provided. Return the String input by the user to the calling progra
private static String promptUserForNumeral(Scanner inScanner) {
boolean run = true;
String numeral = "";
while (run) {
System.out.print("Enter a roman numeral (Q to quit): ");
numeral = inScanner.nextLine();
numeral = numeral.toUpperCase();
if (numeral.equalsIgnoreCase("")) {
System.out.println("ERROR! You must enter a non-empty line!");
System.out.println();
} else if (numeral.equalsIgnoreCase("Q")) {
run = false;
} else {
run = false;
}
}
return numeral;
}
// Given a String as input, converts the String to a number assuming that the String
// is a Roman numeral (following the rules in the writeup for Project 09). Returns the
// number to the calling program. NOTE: This method can possibly get long and complex.
// This about how you can break it up into two or three smaller methods to make it
// less complex.
private static int convertNumeralToNumber(String numeral) {
int decimal = 0;
while (numeral.length() != 0) {
if (numeral.length() == 1) {
int i = 0;
decimal = decimal + convertCharacterToNumber(numeral.charAt(i));
numeral = "";
} else {
int i = 0;
int firstNum = convertCharacterToNumber(numeral.charAt(i));
int secondNum = convertCharacterToNumber(numeral.charAt(i + 1));
if (firstNum > secondNum) {
decimal = decimal + firstNum;
numeral = numeral.substring(1, numeral.length());
} else if (firstNum == secondNum) {
decimal = decimal + (firstNum + secondNum);
numeral = numeral.substring(2, numeral.length());
} else {
decimal = decimal + secondNum - firstNum;
numeral = numeral.substring(2, numeral.length());
}
}
}
return decimal;
}
// Given a character that contains a single numeral, returns the integer value for
// that character. Use the tables in the writeup for Project 09 to fill in this method.
// For example, if the method is called with:
// convertCharacterToNumber('X')
// the method should return the value 10. And if the method is called with:
// convertCharacterToNumber('L')
// the method should return the value 50.
// Use this method with convertNumeralToNumber above to convert an entire Roman numeral
// into a number.
private static int convertCharacterToNumber(char numeral) {
if (numeral == 'I') {
return 1;
} else if (numeral == 'V') {
return 5;
} else if (numeral == 'X') {
return 10;
} else if (numeral == 'L') {
return 50;
} else if (numeral == 'C') {
return 100;
} else if (numeral == 'D') {
return 500;
} else if (numeral == 'M') {
return 1000;
} else {
return 0;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.