Create a program to test various operations


Assignment:

a.) Modify the definition of the class Roman so that the data members are declared as protected. Also incluse the method decimalToRoman, which converts the decimal number (the decimal number must be a positive integer) to an equivalent Roman numeral format. Write the definition of the method decimalToRoman. Your definition of the class Roman must contain the method toString, which returns the string containing the number in Roman format. For simplicity, we assume that only the letter I can appear in front of another letter and that it appears only in front of the letters V and X. For example, 4 is represented as IV, 9 is represented as IX, 39 is represented as XXXIX, and 49 is represented as XXXXIX. Also, 40 is represented as XXXX, 190 is represented as CLXXXX, so on.

b.) Derive the class ExtendedRoman from the class Roman to do the following. In the class ExtendedRoman, include the methods add, subtract, multiply, and divide so that arithmetic operations can be performed on Roman numerals.
To add (subtract, multiply, or divide) Roman numerals, add (subtract, multiply, or divide, respectively) their decimal representations and then convert the result to the Roman numeral format. For subtraction, if the first number is smaller than the second number, throw the exception, "Because the first number is smaller than the second, the numbers cannot be subtracted." Similarly, or division, the numerator must be larger than the denominator.

c.) Write the definition of the methods add, subtract, multiply, and divide as described in part B. Also, your definition of the class ExtendedRoman must contain the method toString that returns the string containing the number in Roman format.

d.) Write a program to test various operations on your class ExtendedRoman.

public class Roman

{
    private String romanNum;
    private int decimalNum;

    public Roman()
    {
       romanNum = "I";
       decimalNum = 1;
    }

    public Roman(String rString)
    {
        romanNum = rString;
      	romanToDecimal();
    }

    public void printDecimal()
    {
        System.out.println(decimalNum);
    }

    public void printRoman()
    {
     	System.out.println(romanNum);
    }

    public void setRoman(String rString)
    {
        romanNum = rString;
        romanToDecimal();
    }

    public String getRoman()
    {
        return romanNum;
    }

    public void romanToDecimal()
    {
      	int sum = 0;
      	int len =  romanNum.length();
      	int i;

       	int previous = 1000;

      	for (i = 0; i < len; i++)
      	{
         	switch (romanNum.charAt(i))
         	{
         	case 'M': sum = sum + 1000;
                      if (previous < 1000)
                          sum = sum -  2 * previous;
                      previous = 1000;
                      break;
         	case 'D': sum = sum + 500;
                      if (previous < 500)
                          sum = sum - 2 * previous;
                      previous = 500;
                      break;
         	case 'C': sum = sum + 100;
                      if (previous < 100)
                          sum = sum - 2 * previous;
                      previous = 100;
                      break;
         	case 'L': sum = sum + 50;
                      if (previous < 50)
                          sum = sum - 2 * previous;
                      previous = 50;
                      break;
         	case 'X': sum = sum + 10;
                      if (previous < 10)
                          sum = sum - 2 * previous;
                      previous = 10;
                      break;
         	case 'V': sum = sum + 5;
                      if (previous < 5)
                          sum = sum - 2 * previous;
                      previous = 5;
                      break;
         	case 'I': sum = sum + 1;
                      previous = 1;
         	}
 		}

 		decimalNum = sum;
    }
}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Create a program to test various operations
Reference No:- TGS01934416

Now Priced at $25 (50% Discount)

Recommended (94%)

Rated (4.6/5)