1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
pantera1 [17]
3 years ago
14

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 number values of the Roman numerals are:M 1000D 500C 100L 50X 10V 5I 1d. Your class must contain the method romanToDecimal to convert a Roman numeral into its equivalent decimal numbere. Test your program using the following Roman numerals: CXIV, CCCLIX, and MDCLXVI.
Computers and Technology
1 answer:
arlik [135]3 years ago
3 0

Answer:

The code is given below in Java with appropriate comments

Explanation:

import java.util.*;

public class RomantoDecimal {

    public static void main(String[] args)

    {

         Scanner SC = new Scanner(System.in);

         RomantoDecimal r = new RomantoDecimal();

         System.out.println("Enter a Roman number :");

         // INPUT A ROMAN NUMBER

         String rNum = SC.next();

         // CALL convertToDecimal FOR CONVERSION OF ROMAN TO DECIMAL

         r.convertToDecimal(rNum);

    }

    // M=1000, D=500, C=100, L=50, X=10, V=5, I=1

    public void convertToDecimal(String roamNo)

    {

         int number = 0;

         // TEACK EACH DIGIT IN THE GIVEN NUMBER IN REVERSE ORDER

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

         {

             // FIND OUT WHETHER IT IS 'M' OR NOT

             if (roamNo.charAt(i) == 'M')

             {

                  if (i != 0)

                  { // CHECK WHETHER THERE IS C BEFORE M

                       if (roamNo.charAt(i - 1) == 'C')

                       {

                            number = number + 900;

                            i--;

                            continue;

                       }

                  }

                  number = number + 1000;

             }

             // FIND OUT WHETHER IT IS 'D' OR NOT

             else if (roamNo.charAt(i) == 'D')

             {

                  if (i != 0)

                  {

                  // CHECK WHETHER THERE IS C BEFORE D

                       if (roamNo.charAt(i - 1) == 'C')

                       {

                            number = number + 400;

                            i--;

                            continue;

                       }

                 }

                  number = number + 500;

             }

            // FIND OUT WHETHER IT IS 'C' OR NOT

             else if (roamNo.charAt(i) == 'C')

             {

                  if (i != 0)

                  {

                       if (roamNo.charAt(i - 1) == 'X')

                       {

                            number = number + 90;

                            i--;

                            continue;

                      }

                  }

                  number = number + 100;

             }

             else if (roamNo.charAt(i) == 'L')

             {

                  if (i != 0)

                  {

                       if (roamNo.charAt(i) == 'X')

                       {

                            number = number + 40;

                            i--;

                            continue;

                       }

                  }

                  number = number + 50;

             }

             // FIND OUT WHETHER IT IS 'X' OR NOT

             else if (roamNo.charAt(i) == 'X')

             {

                  if (i != 0)

                  {

                       if (roamNo.charAt(i - 1) == 'I')

                       {

                            number = number + 9;

                            i--;

                            continue;

                       }

                  }

                  number = number + 10;

             }

             // FIND OUT WHETHER IT IS 'V' OR NOT

             else if (roamNo.charAt(i) == 'V')

             {

                  if (i != 0)

                  {

                       if (roamNo.charAt(i - 1) == 'I')

                       {

                            number = number + 4;

                            i--;

                            continue;

                       }

                  }

                  number = number + 5;

             }

            else // FIND OUT WHETHER IT IS 'I' OR NOT

             {

                  number = number + 1;

             }

         }// end for loop

         System.out.println("Roman number: " + roamNo + "\nIts Decimal Equivalent: " + number);

    }

}

You might be interested in
Which of the following statements represents the number of columns in a regular two-dimensional array named values?
puteri [66]

Answer:

(a) values[0].length

Explanation:

In programming languages such as Java, an array is a collection of data of the same type. For example, a and b below are an example of an array.

a = {5, 6, 7, 9}

b = {{2,3,4}, {3,5,4}, {6,8,5}, {1,4,6}}

But while a is a one-dimensional array, b is a regular two-dimensional array. A two-dimensional array is typically an array of one-dimensional arrays.

Now, a few thing to note about a two-dimensional array:

(i) The number of rows in a 2-dimensional array is given by;

<em>arrayname.length</em>

For example, to get the number of rows in array b above, we simply write;

<em>b.length </em>which will give 4

(ii) The number of columns in a 2-dimensional array is given by;

<em>arrayname[0].length</em>

This is with an assumption that all rows have same number of columns.

To get the number of columns in array b above, we simply write;

<em>b[0].length </em>which will give 3

Therefore, for a regular two-dimensional array named values, the number of columns is represented by: values[0].length

5 0
3 years ago
What forms the foundation of cloud computing?
makkiz [27]
Virtualization. To enable easy deployment and scalability, it should be easy to spin up new machines on request. Virtualization offers the possibility to clone operating systems and deploy solutions independent of the actual hardware.
8 0
3 years ago
You have been hired by Beta Airlines to help them with their flight prices. You are to create a 10 x 10 2D array (also called a
AveGali [126]

Answer:

See explaination

Explanation:

//Array to store prices

INT PRICE[10][10]

//Loop through 2d array

FOR I FROM 0 TO 10

FOR J FROM 0 TO 10

//Populate a random int

PRICE[I][J] = RANDOM_INT(99,1999)

PRINT(PRICE[I][J]+" ")

PRINT()

//Prompt user for desired price

COST = READ_INPUT('Please enter your desired flight price: ')

//Variable to store if a match is found

MATCH = FALSE

//Loop through all ints in price

FOR I FROM 0 TO 10

FOR J FROM 0 TO 10

//Compare price with Cost

IF PRICE[I][J]==COST

MATCH = TRUE

BREAK

//Check if match is true

IF MATCH = TRUE

PRINT('A flight was found for that price, have a safe trip!')

ELSE

PRINT('No flight was found for that price, please try a new price.')

4 0
3 years ago
Default tab stops are set in word every _______ inch.<br> a. ¼<br> b. ¾<br> c. ½<br> d. 1
UNO [17]
Default tab stops are set in word every 1/2 inch so your correct answer is C :)
5 0
2 years ago
Design and implement a set of classes that define various types of reading material: books, novels, magazines, technical journal
tino4ka555 [31]

Answer:

Following are the code to this question:

please find the attached file.

Explanation:

In this code, four class "novels, magazines, technical journals, and textbooks", is defined, in which it holds their respective default constructor and the get and set method to hold the string and integer value, and in the book class the main method is defined, that creates its object and a switch to for search value and print its value.

6 0
3 years ago
Other questions:
  • Mike wants to build an amplifier. Which technology can he use?
    15·1 answer
  • Respond to the following in a paragraph of no less than 125 words. What is the purpose of netiquette guidelines?
    10·1 answer
  • Why is it important to ask an interviewer at least one question at the end of an interview?
    12·2 answers
  • 1. In the.js file, write the JavaScript code for this application. Within the click event handlers for the elements in the sideb
    14·1 answer
  • What does snap do need the answer now
    10·2 answers
  • Why does the job market change overtime
    9·1 answer
  • 4.17 LAB: Varied amount of input data ( C++)
    5·1 answer
  • What are some other ways to program a robot to navigate a complicated environment other than straight paths and right angle (90
    15·1 answer
  • Write a statement that slices a substring out of the string quote and puts it into a variable named selection. If given the stri
    8·1 answer
  • what is one benefit of placing voip gateways in geographically separated branch offices that have an existing wan connection?
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!