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

Write a program that converts a number entered in Roman numerals to decimal. You

ID: 3760222 • Letter: W

Question

Write a program 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

please rate - thanks

import java.util.*;
public class Malik8_2
{static Scanner in=new Scanner(System.in);
public static void main(String[] args)
{String input;
char RD;
System.out.print("Enter a roman number: ");
input=in.nextLine();
Roman a=new Roman(input);
System.out.println("Output as Roman number or decimal (enter R or D): ");
RD=in.next().charAt(0);
if(Character.toUpperCase(RD)=='R')
     System.out.println(a.getRoman());
else
     System.out.println(a.getDecimal());   
}
}

------------------------------

public class Roman
{private String roman;
private int decimal;
public Roman(String r)
    {int i,prev=1000;
    roman=r;
    decimal=0;
    for(i=0;i<r.length();i++)
          {switch(r.charAt(i))
             {case 'm':
                case 'M': decimal+=1000;
                         if(prev<1000)
                              decimal-=(2*prev);
                          prev=1000;
                          break;
                case 'd':               
             case 'D': decimal+=500;
                         if(prev<500)
                              decimal-=(2*prev);
                          prev=500;
                          break;
                case 'c':               
             case 'C': decimal+=100;
                         if(prev<100)
                              decimal-=(2*prev);
                          prev=100;
                          break;
            case 'l':
             case 'L':decimal+=50;
                         if(prev<50)
                              decimal-=(2*prev);
                          prev=50;
                          break;
                case 'x':               
             case 'X': decimal+=10;
                         if(prev<10)
                              decimal-=(2*prev);
                          prev=10;
                          break;
                case 'v':               
             case 'V': decimal+=5;
                         if(prev<5)
                              decimal-=(2*prev);
                          prev=5;
                          break;
                case 'i':               
             case 'I': decimal++;
                      prev=1;
             }
         }
}

    public int getDecimal()
    {return decimal;
         }

    public String getRoman()
    {return roman;
         }
}

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