I need this program done in Java in basic steps I am in a level 200 class I am n
ID: 3733160 • Letter: I
Question
I need this program done in Java in basic steps I am in a level 200 class I am not advance and i wont undersand things over my head
Write a program that converts a Roman number such as MCMLXXVIII to its decimal number representation.
Hint: First write a method that yields the numeric value of each of the letters. Then use the following algorithm:
total = 0 While the roman number string is not empty If value(first character) is at least value(second character), or the string has length 1 Add value(first character) to total. Remove the character. Otherwise Add the difference value(second character) value(first character) to total. Remove both characters.Explanation / Answer
import java.util.Scanner;
public class ConvertRoman
{
public static String readAndConvert()
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter the roman number: ");
return scan.next();
}
public static int conversion(String roman)
{
if (roman.isEmpty())
return 0;
if (roman.length() == 1)
{
char ch = roman.charAt(0);
if (ch == 'M')
return 1000;
if (ch == 'D')
return 500;
if (ch == 'C')
return 100;
if (ch == 'L')
return 50;
if (ch == 'X')
return 10;
if (ch == 'V')
return 5;
if (ch == 'I')
return 1;
return 0;
}
int first = conversion(roman.substring(0, 1));
int second = conversion(roman.substring(1, 2));
if (first >= second)
{
return first + conversion(roman.substring(1));
}
else
{
return (second - first) + conversion(roman.substring(2));
}
}
public static void output(String roman, int value)
{
System.out.println("Roman Number: " + roman);
System.out.println("Decimal Value: " + value);
}
public static void main(String[] args)
{
String roman = readAndConvert();
int value = conversion(roman.toUpperCase());
output(roman, value);
}
}
/* sample output
Enter the roman number: LXXXIX
Roman Number: LXXXIX
Decimal Value: 89
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.