Been racking my brain on this for a while now need some help java. In this assig
ID: 3588554 • Letter: B
Question
Been racking my brain on this for a while now need some help java.
In this assignment, your task is to write a program the converts Arabic numerals (the kind we usually use) to Roman numerals. Your program should be able to handle integers between 1 (the Romans had no way to represent zero, negative, or decimal numbers) 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 method with the following signature public static String arabicToRoman(int arabic); Part of the task of writing good code that is often overlooked in school is testing your code in a logical way. When you test code, it is important to have tests that cover each of the following situations normal case - a standard instance of the problem that tests the basic operation on normal inputs invalid case - tests that the program responds appropriately to various types of invalid inputs boundary case - a tricky instance of the problem that tests the operation on inputs that have to be handled in a special way . . . At the bottom of your code in this assignment, include a comment that explains your test plan - the inputs you chose for each case, why you chose them, and the results produced by your program. Do NOT write a description of your development or debugging process. Instead, provide a list of normal, invalid, and boundary test cases and the expected results Roman Numerals 10 50 100 500 1000 The Romans thought it would be easier to read if they did not have the same numeral four times in a row, so instead of lIII, the number 4 is written IV. Similarly, 9 is written IX instead of VII. 99 is XCIX. Please see Wikipedia if you need more information about Roman numerals. Hint: If you can get your program to produce the Roman numerals, but you are having trouble with the rule about not having four of the same character in a row, you might want to consider using the replaceAll method of the String class toExplanation / Answer
import java.util.Scanner;
public class RomanNumber {
public enum Digit {
M(1000, 3),
CM(900, 1),
D(500, 1),
CD(400, 1),
C(100, 3),
XC(90, 1),
L(50, 1),
XL(40, 1),
X(10, 3),
IX(9, 1),
V(5, 1),
IV(4, 1),
I(1, 3);
public final int value;
public final String symbol = name();
public final int maxArity;
private Digit(int value, int maxArity) {
this.value = value;
this.maxArity = maxArity;
}
}
private static final Digit[] DIGITS = Digit.values();
public static String of(int number) {
if (number == -1) {
System.out.println("Goodbye!");
return null;
}
if (number < 1 || 3999 < number) {
System.out.println("Roman numbers are only defined for numbers between 1 and 3999 ");
return null;
}
StringBuilder sb = new StringBuilder();
for (Digit digit : DIGITS) {
int value = digit.value;
String symbol = digit.symbol;
while (number >= value) {
sb.append(symbol);
number -= value;
}
}
return sb.toString();
}
public static int parse(String roman) {
if (roman.isEmpty()) {
System.out.println(" The empty string does not comprise a valid Roman number");
return -1;
}
int number = 0;
int offset = 0;
for (Digit digit : DIGITS) {
int value = digit.value;
int maxArity = digit.maxArity;
String symbol = digit.symbol;
for (int i = 0; i < maxArity && roman.startsWith(symbol, offset); i++) {
number += value;
offset += symbol.length();
}
}
if (offset != roman.length()) {
throw new NumberFormatException(String.format(
"The string '%s' does not comprise a valid Roman number",
roman
));
}
return number;
}
/** TESTS */
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// prompt for the user's name
System.out.print("Enter an integer: ");
// get their input as a String
String numberString = scanner.next();
int number=Integer.parseInt(numberString);
String roman = of(number);
if(roman!=null){
int parsed = parse(roman);
System.out.println(roman);
if (parsed != number) {
System.err.format(
"ERROR: number: %d, roman: %s, parsed: %d ",
number,
roman,
parsed
);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.