Question: JAVA ROMAN NUMERAL CONVERSION! PLEASE REMODIFY THE CODE BELOW WITHOUT ARRAYS. Program import java.util.*; public class RomanToNumber { int value(char r) { if (r ==

JAVA ROMAN NUMERAL CONVERSION!

PLEASE REMODIFY THE CODE BELOW WITHOUT ARRAYS.

Program

import java.util.*;

public class RomanToNumber {

int value(char r) { if (r == 'I') return 1; if (r == 'V') return 5; if (r == 'X') return 10; if (r == 'L') return 50; if (r == 'C') return 100; if (r == 'D') return 500; if (r == 'M') return 1000; return -1; } int romanToDecimal(String str) { // Initialize result int res = 0; for (int i=0; i= s2) { // Value of current symbol is greater // or equalto the next symbol res = res + s1; } else { res = res + s2 - s1; i++; // Value of current symbol is // less than the next symbol } } else { res = res + s1; i++; } } return res; } String decimalToRoman(Integer number) { number = Math.min(3999, Math.max(1, number)); // wraps number between 1-3999 String asRomanNumerals = "";

// Array including numerals in ascending order String[] RN = {"I", "V", "X", "L", "C", "D", "M" }; int i = 0; // Index used to keep track which digit we are translating while (number > 0) { switch(number % 10) { case 1: asRomanNumerals = RN[i] + asRomanNumerals; break; case 2: asRomanNumerals = RN[i] + RN[i] + asRomanNumerals; break; case 3: asRomanNumerals = RN[i] + RN[i] + RN[i] + asRomanNumerals; break; case 4: asRomanNumerals = RN[i] + RN[i + 1] + asRomanNumerals; break; case 5: asRomanNumerals = RN[i + 1] + asRomanNumerals; break; case 6: asRomanNumerals = RN[i + 1] + RN[i] + asRomanNumerals; break; case 7: asRomanNumerals = RN[i + 1] + RN[i] + RN[i] + asRomanNumerals; break; case 8: asRomanNumerals = RN[i + 1] + RN[i] + RN[i] + RN[i] +asRomanNumerals; break; case 9: asRomanNumerals = RN[i] + RN[i + 2] + asRomanNumerals; break; } number = (int) number / 10; i += 2; } return asRomanNumerals; }

public static void main(String[] args) { RomanToNumber ob = new RomanToNumber(); Scanner in = new Scanner(System.in); String s; do { System.out.println("1.Integer to Roman Coversion : 2. Roman to Integer Conversion "); System.out.println("Enter your choice: "); int choice=in.nextInt(); if(choice==1) { System.out.println("Input an integer < 4000 : "); System.out.println("The roman numeral version is: " + ob.decimalToRoman(in.nextInt())); } else if (choice==2) { System.out.println("Input a Roman Numeral : "); System.out.println("The integer number is : " + ob.romanToDecimal(in.next())); } else { System.out.println("Invalid Choice"); } System.out.println("Do you want to continue...(Press Y)"); s=in.next(); }while(s.equals("Y"));

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!