Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write in Java and explain what each step does please Problem 3: Write a method int convertRomanToDecimal(String roman) that takes in a valid Roman numeral

Write in Java and explain what each step does please

Problem 3: Write a method int convertRomanToDecimal(String roman) that takes in a valid Roman numeral string as input and returns its value as a base-10 number. An example of a Roman numeral is MCMLXXVIII, which translates to 1000 + (1000 100) + 50 + 10 + 10 + 5 + 3 = 1978. You should create a helper method int getRomanLetterValue(String romanLetter) which converts an individual letter in Roman numerals to its base-10 value. This only needs to work for the letters I, V, X, L, C, and M.

Refer to the table of Roman numeral values below. Remember that in Roman numerals, values generally go (left-to-right) from biggest to smallest. The exception to this is when a smaller value appears before a bigger value, like in the numeral IX, which denotes a subtraction of the smaller value from the larger one. There are two rules about how this works.

1. Only one smaller value may appear before a bigger value. This is why 8 is represented as VIII = 5 + 1 + 1 and not IIX. This is part of the pseudocode algorithm we give you. 2. A smaller value can only appear before bigger values which are at most two levels greater than it. For example, X may appear before L and C but not M, like in XCV = 95 and XLII = 42. This is not part of the algorithm we give you, but you dont need to worry about it as you can assume all inputs are valid Roman numerals. Letter Value I 1 V 5 X 10 L 50 C 100 M 1000 Implement the following pseudocode for your convertRomanToDecimal method. total = 0 str = roman number string While str is not empty If str has length 1, or value(first character of str) is at least value(second character of str) Add value(first character of str) to total. Remove first character from str. Else difference = value(second character of str) - value(first character of str) Add difference to total. Remove first character and second character from str.

Finally, write a main method which displays the value of the upcoming Super Bowl as a base-10 number, exactly matching the output below. Hardcode the String This year is Super Bowl LVII, which is number and call the method on it to get the integer. After, the main method should ask the user to input their own Roman numeral string, and the program will convert it to base-10 and display the result. Example: This year is Super Bowl LVII, which is number 57. Enter your own Roman numeral: MCMLXXVIII MCMLXXVIII in base-10 is 1978

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions