Write a RomanNumeral class that encapsulates a Roman numeral value. The methods
ID: 3636118 • Letter: W
Question
Write a RomanNumeral class that encapsulates a Roman numeral value. The methods the class has are: public void read(), public void set(String), public void set(RomanNumeral), public RomanNumeral add(RomanNumeral), public RomanNumeral subtract(RomanNumeral), public RomanNumeral multiply(RomanNumeral), public RomanNumeral divide(RomanNumeral), public RomanNumeral add(int), public RomanNumeral subtract(int), public RomanNumeral multiply(int), public RomanNumeral divide(int), public boolean equals(RomanNumeral), and public String toString(). Although historically there are many ways to write Roman numerals we will use these rules: The symbols "I", "X", "C", and "M" can be repeated three times in succession, but no more. "D", "L", and "V" can never be repeated. "I" can be subtracted from "V" and "X" only. "X" can be subtracted from "L" and "C" only. "C" can be subtracted from "D" and "M" only. "V", "L", and "D" can never be subtracted Only one small-value symbol may be subtracted from any large-value symbol.I will provide a main program as the demo.
public class RomanNumeralDemo
{
public static final int ARRAY_SIZE = 5;
public static void main(String[] args)
{
RomanNumeral rn1 = new RomanNumeral ("XXIV");
RomanNumeral rn2 = new RomanNumeral ("III");
RomanNumeral rn4, rn5, rn6, rn7;
RomanNumeral rn8 = new RomanNumeral(1999);
int number;
System.out.println ("Roman Numeral number of integer: " + rn8);
RomanNumeral [] RomanNumeralArray = new RomanNumeral [ARRAY_SIZE];
System.out.println ("First Roman Numeral number: " + rn1);
System.out.println ("Second Roman Numeral number: " + rn2);
if (rn1.equals(rn2))
System.out.println ("rn1 and rn2 are equal.");
else
System.out.println ("rn1 and rn2 are NOT equal.");
rn4 = rn1.add(rn2);
rn5 = rn1.subtract(rn2);
rn6 = rn1.multiply(rn2);
rn7 = rn1.divide(rn2);
System.out.println ("rn1 + rn2: " + rn4);
System.out.println ("rn1 - rn2: " + rn5);
System.out.println ("rn1 * rn2: " + rn6);
System.out.println ("rn1 / rn2: " + rn7);
System.out.println ();
number = 25;
System.out.println ("using the integer " + number +
" as the argument to the math operators ");
rn4 = rn1.add(number);
rn5 = rn1.subtract(number);
rn6 = rn1.multiply(number);
rn7 = rn1.divide(number);
System.out.println ("rn1 + number: " + rn4);
System.out.println ("rn1 - number: " + rn5);
System.out.println ("rn1 * number: " + rn6);
System.out.println ("rn1 / number: " + rn7);
//The next two methods must be written by you.
System.out.println ("Fill a roman numeral array with " + ARRAY_SIZE +" roman numerals ");
rn7.readRomanNumeralArray(RomanNumeralArray);
rn7 = rn7.getAverage(RomanNumeralArray);
System.out.println("the average of the Roman numeral “ +
“array is " + rn7);
}
}
Explanation / Answer
The Roman numerals class: /** * An object of type RomanNumeral is an integer between 1 and 3999. It can * be constructed either from an integer or from a string that represents * a Roman numeral in this range. The function toString() will return a * standardized Roman numeral representation of the number. The function * toInt() will return the number as a value of type int. */ public class RomanNumeral { private final int num; // The number represented by this Roman numeral. /* The following arrays are used by the toString() function to construct the standard Roman numeral representation of the number. For each i, the number numbers[i] is represented by the corresponding string, letters[i]. */ private static int[] numbers = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; private static String[] letters = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" }; /** * Constructor. Creates the Roman number with the int value specified * by the parameter. Throws a NumberFormatException if arabic is * not in the range 1 to 3999 inclusive. */ public RomanNumeral(int arabic) { if (arabic < 1) throw new NumberFormatException("Value of RomanNumeral must be positive."); if (arabic > 3999) throw new NumberFormatException("Value of RomanNumeral must be 3999 or less."); num = arabic; } /* * Constructor. Creates the Roman number with the given representation. * For example, RomanNumeral("xvii") is 17. If the parameter is not a * legal Roman numeral, a NumberFormatException is thrown. Both upper and * lower case letters are allowed. */ public RomanNumeral(String roman) { if (roman.length() == 0) throw new NumberFormatException("An empty string does not define a Roman numeral."); roman = roman.toUpperCase(); // Convert to upper case letters. int i = 0; // A position in the string, roman; int arabic = 0; // Arabic numeral equivalent of the part of the string that has // been converted so far. while (i number) { // Combine the two letters to get one value, and move on to next position in string. arabic += (nextNumber - number); i++; } else { // Don't combine the letters. Just add the value of the one letter onto the number. arabic += number; } } } // end while if (arabic > 3999) throw new NumberFormatException("Roman numeral must have value 3999 or less."); num = arabic; } // end constructor /** * Find the integer value of letter considered as a Roman numeral. Throws * NumberFormatException if letter is not a legal Roman numeral. The letter * must be upper case. */ private int letterToNumber(char letter) { switch (letter) { case 'I': return 1; case 'V': return 5; case 'X': return 10; case 'L': return 50; case 'C': return 100; case 'D': return 500; case 'M': return 1000; default: throw new NumberFormatException( "Illegal character "" + letter + "" in Roman numeral"); } } /** * Return the standard representation of this Roman numeral. */ public String toString() { String roman = ""; // The roman numeral. int N = num; // N represents the part of num that still has // to be converted to Roman numeral representation. for (int i = 0; i = numbers[i]) { roman += letters[i]; N -= numbers[i]; } } return roman; } /** * Return the value of this Roman numeral as an int. */ public int toInt() { return num; } } // end class RomanNumeral The main program class: /** * This program will convert Roman numerals to ordinary arabic numerals * and vice versa. The user can enter a numerals of either type. Arabic * numerals must be in the range from 1 to 3999 inclusive. The user ends * the program by entering an empty line. */ public class RomanConverter { public static void main(String[] args) { TextIO.putln("Enter a Roman numeral and I will convert it to an ordinary"); TextIO.putln("arabic integer. Enter an integer in the range 1 to 3999"); TextIO.putln("and I will convert it to a Roman numeral. Press return when"); TextIO.putln("you want to quit."); while (true) { TextIO.putln(); TextIO.put("? "); /* Skip past any blanks at the beginning of the input line. Break out of the loop if there is nothing else on the line. */ while (TextIO.peek() == ' ' || TextIO.peek() == ' ') TextIO.getAnyChar(); if ( TextIO.peek() == ' ' ) break; /* If the first non-blank character is a digit, read an arabic numeral and convert it to a Roman numeral. Otherwise, read a Roman numeral and convert it to an arabic numeral. */ if ( Character.isDigit(TextIO.peek()) ) { int arabic = TextIO.getlnInt(); try { RomanNumeral N = new RomanNumeral(arabic); TextIO.putln(N.toInt() + " = " + N.toString()); } catch (NumberFormatException e) { TextIO.putln("Invalid input."); TextIO.putln(e.getMessage()); } } else { String roman = TextIO.getln(); try { RomanNumeral N = new RomanNumeral(roman); TextIO.putln(N.toString() + " = " + N.toInt()); } catch (NumberFormatException e) { TextIO.putln("Invalid input."); TextIO.putln(e.getMessage()); } } } // end while TextIO.putln("thanks for using."); } // end main() } // end class RomanConverterRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.