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
How to make a project using PID and thermacouple
Andrei [34K]

Explanation:

Required tools and components:

Soldering iron

Solder

Flux pen

Fine gauge solder (23 gauge or finer)

Wire cutter

MAX31855 thermocouple interface chip (available directly from Maxim Integrated)

Thermocouple with junction suitable for your application (e.g., probe, washer, etc.)

J-type (-40°C to +750°C)

K-type (-200°C to +1350°C)

T-type (-200°C to +350°C) Solid state relay (will depend on your application)

USB FTDI 5V cable (Note: A single cable can be used for many different microcontroller projects)

Computer with Arduino IDE v1.0

Wall-wart 9V/1.5A, 2.1mm center positive

6 0
3 years ago
A representation of something by using a description, summary or image.
ANTONII [103]

Answer: Thumbnail

Explanation:

Thumbnail is a representation of something by using a description, summary or image.

Thumbnails are simply the versions of either pictures or videos that have been reduced and can be used as placeholder for a multimedia content. A thumbnail that is used by a company on the internet gives the first impression of the product of the company to the prospective customers.

3 0
3 years ago
What is the nickname given to the new generation that was raised entirely within the digital age and often communicates through
Jobisdone [24]
This nickname would be Generation Me
4 0
3 years ago
Read 2 more answers
If a user wants to add an expansion card to increase the memory of a computer, where should the user insert the card?
uysha [10]

into a port on the outside of the computer


6 0
3 years ago
Read 2 more answers
Who needs to be involved when assembling business solutions that include hardware and software?
sveta [45]
Software engineer i would say
5 0
3 years ago
Read 2 more answers
Other questions:
  • Select the most likely outcome of making only on-time minimum payments to a credit card with a balance for an entire year?
    7·2 answers
  • Meg is in the process of creating a storyboard for her personal website, but she is unable to decide which storyboarding techniq
    10·1 answer
  • Type the correct answer in the box. Spell all words correctly.
    11·1 answer
  • Which of the following statements is true?
    8·1 answer
  • Using a cell phone while operating a motor vehicle is considered distraction because
    8·1 answer
  • 1. Define a C++ function with the name evaluateBook. The function receives as one argument the name of the file to be processed
    13·1 answer
  • Can change the tab colors of the worksheets in excel
    13·1 answer
  • Examples of analog computer
    8·1 answer
  • 2. Which pattern microphone should Jennifer take to the press conference? Jennifer is a journalist with one of the leading newsp
    14·1 answer
  • Describe the differences and similarities between the most common operating systems used for Mac, PC, and Linux in
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!