21.5k views
5 votes
Write a program that converts a number entered in Roman numerals to decimal.

Your program should consisit of a "class", say, romanType. An object of type romanType 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 as requested by user.
the decimal values of the roman numerals are:
M=1000, D=500, C=100, L=50, X=10, V=5, I=1.

d. Test your program using the following Roman numerals: MCXIV, CCCLIX, MDCLXVI

1 Answer

5 votes

Answer:

Here is the JAVA program:

import java.util.Scanner; // for importing scanner class

public class RomanToDecimal{ //class to convert roman numeral to decimal

static String romNum; // variable that stores roman numeral

static int decNum; //variable that stores decimal number

public static void main(String args[]) { //start of the program

//romtodec object created

RomanToDecimal romtodec = new RomanToDecimal();

//call function decimalConversion() that converts roman numeral to decimal

romtodec. decimalConversion();

//call function displayRomanNumeral to display the results of conversion

romtodec. displayRomanNumeral(romNum);

//call function to display the decimal no. of roman numeral

romtodec . displayDecimalNumber(decNum); }

//method decimalConversion to convert Roman numeral to decimal

public void decimalConversion () {

Scanner scan = new Scanner(System.in); //to get input

//prompts user to enter a Roman numeral

System.out.print("enter a Roman numeral: ");

romNum = scan.nextLine(); //reads input

//stores the length of the entered Roman numeral

int length= romNum.length();

int number=0; //initializes number to 0

int prevNum = 0; //initializes previous number to 0

//loops through the length of the input Roman numeral

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

//charAt() method returns the roman numeral symbol at the index i

{ char symbol = romNum.charAt(i);

//every symbol will be converted to upper case

symbol = Character.toUpperCase(symbol);

//switch statement tests the symbol variable for equality against a list of //numerals given and each numeral is assigned its equal decimal value.

switch(symbol)

{ case 'I':

prevNum = number;

number = 1;

break;

case 'V':

prevNum = number;

number = 5;

break;

case 'X':

prevNum = number;

number = 10;

break;

case 'L':

prevNum = number;

number = 50;

break;

case 'C':

prevNum = number;

number = 100;

break;

case 'D':

prevNum = number;

number = 500;

break;

case 'M':

prevNum = number;

number = 1000;

break; }

/*checks if the number is greater than previous number

if the value of current roman symbol (character) is greater than the next

symbol's value. then add value of current roman symbol to the decNum

else subtract the value of current roman symbol from the value of its next symbol. */

if (number>prevNum)

{ decNum= decNum+number; }

else

decNum= decNum-number; }

//displays the decimal value of the roman numeral

public static void displayRomanNumeral (String romNum){

System.out.println ("The Roman numeral "+romNum+" is equal to decimal number"+decNum); }

//displays the number as decimal

public static void displayDecimalNumber (int decNum){

System.out.println ("The decimal number is: " + decNum); } }

Output:

enter a Roman numeral: MCXIV

The Roman numeral MCXIV is equal to decimal number 1114

Output:

enter a Roman numeral: CCCLIX

The Roman numeral CCCLIX is equal to decimal number 359

Output:

enter a Roman numeral: MDCLXVI

The Roman numeral MDCLXVI is equal to decimal number 1666

User Marc Uberstein
by
5.5k points