Write a RomanNumeral class that encapsulates a Roman numeral value. The methods
ID: 3635214 • 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.
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
a. Store the number as Roman numeral b. Convert and store the number into decimal c. Print the number as Roman numeral or decimal number as requested by the user. The decimal values of Roman numerals are: M 1000 D 500 C 100 L 50 X 10 V 5 I 1 01 import javax.swing.JOptionPane; 02 03 public class CH8EX1 04 { 05 public static void main(String[] args) 06 { 07 private String numStr = null; 08 private char ch; 09 private roman; 10 11 public CH8EX1() 12 { 13 do 14 { 15 16 numStr = JOptionPane.showinputDialog("Enter a number entered in roman numeral to convert to decimal"); 17 num = interger.parseInt(numStr); 18 19 switch(roman) 20 { 21 case M: ch = '1000'; 22 break; 23 case D: ch = '500'; 24 break; 25 case C: ch = '100'; 26 break; 27 case L: ch = '50'; 28 break; 29 case X: ch = '10'; 30 break; 31 case V: ch = '5'; 32 break; 33 case I: ch = '1'; 34 break; 35 } 36 JOPtionPane.showmessageDialog(null, "Roman Numeral Converted", 37 JOptionPane.INOFRMATION_MESSAGE); 38 } 39 System.exit(0) 40 } 41 }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.