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

(Java programming )Use the test application shown below to guide you in creating

ID: 3663502 • Letter: #

Question

(Java programming )Use the test application shown below to guide you in creating your class. Complete the five tasks specified in the comments. This code should be run to produce your output.

public class RomanApp {public static void main(String[] args) {System.out.println("Test Application for RomanApp");

//TASK 1: DECLARE TWO ROMAN NUMERALS.
RomanNumeral x1 = newRomanNumeral("DXI");
RomanNumeral x2 = newRomanNumeral();

//TASK 2: VERIFY THE INITIAL VALUES OF BOTH OBJECTS
System.out.println("x1 decimal = " + x1.decimalValue() + " roman = " + x1.romanValue());
System.out.println("x2 decimal = " + x2.decimalValue() + " roman = " + x2.romanValue());

//TASK 3: RESET THE ROMAN VALUE FOR X2
System.out.println(" RESET X2:");
System.out.println("BEFORE decimal = " + x2.getDecimalValue() + " roman = " + x2.getRomanValue());
x2.set("XD");
System.out.println("AFTER decimal = " + x2.getDecimalValue() + " roman =" + x2.getRomanValue());

//TASK 4: INCREMENT X2
System.out.println(" INCREMENTING X2:");
System.out.println("BEFORE decimal = " + x2.getDecimalValue() + " roman =" + x2.getRomanValue());
x2.increment();
System.out.println("AFTER decimal = " + x2.getDecimalValue() + " roman =" + x2.getRomanValue());
if (x1.equals(x2))

System.out.println("x1 and x2 are equal in value.");

else

System.out.println("x1 and x2 are NOT equal in value.");

}
}

Expected output:

Test Application for RomanApp
x1 decimal = 511 roman = DXI
x2 decimal = 1 roman = I

RESET X2:
BEFORE decimal = 1 roman = I
AFTER decimal = 510 roman =XD


INCREMENTING X2:
BEFORE decimal = 510 roman =XD
AFTER decimal = 511 roman =XDI
x1 is equal to x2.

Explanation / Answer

public class RomanNumeral {
   // I = 1
   // V = 5
   // X = 10
   // L = 50
   // C = 100
   // D = 500
   // M = 1000

   private int decimal = 0;
   private String roman = "";
   private int lastNumber = 0;

   public RomanNumeral() {
       this("");
   }

   public RomanNumeral(String romanString) {
       this.roman = romanString;
       if (romanString.isEmpty()) {
           this.decimal = 1;
           this.roman = "I";
       } else
           this.decimal = romanToDecimal(romanString);
   }

   public int decimalValue() {
       return this.decimal;
   }

   public String romanValue() {
       return this.roman;
   }

   public int getDecimalValue() {
       return this.decimal;
   }

   public String getRomanValue() {
       return this.roman;
   }

   public void set(String romanString) {
       this.roman = romanString;
       if (romanString.isEmpty()) {
           this.decimal = 1;
           this.roman = "I";
       } else
           this.decimal = romanToDecimal(romanString);
   }

   public void increment() {
       this.decimal++;
       decimalToRoman();
   }

   private int[] numbers = { 1000,900,500,400,100,90,50,40,10,9,5,4,1};

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

   private void decimalToRoman() {
   int value = this.decimal;
   for (int i=0; i<numbers.length; i++) {
   while (value >= numbers[i]) {
   value -= numbers[i];
   this.roman += letters[i];
   }
   }
   }

   private int romanToDecimal(String romanNumber) {

       this.lastNumber = 0;
       this.decimal = 0;
       String romanNumeral = romanNumber.toUpperCase();
       for (int x = romanNumeral.length() - 1; x >= 0; x--) {
           char convertToDecimal = romanNumeral.charAt(x);

           switch (convertToDecimal) {
           case 'M':
               decimal = processDecimal(1000, lastNumber, decimal);
               lastNumber = 1000;
               break;

           case 'D':
               decimal = processDecimal(500, lastNumber, decimal);
               lastNumber = 500;
               break;

           case 'C':
               decimal = processDecimal(100, lastNumber, decimal);
               lastNumber = 100;
               break;

           case 'L':
               decimal = processDecimal(50, lastNumber, decimal);
               lastNumber = 50;
               break;

           case 'X':
               decimal = processDecimal(10, lastNumber, decimal);
               lastNumber = 10;
               break;

           case 'V':
               decimal = processDecimal(5, lastNumber, decimal);
               lastNumber = 5;
               break;

           case 'I':
               decimal = processDecimal(1, lastNumber, decimal);
               lastNumber = 1;
               break;
           }
       }
       return decimal;
   }

   public static int processDecimal(int decimal, int lastNumber,
           int lastDecimal) {
       if (lastNumber > decimal) {
           return lastDecimal - decimal;
       } else {
           return lastDecimal + decimal;
       }
   }

}