Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Assignment is to write a program that converts Arabic numerals to Roman numerals. The program should be able to handle integers between 1 and

Java Assignment is to write a program that converts Arabic numerals to Roman numerals. The program should be able to handle integers between 1 and 3999. You should display an error message for numbers outside of this range. Allow the user to continue converting values until the enter -1 to quit. The code for doing the conversion should be contained within a metody with the following signature: public static String ArabicToRoman(int arabic);

I have the following code completed but it's not outputting the response into Roman.

import java.util.Scanner;

public class RomanNumerals {

private static String ArabicToRoman (int num) {

String [] romanLetters = {"I", "IV", "V", "IX", "X", "L", "XC", "C", "D", "CM", "M"} ;

int [] romanValues = {1, 4, 5, 9, 10, 50, 90, 100, 500, 900, 1000};

String romanOutput = "";

for (int i = 0; i < romanValues.length; i++) {

int quotient = num / romanValues[i];

if (quotient == 0) continue;

if (quotient == 4)

romanOutput = romanOutput+romanLetters[i]+romanLetters[i-1];

else

romanOutput += new String(new char[quotient]).replace("\0",romanLetters[i]);

num = num % romanValues[i];

}

return romanOutput;

}

public static void main(String[] args)

{

Scanner s = new Scanner(System.in);

System.out.print("Enter an integer (-1 to quit):");

int num = s.nextInt();

while (num!=-1) {

if(num<+ 0 || num > 3999)

System.out.println ("Invalid input. Please enter again.");

else

System.out.println(ArabicToRoman(num));

System.out.print("Enter an integer (-1 to quit):");

num = s.nextInt();

}

}

}

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

Recommended Textbook for

Introduction To Constraint Databases

Authors: Peter Revesz

1st Edition

1441931554, 978-1441931559

More Books

Students also viewed these Databases questions

Question

Communicate ethically.

Answered: 1 week ago

Question

Explain the forces that influence how people handle conflict

Answered: 1 week ago