Question

In: Computer Science

Exercise 1: Write a program that converts a number entered in Roman numerals to decimal. Your...

Exercise 1:

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:

  1. Store the number as a Roman numeral.
  2. Convert and store the number into decimal.
  3. 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

  1. Your class must contain the method romanToDecimal to convert a Roman numeral into its equivalent decimal number
  2. Test your program using the following Roman numerals: MCXIV, CCCLIX, and MDCLXVI.

Solutions

Expert Solution

solution:

import java.util.*;
public class Roman
{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;
}
}

please give me thumb up


Related Solutions

Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change it to convert an octal number to decimal in perl language.
c ++ program that converts from any base to a decimal number
c ++ program that converts from any base to a decimal number
***C PROGRAMMING*** Roman Numerals: I           =          1 V          =       
***C PROGRAMMING*** Roman Numerals: I           =          1 V          =          5 X          =        10 L          =        50 C          =      100 D          =      500 M         =     1000 Use % for modulus division. Step 1 Write a program that will and display 3 menu options. The options are: Display the first 50 Roman Numerals. Enter a number to be converted to Roman Numerals. Exit. Prompt the user for a selection: A, B or C. If the user selects A then print 2 Columns...
Write a java program to convert a positive integer value to a roman number. Your program...
Write a java program to convert a positive integer value to a roman number. Your program should print a description, prompt them for how many numbers they need to convert and then loop that many times. Each time the program needs to prompt for a number and output the equivalent roman numeral. If the user enters a number less than 1 then output an error message. See sample output below: **************************************************** * Welcome to the Roman Numeral Converter!          * *...
3. Write a java method that accepts a binary number and converts it to decimal then...
3. Write a java method that accepts a binary number and converts it to decimal then display the result. For Example: (110)2 = (6)10 (2 2 *1)+ (21 *1) + (20*0) = 6 Additional task: write a method that accepts a decimal and converts it to binary. i need to solve it as soon as and i will upvote you directly
2. Create a C++ program that converts the number of American dollars entered by the user...
2. Create a C++ program that converts the number of American dollars entered by the user into one of the following foreign currencies: Euro, British pound, German mark, or Swiss franc. Allow the user to select the foreign currency from a menu. Store the exchange rates in a four-element double array named rates. Notice that the menu choice is always one number more than the subscript of its corresponding rate. For example, menu choice 1's rate is stored in the...
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are...
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are not allowed to use library functions for conversion. The output should look exactly as follows: DECIMAL      BINARY                     HEXDECIMAL                      BCD 0                      0000 0000                   00                                            0000 0000 0000 1                      0000 0001                   01                                            0000 0000 0001 2                      0000 0010                   02                                            0000 0000 0010 .                       .                                   .                                               . .                       .                                   .                                               . 255                  1111 1111                   FF                                            0010 0101 0101
Write a program that accepts a number of minutes and converts it both to hours and...
Write a program that accepts a number of minutes and converts it both to hours and days. For example, 6000 minutes is 100.0 hours or 4.166666666666667 days. (I currently have what is below) import java.util.Scanner; public class MinutesConversion { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numOfMinutes = sc.nextInt(); double hour = numOfMinutes/60.00; double days = (hour/24); System.out.println(numOfMinutes + " minutes is " + hour + " hours or " + days + " days.");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT