Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this lab you will write a Java program that manipulates numbers. The program

ID: 646038 • 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 should start with the following "skeleton" of Java code. Import this into your Eclipse workspace and fill in the methods as directed. For this assignment youWILLwant 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 issubtractedfrom 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 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

Subtractthe value of the first character from the total

Remove thefirst twocharacters 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 empty

The 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 inboldare 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!

Skeleton::

/*

* Project09.java

*

* A program that converts Roman numerals to decimal numbers.

* Used to practice breaking code up into methods.

*

* @author ENTER YOUR NAME HERE

*

*/

package osu.cse1223;

import java.util.Scanner;

public class Project09 {

public static void main(String[] args) {

// Fill in the body

}

// 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 program.

private static String promptUserForNumeral(Scanner inScanner) {

// Fill in the body

}

// 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) {

// Fill in the body

}

// 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) {

// Fill in the body

}

}

I 1 V 5 X 10 L 50 C 100 D 500 M 1000

Explanation / Answer

/** Project09.java** A program that converts Roman numerals to decimal numbers.* Used to practice breaking code up into methods.** @author ENTER YOUR NAME

HERE**/

import java.util.Scanner;

public class Project09
{
private static String promptUserForNumeral(Scanner inScanner)
{

String s="";
System.out.println("Please Enter String");
do
{
s=inScanner.nextLine();
System.out.println(s);
}
while(s.trim().isEmpty());
return s;
}

public static int CheckRoman(int TotalDecimal, int LastRomanLetter, int LastDecimal)
{
if (LastRomanLetter > TotalDecimal) {
return LastDecimal - TotalDecimal;
} else {
return LastDecimal + TotalDecimal;
}
}

private static int convertNumeralToNumber(String numeral)
{
int decimal = 0;
int lastNumber = 0;
String romanNumeral = numeral.toUpperCase();
for (int x = romanNumeral.length() - 1; x >= 0; x--)
{
char convertToDecimal = romanNumeral.charAt(x);

switch (convertToDecimal)
{
case 'M':
decimal = CheckRoman(1000, lastNumber, decimal);
lastNumber = 1000;
break;

case 'D':
decimal = CheckRoman(500, lastNumber, decimal);
lastNumber = 500;
break;

case 'C':
decimal = CheckRoman(100, lastNumber, decimal);
lastNumber = 100;
break;

case 'L':
decimal = CheckRoman(50, lastNumber, decimal);
lastNumber = 50;
break;

case 'X':
decimal = CheckRoman(10, lastNumber, decimal);
lastNumber = 10;
break;

case 'V':
decimal = CheckRoman(5, lastNumber, decimal);
lastNumber = 5;
break;

case 'I':
decimal = CheckRoman(1, lastNumber, decimal);
lastNumber = 1;
break;
}
}
return decimal;
}

public static void main(String[] args)
{
Project09 p=new Project09();
Scanner sc=new Scanner(System.in);
String s=p.promptUserForNumeral(sc);
int i=p.convertNumeralToNumber(s);
System.out.println("The numeral "+s+"is the decimal number" +i);
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote