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: 3668573 • Letter: W

Question

Write a program in Java that converts a number entered in Roman numerals to decimal. The program should consist of a class, say Roman. An object of type Roman should do the following (which means these are methods in the class):

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:

Roman    Decimal

M            1000

D            500

C            100

L            50

X            10

V            5

I             1

d. The class must contain the method romanToDecimal to convert a Roman numeral into its equivalent decimal number.

e. Write a TestClient to test the class program using the following Roman numerals. The user enters in: MCXIV, CCCLIX, and MDCLXVI

Explanation / Answer


// header files
    import java.util.Scanner;
   
    public class Roman {
      
       // declare variables
            static String romanNumeral;
            static int decimalNum;
            public static void main(String args[]) {
                    Roman rmtoD = new Roman();
                    rmtoD .convertToDecimal();
                    rmtoD .printRoman(romanNumeral);
            }
   
   // converting to decimal number
            public void convertToDecimal () {
                    Scanner scan = new Scanner(System.in);
                    System.out.print("Enter a Roman number: ");
                    romanNumeral = scan.nextLine();
                    romanNumeral = romanNumeral.toUpperCase();
                  
                    int l= romanNumeral.length();
                    int num=0;
                    int previousnum = 0;
                  
                   // checking the condition
                    for (int i=l-1;i>=0;i--)
                    {
                            char x = romanNumeral.charAt(i);
                            x = Character.toUpperCase(x);
                            switch(x)
                            {
                                    case 'I':
                                    previousnum = num;
                                    num = 1;
                                    break;
                                 case 'V':
                                         previousnum = num;
                                    num = 5;
                                    break;
                                    case 'X':
                                            previousnum = num;
                                    num = 10;
                                    break;
                                    case 'L':
                                            previousnum = num;
                                    num = 50;
                                    break;
                                    case 'C':
                                            previousnum = num;
                                    num = 100;
                                    break;
                                    case 'D':
                                            previousnum = num;
                                    num = 500;
                                    break;
                                    case 'M':
                                            previousnum = num;
                                    num = 1000;
                                    break;
                            }         
                            if (num<previousnum)
                            {decimalNum= decimalNum-num;}
                             else
                            decimalNum= decimalNum+num;
                    }
            }
           // print roman letter
            public static void printRoman (String romanNumeral){
                    System.out.println ("The equivalent of the Roman numeral "+romanNumeral+" is "+decimalNum);
            }
   
            // decimal number stored
           public static void printDecimal (int decimalNum){
                    System.out.println ("Decimal Number stored is: " + decimalNum);
            }
          
    }


sample output
                                                                                                                                                   
sh-4.3$ javac HelloWorld.java                                                                                                                               
sh-4.3$ java -Xmx128M -Xms16M HelloWorld                                                                                                                    
Enter a Roman number: MCXIV                                                                                                                                 
The equivalent of the Roman numeral MCXIV is 1114                                                                                                           
sh-4.3$ javac HelloWorld.java                                                                                                                               
sh-4.3$ java -Xmx128M -Xms16M HelloWorld                                                                                                                    
Enter a Roman number: CCCLIX                                                                                                                                
The equivalent of the Roman numeral CCCLIX is 359                                                                                                           
sh-4.3$ javac HelloWorld.java                                                                                                                               
sh-4.3$ java -Xmx128M -Xms16M HelloWorld                                                                                                                    
Enter a Roman number: MDCLXVI                                                                                                                               
The equivalent of the Roman numeral MDCLXVI is 1666                                                                                                         


       

      

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