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

Write a program that converts a number entered in Roman numerals to decimal. You

ID: 3545428 • Letter: W

Question

Write a program that converts a number entered in Roman numerals to decimal. Your program should consist of a class Roman.  An object of type Roman should do the following:


store number as a roman numeral

convert and store the number into decimal

print the number as a roman numeral or decimal number as requested by the user

decimal values of roman numerals are:

M 1000

D 500

C 100

L 50

X 10

V 5

I 1


your class must contain the method romanToDecimal to convert a roman numeral into its equivalent decimal number


test your program with

XI = 11

CCL = 250

CCCLIX = 359

Explanation / Answer

import java.util.Scanner;


public class Roman {


public static void main(String[] args) {

promptUserForNumeral(new Scanner(System.in));

}


private static int promptUserForNumeral(Scanner inScanner) {

int i = 0;

System.out.println("Enter a Numerical or Q to quit! : ");

String numerical = inScanner.next();

while (true) {

if (!numerical.equalsIgnoreCase("Q")) {

i = romanToDecimal(numerical);

System.out.println(numerical + " = " + i);

promptUserForNumeral(inScanner);

return i;

} else {

System.out.println("Goodbye!");

System.exit(1);

}

}

}


private static int romanToDecimal(String roman) {

int num;

if (roman.length() == 0)

throw new NumberFormatException(

"An empty string does not define a Roman numeral.");


roman = roman.toUpperCase();


int i = 0;

int arabic = 0;


while (i < roman.length()) {


char letter = roman.charAt(i);

int number = convertCharacterToNumber(letter); // Numerical


if (number < 0)

throw new NumberFormatException("Illegal character "" + letter

+ "" in roman numeral.");


i++;


if (i == roman.length()) {

arabic += number;

} else {

int nextNumber = convertCharacterToNumber(roman.charAt(i));

if (nextNumber > number) {

arabic += (nextNumber - number);

i++;

} else {

arabic += number;

}

}


}

if (arabic > 3999)

throw new NumberFormatException(

"Roman numeral must have value 3999 or less.");


num = arabic;

return num;

}


private static int convertCharacterToNumber(char numeral) {

/*

* switch (numeral) { case 'I': return 1; case 'V': return 5; case 'X':

* return 10; case 'L': return 50; case 'C': return 100; case 'D':

* return 500; case 'M': return 1000; default: return -1; }

*/

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 -1;

}

}

}


##########OUTPUT#################

Enter a Numerical or Q to quit! :

X

X = 10

Enter a Numerical or Q to quit! :

XX

XX = 20

Enter a Numerical or Q to quit! :

XI

XI = 11

Enter a Numerical or Q to quit! :

CCL

CCL = 250

Enter a Numerical or Q to quit! :

CCCLIX

CCCLIX = 359

Enter a Numerical or Q to quit! :

Q

Goodbye!

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