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

Write a Java application that converts integers that are less than 4000 into Rom

ID: 3768332 • Letter: W

Question

Write a Java application that converts integers that are less than 4000 into Roman Numerals and Roman Numerals into integers.

Prompt the user to enter either an integer or a Roman Numeral. If an integer is entered, then print its Roman Numeral equivalent; else, print the integer equivalent of the Roman Numeral. Loop until the user enters a -1. Print error messages if integers less than 1 or greater than 3999 are entered. Print error messages if invalid Roman Numerals are entered.

Your program must have a method named rntoi() that converts a roman numeral into an integer. And it must have a method named itorn() that converts an integer to roman numeral.

Roman Numeral Notes

Note: The same roman numeral digit cannot be used four times consecutively. Examples.

Explanation / Answer

Solution:

package com.urbanpro.nancy.solutions;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Roman {
   static String romanNumeral;
   static int decimalNumeral;
   static int decimalNum;

   public static void main(String args[]) {
       Scanner scan = new Scanner(System.in);
       System.out.print("Enter any integer(1 to 3999) or Roman number: ");
       if (scan.hasNextInt()) {
           decimalNumeral = scan.nextInt();
           if (decimalNumeral > 0 && decimalNumeral < 4000) {
               Roman rmtoD = new Roman();
               rmtoD.itorn(romanNumeral);
               rmtoD.printDecimal(decimalNumeral);
           } else // Displaying an error message if the number entered is out of range
               System.out.println("nYou entered a number out of Range.nPlease enter a number in the range [1-3999]");
       } else {
           romanNumeral = scan.next();
           String notRomanNumerals = romanNumeral.replaceAll("[IVXLCDM]", "");
           if(notRomanNumerals.length() != 0){
               System.out.println("You have entered wrong roman numeral." + notRomanNumerals);
           }else{
               Roman rmtoD = new Roman();
               rmtoD.rntoi(decimalNumeral);
               rmtoD.printRoman(romanNumeral);
           }
          
          
       }
   }

   private void itorn(String romanNumeral) {
       /*
       * Saving the Roman equivalent of the thousand, hundred, ten and units
       * place of a decimal number
       */
       String thou[] = { "", "M", "MM", "MMM" };
       String hund[] = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
       String ten[] = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
       String unit[] = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };

       /*
       * Finding the digits in the thousand, hundred, ten and units place
       */
       int th = decimalNumeral / 1000;
       int h = (decimalNumeral / 100) % 10;
       int t = (decimalNumeral / 10) % 10;
       int u = decimalNumeral % 10;

       /* Displaying equivalent roman number */
       System.out.println("Roman Equivalent= " + thou[th] + hund[h] + ten[t] + unit[u]);
   }

   public void rntoi(int decimalNumeral) {

       romanNumeral = romanNumeral.toUpperCase();

       int l = romanNumeral.length();
       int num = 0;
       int previousnum = 0;
       for (int i = l - 1; i >= 0; i--) {
           char x = romanNumeral.charAt(i);
           x = Character.toUpperCase(x);
           switch (x) {
           case 'I':
               previousnum = num;
               num = 1;
               break;
           case 'V':
               previousnum = num;
               num = 5;
               break;
           case 'X':
               previousnum = num;
               num = 10;
               break;
           case 'L':
               previousnum = num;
               num = 50;
               break;
           case 'C':
               previousnum = num;
               num = 100;
               break;
           case 'D':
               previousnum = num;
               num = 500;
               break;
           case 'M':
               previousnum = num;
               num = 1000;
               break;
           }
           if (num < previousnum) {
               decimalNum = decimalNum - num;
           } else
               decimalNum = decimalNum + num;
       }
   }

   public static void printRoman(String romanNumeral) {
       System.out.println("The equivalent of the Roman numeral " + romanNumeral + " is " + decimalNum);
   }

   public static void printDecimal(int decimalNum) {
       System.out.println("Decimal Number stored is: " + decimalNum);
   }
}

Output:

Enter any integer(1 to 3999) or Roman number: 4000
nYou entered a number out of Range.nPlease enter a number in the range [1-3999]

Enter any integer(1 to 3999) or Roman number: 3000
Roman Equivalent= MMM
Decimal Number stored is: 3000

Enter any integer(1 to 3999) or Roman number: VGHF
You have entered wrong roman numeral.GHF

Enter any integer(1 to 3999) or Roman number: MMM
The equivalent of the Roman numeral MMM is 3000

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