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

Write a program in java that converts a number entered in Roman numerals to deci

ID: 3661892 • Letter: W

Question

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

b. Convert and store the number into decimal.

c. Print the number as a Roman numeral or decimal number as requested by the user.
The decimal values of the Roman numerals are:
M 1000 D 500 C 100 L 50 X 10 V 5 I 1
d. Your class must contain the method romanToDecimal to convert a Roman numeral into its equivalent decimal number e. Test your program using the following Roman numerals: MCXIV, CCCLIX, and MDCLXVI.

Explanation / Answer

import java.util.*; //java package
public class Roman { //class name
public static int Rom(int Total, int LastLetter, int Last) { //constructor
if (LastLetter > Total) { // if condition that checks the condition
return Last - Total;
} else { //else condition
return Last + Total;
}
}
public Roman(String s){ //constructor
int dec=0;
int ln=0;
for(int i=s.length()-1;i>=0;i--){ //for loop
char c=s.charAt(i);
switch(c) //switch statement
{
case 'M':
dec = Rom(1000, ln, dec);
ln = 1000;
break;

case 'D':
dec = Rom(500, ln, dec);
ln = 500;
break;

case 'C':
dec = Rom(100, ln, dec);
ln = 100;
break;

case 'L':
dec = Rom(50, ln, dec);
ln = 50;
break;

case 'X':
dec = Rom(10, ln, dec);
ln = 10;
break;

case 'V':
dec = Rom(5, ln, dec);
ln = 5;
break;

case 'I':
dec = Rom(1, ln, dec);
ln = 1;
break;
}
}
System.out.println(dec);
}

}
class RomanD{ //another class
public static void main (String[] args){ //main function
Scanner sc=new Scanner(System.in); //scanner function
String s=sc.nextLine(); //take the roman number from user
Roman r = new Roman(s); //give value to object
}
}

OutPut :

MCXIV
1114

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