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: 3668754 • 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

import java.util.Scanner;

public class Roman {

//declaring ststic variables

        static String romanNumeral;

        static int decimalNum;

        public static void main(String args[]) {

//creating an object for Roman class

                Roman rmtoD = new Roman();

//calling romanToDecimal() for converting roman to decimal values

                rmtoD .romanToDecimal();

                rmtoD .printRoman(romanNumeral);

        }

        public void romanToDecimal () {

                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;

                for (int i=l-1;i>=0;i--)

                {

                        char x = romanNumeral.charAt(i);

                        x = Character.toUpperCase(x);

//Assigning decimal values to respective roman numbers using switch case

                        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;

                }

        }

//initiating printRoman() for printing decimal value of respective roman number

        public static void printRoman (String romanNumeral){

                System.out.println ("The equivalent of the Roman numeral "+romanNumeral+" is "+decimalNum);

        }

        public static void printDecimal (int decimalNum){

                System.out.println ("Decimal Number stored is: " + decimalNum);

        }

      

}//end java program

o/p:

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

Enter a roman number: MCXIV
The equivalent of the Roman numeral MCXIV is 1114

Enter a roman number: CCCLIX
The equivalent of the Roman numeral CCCLIX is 359

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