We are replacing the normal roman numeral letters V and I with A and B. Example:
ID: 3667849 • Letter: W
Question
We are replacing the normal roman numeral letters V and I with A and B.
Example: BA = 4 AB = 6
Write a program that converts a number entered in Roman numerals to an integer. Your program should consist of a class name RomandConvert. This class has the following: A. Has 2 data members which are rNum (string) and iNum (int) B. A function that calculates from Roman to Integer C. A function that prints the data members (like we have done in class.) The integer values of the Roman numerals are: In your main function (your test client). Implement a test1 function with BBB passed in as a value using the Constructor. In addition, implement a test2 function width BA using a setter function. Last, implement a test3 function with ABA using default constructor (this requires some thought). On all test, do not prompt the user; in addition, show all output to console.Explanation / Answer
public class RomanToDecimal {
public static void romanToDecimal(java.lang.String romanNumber) {
int decimal = 0;
int lastNumber = 0;
String romanNumeral = romanNumber.toUpperCase();
/* operation to be performed on upper cases even if user enters roman values in lower case chars */
for (int x = romanNumeral.length() - 1; x >= 0 ; x--) {
char convertToDecimal = romanNumeral.charAt(x);
switch (convertToDecimal) {
case 'M':
decimal = processDecimal(1000, lastNumber, decimal);
lastNumber = 1000;
break;
case 'D':
decimal = processDecimal(500, lastNumber, decimal);
lastNumber = 500;
break;
case 'C':
decimal = processDecimal(100, lastNumber, decimal);
lastNumber = 100;
break;
case 'L':
decimal = processDecimal(50, lastNumber, decimal);
lastNumber = 50;
break;
case 'X':
decimal = processDecimal(10, lastNumber, decimal);
lastNumber = 10;
break;
case 'A':
decimal = processDecimal(5, lastNumber, decimal);
lastNumber = 5;
break;
case 'B':
decimal = processDecimal(1, lastNumber, decimal);
lastNumber = 1;
break;
}
}
System.out.println(decimal);
}
public static int processDecimal(int decimal, int lastNumber, int lastDecimal) {
if (lastNumber > decimal) {
return lastDecimal - decimal;
} else {
return lastDecimal + decimal;
}
}
public static void main(java.lang.String args[]) {
romanToDecimal("AB");
}
}
//SAmple output-->
AB
6
//
BA
4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.