Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a RomanNumeral class that encapsulates a Roman numeral value. The methods

ID: 3636144 • 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

package roman; public class RomanConversion { // Parallel arrays used in the conversion process. private static final String[] RCODE = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; private static final int[] BVAL = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; //=========================================================== binaryToRoman public static String binaryToRoman(int binary) { if (binary = 4000) { throw new NumberFormatException("Value outside roman numeral range."); } String roman = ""; // Roman notation will be accumualated here. // Loop from biggest value to smallest, successively subtracting, // from the binary value while adding to the roman representation. for (int i = 0; i = BVAL[i]) { binary -= BVAL[i]; roman += RCODE[i]; } } return roman; } } Source code for listener handler in GUI interface try { String dec = decimalTF.getText(); int bin = Integer.parseInt(dec); String rom = RomanConversion.binaryToRoman(bin); romanTF.setText(rom); } catch (NumberFormatException nfe) { romanTF.setText("Invalid input."); }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote